Stack Exchange
Incorrect Message Hooking
After asking about hooking a game's message queue on here, I was told to use GetWindowThreadProcessId(). Here is the code I've written and am injecting, it doesn't seem to work at all.
LRESULT CALLBACK MessageHook ( int code, WPARAM wParam, LPARAM lParam)
{
    if (code >= 0)
    {
        if(((tagMSG*)lParam)->message == WM_KEYDOWN)
            MessageBox(NULL, "Called", "Success", MB_OK);
    }


    return CallNextHookEx (hookHandle, code, wParam, lParam);
}

void OnAttach(HMODULE hDll)
{
    while(1)
    {
        Sleep(100);
        if( GetAsyncKeyState( VK_INSERT ) &0x8000 )
        break;
    }


    hookHandle = SetWindowsHookEx (WH_GETMESSAGE, MessageHook, hDll,
                                  GetWindowThreadProcessId(GetForegroundWindow(), nullptr));

    if(hookHandle == NULL)
        MessageBoxA(NULL, "Error Hooking Message Queue", "Problem", MB_OK);
}

However nothing happens at all, from what I understand from a previous answer the hWnd thread must be the one that handles the message queue, so it doesn't seem possible that I'm hooking the wrong queue. I'm probably wrong though

Have a look at this.

Updated the code now as per your link, thanks for that :) Still does not work though


Make sure the hook callback function is in a DLL and 2) try running your app with elevated privileges.

