Search code examples
c++windowsetwindowshookex

system-wide hook for 64-bit operating systems


I want to perform a system-wide hook (using SetWindowHook) on a 64bit operating system.

I know that 64bit processes (= proc64) can load only 64bit dlls (= dll64) and 32bit processes (= proc32) can load only 32bit dlls (= dll32).

Currently I am planning to call SetWindowHook twice, once with dll32 and once with dll64, expecting that proc64s will load dll64 and proc32s will load dll32 (while dll32 for proc64s and dll64 for proc32s will fail).

Is that the correct way to do that, or is there a "more correct" way to do that?

Thanks! :-)


Solution

  • Approach that you've described is correct and documented.

    From http://msdn.microsoft.com/en-us/library/ms644990(v=vs.85).aspx:

    SetWindowsHookEx can be used to inject a DLL into another process. A 32-bit DLL cannot be injected into a 64-bit process, and a 64-bit DLL cannot be injected into a 32-bit process. If an application requires the use of hooks in other processes, it is required that a 32-bit application call SetWindowsHookEx to inject a 32-bit DLL into 32-bit processes, and a 64-bit application call SetWindowsHookEx to inject a 64-bit DLL into 64-bit processes. The 32-bit and 64-bit DLLs must have different names.

    Note the last statement that names of 32-bit and 64-bit DLLs MUST be different.