Error with pinvoke function getKeyBoardLayout()
-
14-11-2019 - |
質問
I'm trying to write a basic background keylogger... Keyboard scanCodes and States are converted via pinvoke functions ToAsciiEx or ToUnicodeEx. These function have an argument for KeyboardLayout. I have a function (see below) for getting the current (active windows) keyboard layout. But this functions always returns 0. Error code is 6 (ERROR_INVALID_HANDLE).
Any sugessions ?
thx for answers
static public IntPtr getActiveKeyBoardLayout()
{
int handle = 0;
handle = GetForegroundWindow();
IntPtr i = new IntPtr(handle);
HandleRef hr = new HandleRef(wrapper, i);
int pid;
GetWindowThreadProcessId(hr, out pid);
IntPtr layout = GetKeyboardLayout(pid);
int er = Marshal.GetLastWin32Error();
if (er > 0)
{
System.Console.Out.WriteLine("error " + er.ToString());
}
return layout;
}
解決
You are passing a process ID to the function. It requires a thread ID. The return value of GetWindowThreadProcessId(). The way you use Marshal.GetLastWin32Error() is wrong too, you should only use it when an API function returned a failure code.
所属していません StackOverflow