1
votes

I'm facing the problem, that my I'm unable to get a valid hookID returned by SetWindowsHookEx and that GetLastWin32Error() returning 0 too. I wrote the code below:

_LowLevelhookID = SetWindowsHookEx((int)HookType.WH_KEYBOARD_LL, LowLevelKeyboardHookHandler, IntPtr.Zero, GetCurrentThreadId());

int errorCode = -1;

if(_LowLevelhookID == (IntPtr)0)
{
    errorCode = Marshal.GetLastWin32Error(); 
}

Actually I want to create a low level keyboard hook for my application only and according to SetWindowsHookEx parameters description i shall set the hMod parameter to null if I want to set the current threadID and capture only the keyevents for my application.

can anybody confirm that GetCurrentThreadId() is the correct method to desire the ThreadID in which my app is running?

1

1 Answers

2
votes

WH_KEYBOARD_LL hook is global, you cannot install it for your app only.

Marshal.GetLastWin32Error() is not working probably because you did not added SetLastError = true to DllImportAttribute for SetWindowsHookEx.