0

I'm using Global Hooks in C# with OS Windows 10 PRO 64 BITS, Visual Studio 2010 and NetFramework 4. I am getting the follow error: Win32Exception was unhandled by user code The specified module could not be found.

I got the hook codes from this link: Processing Global Mouse and Keyboard Hooks in C#

The example works fine with Net Framework 2.0, but I imported the User Activity Hooks class into my project with NetFramework 4 and I test execute, but I got the error discussed above. I tried running with Net Framework 2 in my project and it doesn't work either.

This is code and I get the error in the throw:

public void Start(bool InstallMouseHook, bool InstallKeyboardHook)
    {
        // install Mouse hook only if it is not installed and must be installed
        if (hMouseHook == 0 && InstallMouseHook)
        {
            // Create an instance of HookProc.
            MouseHookProcedure = new HookProc(MouseHookProc);
            //install hook
            hMouseHook = SetWindowsHookEx(
                WH_MOUSE_LL,
                MouseHookProcedure,
                Marshal.GetHINSTANCE(
                    Assembly.GetExecutingAssembly().GetModules()[0]),
                0);
            //If SetWindowsHookEx fails.
            if (hMouseHook == 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(true, false, false);
                //Initializes and throws a new instance of the Win32Exception class with the specified error.
                throw new Win32Exception(errorCode);//Error
            }
        }

Why am I getting this error? Could you help me with this?

Thanks Erick

Cronoss
  • 1
  • 1
  • The message for that exception is pretty clear. You've referenced a module that wasn't found at runtime when your code tried to load it. See duplicate. – Peter Duniho Oct 14 '20 at 04:52
  • For what it's worth, if this does only work with .NET 2.0 (which I strongly doubt), you can run it against .NET 3.5.1. That version is supported and is effectively the last version of the 2.0 runtime. – Flydog57 Oct 14 '20 at 04:59
  • I already tried it too and I get the same result. @Flydog57 – Cronoss Oct 14 '20 at 14:37
  • Use the SysInternals *Process Monitor* tool to figure out what file (/module) it's looking for and where it's looking. – Flydog57 Oct 14 '20 at 14:43

0 Answers0