Using COM Interfaces THIS pointer Question

Sorry about the incredibly stupid title, anyway, here's my question.

I have been exploring Direct3D for a while now, and I noticed when debugging that the THIS pointer is pushed onto the stack instead of passed through the ECX register.

g_pd3dDevice->EndScene();
mov         eax,dword ptr [g_pd3dDevice (4095E4h)] // get THIS pointer
mov         ecx,dword ptr [eax] // get vTable
push        eax // push THIS pointer
call        dword ptr [ecx+0A8h] // IDirect3D9::EndScene(void)

*please note that the fact it compiled to move ecx, dword ptr [eax] is just a co-incidence, it sometimes compiles to ebx aswell.

I was looking at how the COM interfaces have the THIS pointer pushed onto the stack instead of sent through ECX.  I already know why (for the ability to use with non C++ languages).

At first I thought that the calling convention was just __cdecl, but then I noticed the stack isn't updated after the call (so it must be __stdcall, which is what it shows in d3d9.h).

I tried to figure out how the header file was setup so the THIS pointer is pushed instead of sent through ECX, but the horde if #ifdef and #define's confused me.

I'm looking for your help, what code tells the compiler to compile g_pd3dDevice->EndScene() to push the THIS pointer instead of ECX.

Thank you for your time,
Matthew


Answer this question

Using COM Interfaces THIS pointer Question

  • s_rowse

    This ist the standard for most/all C++ compilers. The this pointer is just the first argument.


    IUnknown *pUnk = PointerFromAnyWhere();
    ...
    // This syntax causes the pUnk pointer pushed on the stack and the
    // corresponding vTable entry to be executed,
    pUnk->Release();

     


  • Dmitriyy

    when creating my own classes / interfaces

    interface IVInterface
    {
    public:
        virtual void TestFunc() = 0;
    };

    class CClass : public IVInterface
    {
    public:
        virtual void TestFunc();
    };

    void CClass::TestFunc()
    {
        cout << "Something" << endl;
    }

    When debugging a class to CClass::TestFunc(), the THIS pointer is sent through ECX, and nothing is pushed onto the stack.

    IVInterface *pInt = new CClass;

        pInt->TestFunc();
    00401039  mov         edx,dword ptr [eax] // get vTable
    0040103B  mov         ecx,eax // move THIS pointer into ECX from EAX
    0040103D  call        dword ptr [edx] // call the first vTable function

    Since COM uses the keyword interface, I'm just wondering what makes the compiler compile it to pushing the THIS pointer instead of ECX.

    Thanks for your help,
    Matthew

  • Francois-Regis Colin

    Thre is no trick in COM that allows the this pointer to be passed in any other way than on the stack.
    There must be a push!

  • f00sion

    In Visual C++ member functions of a class are, by-default, compiled with the __thiscall calling-convention: part of the definition of this calling-convention is that the first two arguments (which incudes the this-pointer) are passed in register.

    But if you take a look at the declaration of a member function of a COM interface you will see that it is explicitly declared with the __stdcall calling-convention: part of the definition of this calling-convention is that all the arguments are passed on the stack.

    This explains the difference you are seeing between calling a COM method and calling your own method.

  • Richard Hough

    I mean whats the C++ syntax that allows the compiler to compile the THIS pointer to being pushed onto the stack instead of sent through the ECX register.
  • Poster Ho

    Thanks a lot for your help :)

  • Using COM Interfaces THIS pointer Question