Tag: setwindowshookex

在Windows XP中使用SetWindowsHookEx时出错,但在Windows 7中没有

我开发了一个使用全局键盘/鼠标钩子的应用程序。 它在Windows 7中运行良好,但在Windows XP中运行不佳。 当我在Windows XP中调用SetWindowsHookEx时,收到错误代码1428 int MouseLowLevel = 14 int code = SetWindowsHookEx(MouseLowLevel, MouseHookProc, IntPtr.Zero, 0); private IntPtr MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam) {}

如何挂钩应用程序?

我正试图在我的C#应用​​程序中挂钩创建一个窗口。 static IntPtr hhook = IntPtr.Zero; static NativeMethods.HookProc hhookProc; static void Main(string[] args) { // Dummy.exe is a form with a button that opens a MessageBox when clicking on it. Process dummy = Process.Start(@”Dummy.exe”); try { hhookProc = new NativeMethods.HookProc(Hook); IntPtr hwndMod = NativeMethods.GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName); hhook = NativeMethods.SetWindowsHookEx(HookType.WH_CBT, hhookProc, hwndMod, (uint)AppDomain.GetCurrentThreadId()); Console.WriteLine(“hhook valid? {0}”, hhook != […]

找不到模块

我一直在研究这个问题并且没有更接近解决方案。 我再次挖出了我的旧版WindowsHookLib – 它可以在http://www.codeproject.com/KB/DLL/WindowsHookLib.aspx上获得 。 该库允许全局Windows鼠标/键盘/剪贴板挂钩,这是非常有用的。 我正在尝试使用此处的Mouse Hook捕获Mouse-Motion(我可以使用一个总是轮询Cursor.Position的Timer,但我打算稍后使用WindowsHookLib的更多function)。 代码如下: MouseHook mh = new MouseHook(); mh.InstallHook(); mh.MouseMove += new EventHandler(mh_MouseMove); 但是在调用InstallHook()时,我得到一个exception:“无法找到指定的模块”。 奇怪。 搜索,我发现有人认为这是因为DLL不在Windows PATH变量中包含的地方,并且因为将它放在system32中并没有帮助我将整个程序转换为C#以直接包含在我的项目(我很好奇它是如何工作的)。 但是这个错误是顽固不变的,所以我对此进行了一些挖掘,发现了负责的库中的代码:在InstallHook()中,我们有 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); } 而这(在修改和重新编译之后)告诉我,我真正得到的是Windows错误“ERROR_MOD_NOT_FOUND”! 现在,我在这里难过。 我不是直接将Hook Library编译成我的项目吗? (UnsafeMethods.SetWindowsHookEx只是来自user32的DllImported方法) 任何答案,或正确方向的Prods,或任何提示,指针或类似的非常感谢!

c#中的窗口挂钩

我试图从csharp连接到其他窗口。 我使用SetWindowsHookEx ,但没有运气转换它fom c ++ tc#。 我在这里找到了这个post,但它没有解决。 问题是SetWindowsHookEx返回0.它包括我发现的最佳代码samle: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowDrawer { public partial class Form1 : Form { private delegate int HookProc(int code, IntPtr wParam, IntPtr lParam); static IntPtr hHook; IntPtr windowHandle; uint processHandle; HookProc PaintHookProcedure; [System.Runtime.InteropServices.DllImport(“user32.dll”, EntryPoint = […]

SetWindowsHookEx失败,错误126

我正在尝试在项目中使用Gma.UserActivityMonitor库,我遇到了一个我自己无法克服的错误。 在HookManager.Callbacks.cs文件中有一个静态方法,名为EnsureSubscribedToGlobalMouseEvents带有以下代码(或多或少): var asm = Assembly.GetExecutingAssembly().GetModules()[0]; var mar = Marshal.GetHINSTANCE(asm); s_MouseHookHandle = SetWindowsHookEx( WH_MOUSE_LL, s_MouseDelegate, mar, 0); //If SetWindowsHookEx fails. if (s_MouseHookHandle == 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 //Initializes and throws a […]

什么可以导致Windows取消挂钩低级别(全局)键盘钩子?

我们通过SetWindowsHookEx和WH_KEYBOARD_LL安装了一些全局键盘钩子,这些钩子看起来随机被Windows WH_KEYBOARD_LL 。 我们validation了它们不再附加钩子,因为在句柄上调用UnhookWindowsHookEx返回false 。 (还validation了它正常工作时返回true ) 似乎没有一致的repro,我听说他们可以因为超时或exception被抛出而脱钩,但我已经尝试了两个只是让它在处理方法中的断点超过一分钟,以及抛出一个随机exception(C#),它似乎仍然有效。 在我们的回调中,我们很快发布到另一个线程,因此可能不是问题。 我已经阅读了Windows 7中的解决方案,用于在注册表中设置更高的超时,因为Windows 7显然更加积极地执行超时(我们都在这里运行Win7,所以不确定这是否发生在其他操作系统上),但这并不是看起来似乎是一个理想的解决方案。 我考虑过只运行一个后台线程来每隔一段时间刷新一次钩子,这很黑,但我不知道这样做有什么真正的负面影响,而且它似乎比改变全局Windows注册表设置更好。 还有其他建议或解决方案? 设置钩子的类和它们所附加的代理都是静态的,所以它们不应该是GC。 编辑:通过调用GC.Collect();validationGC.Collect(); 他们仍然工作,所以他们没有被收集。

C#中的SetWindowsHookEx

我正试图挂钩第三方应用程序,以便我可以绘制到它的屏幕。 绘制到屏幕很容易,我不需要帮助,但我似乎遇到使用SetWindowsHookEx来处理WH_GETMESSAGE的问题。 我无法弄清楚最后2个参数要传递什么。 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowDrawer { public partial class Form1 : Form { private delegate int HookProc(int code, IntPtr wParam, IntPtr lParam); static IntPtr hHook; IntPtr windowHandle; uint processHandle; HookProc PaintHookProcedure; [System.Runtime.InteropServices.DllImport(“user32.dll”, EntryPoint = “FindWindow”, SetLastError = true)] static […]