c++keyboardhookdirectinput

Function pointer - purpose of argument for DInput8.dll hook code


I have been trying to figure out the purpose of one argument in a DirectInput8 hooking project. I am producing some portion the opensource code here. For the sake of brevity, I am only posting a few functions.

ULONG oldCreateDevice;
HRESULT WINAPI xCreateDevice(DWORD d1, DWORD d2, DWORD d3, DWORD d4)
{
    HRESULT hr = ((HRESULT(WINAPI*)(DWORD,DWORD,DWORD,DWORD))oldCreateDevice)(d1,d2,d3,d4);

    // hook only if keyboard requested
    if(*(DWORD*)d2 != GUID_SysKeyboard)
        return hr;

    DWORD dwKeybTable = *(DWORD*)(*(DWORD*)d3);

    DWORD oldprot;
    VirtualProtect((LPVOID)dwKeybTable, 0x2C, PAGE_EXECUTE_READWRITE, &oldprot);

    // already hooked?
    if((DWORD)xGetDeviceState == *((DWORD*)(dwKeybTable+0x24))) goto ex1;
    // hook it!
    oldGetDeviceState = *((DWORD*)(dwKeybTable+0x24));
    *((DWORD*)(dwKeybTable+0x24)) = (DWORD)xGetDeviceState;

ex1:
    // already hooked?
    if((DWORD)xGetDeviceData == *((DWORD*)(dwKeybTable+0x28))) goto ex2;
    // hook it!
    oldGetDeviceData = *((DWORD*)(dwKeybTable+0x28));
    *((DWORD*)(dwKeybTable+0x28)) = (DWORD)xGetDeviceData;

ex2:

    return hr;

}


ULONG oldDirectInput8Create;
HRESULT WINAPI xDirectInput8Create(HINSTANCE hinst, DWORD dwVersion, REFIID riidltf, VOID **ppvOut, LPUNKNOWN punkOuter)
{
    HRESULT ret = ((HRESULT(WINAPI*)(HINSTANCE,DWORD,REFIID,VOID**,LPUNKNOWN))oldDirectInput8Create)(hinst,dwVersion,riidltf,ppvOut,punkOuter);

    DWORD dwFuncTable = (DWORD)*((DWORD*)*ppvOut);

    DWORD oldprot;
    VirtualProtect((LPVOID)dwFuncTable, 0x10, PAGE_EXECUTE_READWRITE, &oldprot);

    //already hooked?
    if((DWORD)xCreateDevice == *((DWORD*)(dwFuncTable + 0x0C))) goto ex;
    //hook it
    oldCreateDevice = *((DWORD*)(dwFuncTable + 0x0C));
    *((DWORD*)(dwFuncTable + 0x0C)) = (DWORD)xCreateDevice;

ex:
    return ret;
}


DWORD WINAPI RemoteMain(LPVOID lpParam)
{

    LoadLibrary("user32.dll");
    LoadLibrary("advapi32.dll");

    Splice_Init();

    Splice((ULONG)GetProcAddress(LoadLibrary("dinput8.dll"),"DirectInput8Create"), xDirectInput8Create, &oldDirectInput8Create);

    ThreadControl(FALSE); // resume all
    return 0;
}

As you might notice that the code is replacing functions with redirected (hooked) ones. My question is for the first argument in xCreateDevice function For DirectInput8, the CreateDevice function takes up 3 parameters as per documentation which I understand. But I am not sure why 4 parameters are being used here. Does it point to the that the original "oldCreateDevice"? Or is it something else?

I hope someone can guide me on this. Thanks


Solution

  • It's an interface (class) non-static method, so the first argument is this pointer (pointer to an instance of a class).

    this (Wikipedia)

    this (MSDN)