0

This question is a follow on from: How do you disable system hotkeys in user32.dll? . I have not added it to chat because after googling, this appears to be an issue and I haven't found a solution which works. The error code seems to have multiple causes, one of which being a possibly corrupt dll. The fact that I can't figure out whether or not this is the case is a problem.

The code from this question comes from: https://www.codeproject.com/articles/7294/processing-global-mouse-and-keyboard-hooks-in-c Look for the file "HookManager.Callbacks.cs"

Useful: https://msdn.microsoft.com/en-us/library/windows/desktop/ms644990(v=vs.85).aspx

I keep getting the following error:

Error: System.ComponentModel.Win32Exception (0x80004005): The specified module could not be found at Gma.UserActivityMonitor.HookManager.EnsureSubscribedToGlobalKeyboardEvents() in [REDACTED DIRECTORY]HookManager.Callbacks.cs:line 401

The code causing this:

private static void EnsureSubscribedToGlobalKeyboardEvents()
    {
        // install Keyboard hook only if it is not installed and must be installed
        if (s_KeyboardHookHandle == 0)
        {
            //See comment of this field. To avoid GC to clean it up.
            s_KeyboardDelegate = KeyboardHookProc;
            //install hook
            s_KeyboardHookHandle = SetWindowsHookEx(
                WH_KEYBOARD_LL,
                s_KeyboardDelegate,
                Marshal.GetHINSTANCE(
                    Assembly.GetExecutingAssembly().GetModules()[0]),
                0);

            //If SetWindowsHookEx fails.
            if (s_KeyboardHookHandle == 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

                //Initializes and throws a new instance of the Win32Exception class with the specified error. 
                throw new Win32Exception(errorCode); // this line is causing this
            }
        }
    }

Unless I am mistaken, the culprit from the above code is:

s_KeyboardHookHandle = SetWindowsHookEx(
                WH_KEYBOARD_LL,
                s_KeyboardDelegate,
                Marshal.GetHINSTANCE(
                    Assembly.GetExecutingAssembly().GetModules()[0]),
                0);

So does anyone understand what the problem here is?

What have I tried?

I have tried previously re-creating the entire project and copy+pasting code in case any of the program's other files were corrupted (from copying to/from USB). I had believed the problem was fixed (though I might be mistaken). The problem is now back and what I previously thought would work is not working (re-creating the project).

I will try other solutions I find online, which relate to problems with Windows. If anyone can see anything wrong with the code, please let me know. Though the code came from a very reliable source and loads of people have used it so this seems unlikely.

https://social.msdn.microsoft.com/Forums/en-US/b7d1a35f-3759-4217-91ba-e4416ac19d78/how-do-you-fix-error-code-0x80004005?forum=jscript I tried the solution which involved "regsvr32 jscript.dll" and "regsvr32 vbscript.dll". It didn't work.

Community
  • 1
  • 1
Rehaan
  • 119
  • 1
  • 10

1 Answers1

-1

You exchange last two parameters of SetWindowsHookEx(): third parameter should be set to IntPtr.Zero for same process and last one is thread.

You can as well pinvoke the thread id:

[DllImport("kernel32.dll")]
public static extern int GetCurrentThreadId();

SetWindowsHookEx(WH_KEYBOARD_LL, s_KeyboardDelegate, IntPtr.Zero, GetCurrentThreadId());
Sinatr
  • 20,892
  • 15
  • 90
  • 319