SetWindowsHookEx gibt 0 zurück, wenn für das .NET 4.0-Framework auf 32-Bit-Computern kompiliert wird.

Ich versuche, einen Low-Level-Windows-Tastaturhaken festzulegen, um drei gedrückte Tasten zu erfassen, auch wenn die Anwendung nicht im Fokus steht. Dazu rufe ich SetWindowsHookEx als

// Create an instance of HookProc.
KeyboardHookProcedure = new HookProc(KeyboardHookProc);
//install hook
hKeyboardHook = SetWindowsHookEx(
    WH_KEYBOARD_LL,
    KeyboardHookProcedure,
    Marshal.GetHINSTANCE(
        Assembly.GetExecutingAssembly().GetModules()[0]),
    0);
//If SetWindowsHookEx fails.
if (hKeyboardHook == 0)
{
    //Returns the error code returned by the last unmanaged function called using platform invoke that has the DllImportAttribute.SetLastError flag set. 
    int errorCode = Marshal.GetLastWin32Error();
    //do cleanup
    Stop(false, true, false);
    //Initializes and throws a new instance of the Win32Exception class with the specified error. 
    throw new Win32Exception(errorCode);
}

Früher funktionierte dies mit .NET Framework 3.5 auf 32-Bit- und 64-Bit-Computern, nach dem Upgrade auf .NET Framework 4.0 jedoch nicht mehr auf 32-Bit-Computern.

Weiß jemand, wie man das löst, damit ich das 4.0-Framework verwenden und es sowohl auf 32-Bit- als auch auf 64-Bit-Computern zum Laufen bringen kann?

Antworten auf die Frage(8)

Ihre Antwort auf die Frage