Module not found
Asked Answered
C

3

5

I've been working on this one quite a bit and haven't gotten any closer to a solution.

I juut dug up my old copy of the WindowsHookLib again - It's available with source at http://www.codeproject.com/KB/DLL/WindowsHookLib.aspx. This library allows Global Windows Mouse/Keyboard/Clipboard Hooks, which is very useful.

I'm trying to use the Mouse Hook in here to Capture Mouse-Motion (I could use a Timer that always polls Cursor.Position, but I plan on using more features of WindowsHookLib later).

Code as follows:

MouseHook mh = new MouseHook();
mh.InstallHook();
mh.MouseMove += new EventHandler<WindowsHookLib.MouseEventArgs>(mh_MouseMove);

But on the call to InstallHook(), I get an Exception: "The specified Module could not be found". Strange. Searching, I found that someone thought this occurs because a DLL is not in a place included in the Windows PATH variable, and because placing it in system32 didn't help I went the whole hog and translated the thing to C# for inclusion directly in my project (I was curious how it works).

However the error was obstinately persistent, so I dug a bit on this, and found the Code in the Library that is responsible: In InstallHook(), we have

IntPtr hinstDLL = Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]);
this._hMouseHook = UnsafeNativeMethods.SetWindowsHookEx(14, this._mouseProc, hinstDLL, 0);
if (this._hMouseHook == IntPtr.Zero)
{
      throw new MouseHookException(new Win32Exception(Marshal.GetLastWin32Error()).Message);
}

And this (after modification and recompile) tells me that what I'm really getting is a Windows error "ERROR_MOD_NOT_FOUND"! Now, Here I'm stumped. Didn't I just compile the Hook Library directly into my project?

(UnsafeMethods.SetWindowsHookEx is just a DllImported Method from user32)

Any Answers, or Prods in the right direction, or any hints, pointers or similar are very much appreciated!

Charlotte answered 5/5, 2010 at 15:46 Comment(0)
S
13

I found when migrating to .NET 4.0 I had to send in IntPtr.Zero for the hMod parameter when the Hook Procedure was in the local assembly. You can refer to the msdn documentation here.

http://msdn.microsoft.com/en-us/library/ms644990%28VS.85%29.aspx

Systematics answered 18/11, 2010 at 18:3 Comment(2)
This worked for me as well. And, to think, I found this question by accident just minutes before testing my code. :)Herbarium
It may have been helpful to include some code here. As it stands, it's a little ambiguous.Intuitivism
S
1

I am also having this problem. I found that it seems to be to do with the version of .Net you are using. .Net 4 you get this error, change to .Net 3.5 and it works.

Sagacious answered 14/6, 2010 at 13:18 Comment(0)
I
0

Expanding on @MichaelWohltman's answer above (no code) here's an exmple of its implementation for Kaeyboard hook:

/// <summary>
/// Installs the keyboard hook for this application. 
/// </summary>
public void InstallHook()
{
    if (this._hKeyboardHook == IntPtr.Zero)
    {
        this._keyboardProc = new KeyboardHook.KeyboardMessageEventHandler(KeyboardProc);
        IntPtr hinstDLL = Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]);
        this._hKeyboardHook = UnsafeNativeMethods.SetWindowsHookEx(UnsafeNativeMethods.WH_KEYBOARD_LL, this._keyboardProc, IntPtr.Zero, 0);
        if (this._hKeyboardHook == IntPtr.Zero)
        {
            // Failed to hook. Throw a HookException
            throw new Exception("That fucking exception!");
            //int eCode = Marshal.GetLastWin32Error();
            //this._keyboardProc = null;
            //throw new WindowsHookException(new Win32Exception(eCode).Message);
        }
        else
        {
            this.OnStateChanged(new WindowsHookLib.StateChangedEventArgs(this.State));
        }
    }
}

The line of interest is:

this._hKeyboardHook = UnsafeNativeMethods.SetWindowsHookEx(UnsafeNativeMethods.WH_KEYBOARD_LL, this._keyboardProc, IntPtr.Zero, 0);

note the ItrPtr.Zero replaces hinstDLL

Interestingly, I was having problems with a Framework 3.5 build. It was rather complicated as it was a plugin/class library for another application, but this replacement seemed to solve the problem.

Intuitivism answered 21/8, 2022 at 12:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.