C++ Programming
05-20-2010, 06:40 PM
Hey everybody.
I am trying to a call a virtual function directly from the VTable.
I created a COM component using the visual studio (ATL Simple object).
STDMETHODIMP Ctest_com::print_me(BSTR txt)
{
OutputDebugString(txt);
return S_OK;
}
Now, I am trying this use the COM, but calling the function via VTable:
// signature of print_me.
// STDMETHODCALLTYPE is __stdcall
typedef HRESULT (STDMETHODCALLTYPE* ptr_print)(BSTR);
int _tmain(int argc, _TCHAR* argv[])
{
CoInitialize(NULL);
cpp_com_testLib::Itest_com* comobj;
HRESULT hr = CoCreateInstance(__uuidof(cpp_com_testLib::test_com),
NULL,
CLSCTX_INPROC_SERVER,
__uuidof(cpp_com_testLib::Itest_com),
(void**)&comobj);
int* vptr = (int*)(comobj);
vptr = (int*)*vptr; // gets a pointer to the VTABLE
int* vproc0 = (int*)vptr[0]; // get a point to first function in VTable
...
...
...
int* vproc7 = (int*)vptr[7]; // pointer to print_me() !
ptr_print p = (ptr_print)vproc7; // cast to the function pointer.
_bstr_t bstr(_T("MY TEXT!"));
p(bstr); //<span class="code-comment"> Release();
CoUninitialize();
return 0;
}
NOW, here is THE PROBLEM.
When I call "p(bstr)" I do get to "print_me()", BUT the parameter "txt" is not passed to the function correctly (I sends a whole different address, so it is a BAD POINTER).
ANY IDEAS????
THANKS A LOT IN ADVANCE!
I am trying to a call a virtual function directly from the VTable.
I created a COM component using the visual studio (ATL Simple object).
STDMETHODIMP Ctest_com::print_me(BSTR txt)
{
OutputDebugString(txt);
return S_OK;
}
Now, I am trying this use the COM, but calling the function via VTable:
// signature of print_me.
// STDMETHODCALLTYPE is __stdcall
typedef HRESULT (STDMETHODCALLTYPE* ptr_print)(BSTR);
int _tmain(int argc, _TCHAR* argv[])
{
CoInitialize(NULL);
cpp_com_testLib::Itest_com* comobj;
HRESULT hr = CoCreateInstance(__uuidof(cpp_com_testLib::test_com),
NULL,
CLSCTX_INPROC_SERVER,
__uuidof(cpp_com_testLib::Itest_com),
(void**)&comobj);
int* vptr = (int*)(comobj);
vptr = (int*)*vptr; // gets a pointer to the VTABLE
int* vproc0 = (int*)vptr[0]; // get a point to first function in VTable
...
...
...
int* vproc7 = (int*)vptr[7]; // pointer to print_me() !
ptr_print p = (ptr_print)vproc7; // cast to the function pointer.
_bstr_t bstr(_T("MY TEXT!"));
p(bstr); //<span class="code-comment"> Release();
CoUninitialize();
return 0;
}
NOW, here is THE PROBLEM.
When I call "p(bstr)" I do get to "print_me()", BUT the parameter "txt" is not passed to the function correctly (I sends a whole different address, so it is a BAD POINTER).
ANY IDEAS????
THANKS A LOT IN ADVANCE!