我尝试通过下面的代码钩入启动button。
// Create an instance of HookProc. StartHookProcedure = new CallBack(StartHookProc); IntPtr desktop = FindWindowEx( IntPtr.Zero, IntPtr.Zero, "Progman", null); uint procId = 0; uint threadId = GetWindowThreadProcessId(desktop, out procId); Process process = Process.GetCurrentProcess(); ProcessModule module = process.MainModule; IntPtr hModule = GetModuleHandle(null); // get my module handle... //IntPtr start = FindWindowEx(desktop, IntPtr.Zero, "Button", null); hHook = SetWindowsHookEx(WH_GETMESSAGE, StartHookProcedure, hModule, (int)threadId); //If the SetWindowsHookEx function fails. if (hHook == 0) { int err = Marshal.GetLastWin32Error(); MessageBox.Show("SetWindowsHookEx Failed" + err.ToString()); return; }
SetWindowHookEx总是运行到错误1428 ERROR_HOOK_NEEDS_HMOD
没有模块句柄,不能设置非本地钩子。
我无法理解这一点。 虽然我调用GetModuleHandle(null)
但它失败。