calling setwindowshookex with WH_GETMESSAGE doesn't work on any process except the injector

StackOverflow https://stackoverflow.com/questions/4340313

  •  30-09-2019
  •  | 
  •  

Question

I previously asked a question about something similar but I believe this time the circumstances are different.

I have a DLL that has standard hook, unhook and msgProc functions. I load this DLL in my main application and then call 'hook', that is below:

HOOKDLL_API BOOL setHook( HWND hWnd, DWORD threadID )
{

if( hWndServer != NULL )
    return FALSE;

hook = SetWindowsHookEx( WH_GETMESSAGE, (HOOKPROC)msghook, hInstance, threadID );

if( hook != NULL )
{
    hWndServer = hWnd;

    ofstream logFile;
    logFile.open( "LOG.txt" );
    logFile << "Hooked for: " << hWndServer << endl;
    logFile.close();

    return TRUE;
}

return FALSE;
}

The problem is that if I make it global, with threadID = 0, then msgHook only and only captures the messages received by the window of the process that loaded the library and nothing else, even if it is supposed to be a global hook.

If I supply a threadID of some other window, then I don't receive messages at all.

What could perhaps be the reason for it?

Was it helpful?

Solution

  • Are you using 64-bit Windows? If so, your hook process and DLL must match the bitness of the process(es) you wish to hook.

  • What is hInstance in your example? The DLL or the EXE instance? It should be the DLL that contains the msgHook function.

  • What does your msgHook do? How do you detect whether or not it is being called? Note that it will be called within the process(es) that you hook, not within your own process. (So if you've set a breakpoint on it, it won't be triggered unless you attach the debugger to the process you've hooked, rather than the process that installed the hook.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top