SetWindowsHookEx returns 0 when compiling for the .NET 4.0 framework in 32bit machines

后端 未结 4 922
情话喂你
情话喂你 2020-12-16 20:38

I\'m trying to set a low level windows keyboard hook to grab three keys pressed even if the application is not in focus. To do this I\'m calling SetWindowsHookEx as

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-16 21:07

    Hans Passant:

    Any module handle will do since it doesn't actually get used for low-level hooks, no DLL needs to be injected to make them work. Some care in selecting one is required for .NET 4 since its CLR no longer fakes module handles for pure managed assemblies. A good one to use is the one you get out of pinvoking LoadLibrary("user32.dll") since it is always already loaded. You don't have to call FreeLibrary().

    You'll need this declaration to call LoadLibrary:

    [DllImport("kernel32", SetLastError=true, CharSet = CharSet.Auto)]
    private static extern IntPtr LoadLibrary(string fileName);
    

提交回复
热议问题