Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Calling a function directly via VTable.

Calling a function directly via VTable.

Scheduled Pinned Locked Moved C / C++ / MFC
csharpc++visual-studiocomhelp
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Green Fuze
    wrote on last edited by
    #1

    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); // <--- happens the problem. comobj->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!

    A 1 Reply Last reply
    0
    • G Green Fuze

      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); // <--- happens the problem. comobj->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!

      A Offline
      A Offline
      Aescleal
      wrote on last edited by
      #2

      I probably shouldn't encourage this sort of low level hackery, it'll only hurt someone you work with one day... However, why what you're up to won't work is due to the "this" pointer. All C++ member function calls have an implied first parameter which is the address of the object the function is invoked on. So if you write: a->b( c ); the compiler converts this to: ::b( &a, c ); So in your case if you call: p( &comobj, bstr ); it might all start working swimmingly. Oh, and if it does don't tell anyone I told you or generations of coders will be cursing my name for spreading low level hacks around. Cheers, Ash

      C G 2 Replies Last reply
      0
      • A Aescleal

        I probably shouldn't encourage this sort of low level hackery, it'll only hurt someone you work with one day... However, why what you're up to won't work is due to the "this" pointer. All C++ member function calls have an implied first parameter which is the address of the object the function is invoked on. So if you write: a->b( c ); the compiler converts this to: ::b( &a, c ); So in your case if you call: p( &comobj, bstr ); it might all start working swimmingly. Oh, and if it does don't tell anyone I told you or generations of coders will be cursing my name for spreading low level hacks around. Cheers, Ash

        C Offline
        C Offline
        Chris Meech
        wrote on last edited by
        #3

        Great answer. Including all of the "It wasn't me who told you how to do this". :)

        Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra]

        1 Reply Last reply
        0
        • A Aescleal

          I probably shouldn't encourage this sort of low level hackery, it'll only hurt someone you work with one day... However, why what you're up to won't work is due to the "this" pointer. All C++ member function calls have an implied first parameter which is the address of the object the function is invoked on. So if you write: a->b( c ); the compiler converts this to: ::b( &a, c ); So in your case if you call: p( &comobj, bstr ); it might all start working swimmingly. Oh, and if it does don't tell anyone I told you or generations of coders will be cursing my name for spreading low level hacks around. Cheers, Ash

          G Offline
          G Offline
          Green Fuze
          wrote on last edited by
          #4

          It works! :-) That's so awesome! I'm just asking to get the whole idea of that interface, COM and vtable stuff better. Thanks a lot ! :-)

          A 1 Reply Last reply
          0
          • G Green Fuze

            It works! :-) That's so awesome! I'm just asking to get the whole idea of that interface, COM and vtable stuff better. Thanks a lot ! :-)

            A Offline
            A Offline
            Aescleal
            wrote on last edited by
            #5

            I was hoping that it was just to get an idea of what's going on under the covers, glad I could help. Cheers, Ash

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups