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