我现在想实现一个功能就是:在我我程序窗口不是当前窗口时(即比如:处于最小化时)按键盘上某特定的键,程序作出响应。 我想到了全局钩子,于是我在网上找了好多资料,结果都不行!! 问题集中在如下代码: //hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProcedure, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0);或
IntPtr pInstance = Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().ManifestModule); hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProcedure, pInstance, 0); 判断 hKeyboardHook 横等于0!!!!即钩子安装失败!! 源代码在:http://topic.csdn.net/u/20090107/16/3231833d-92d6-4961-8fbd-1aaf7200b9b1.html我自己用VS2008 写的代码在:http://files.cnblogs.com/lkf18/lkfHook1.rar
--------------------------------------------------------------------------------
IntPtr pInstance = Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().ManifestModule); hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProcedure, pInstance, 0); 判断 hKeyboardHook 横等于0!!!!即钩子安装失败!! 源代码在:http://topic.csdn.net/u/20090107/16/3231833d-92d6-4961-8fbd-1aaf7200b9b1.html我自己用VS2008 写的代码在:http://files.cnblogs.com/lkf18/lkfHook1.rar
--------------------------------------------------------------------------------
看看去
IntPtr pInstance = GetModuleHandle(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
#else
IntPtr pInStance =Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().ManifestModule);
#endif在调试环境下:
因为有一个xxx.vshost.exe作祟Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().ManifestModule)取得的 HINSTANCE是不正确的.
[DllImport("kernel32.dll")]
public extern static IntPtr GetModuleHandle(string lpModuleName);