Python ctypes:SetWindowsHookEx 回调函数从未被调用

发布于 2024-11-16 21:47:58 字数 839 浏览 5 评论 0原文

我正在尝试用 Python 编写一个程序,该程序可以识别何时显示警报框/对话框。它正在处理多个监视器,我希望它在任务栏图标闪烁、弹出错误/通知等时在辅助监视器上显示可视化效果。

据我所知,检测这些事件的方法是使用消息挂钩,如下所述: http://msdn.microsoft.com/en-us/library/ms632589%28v=vs.85%29.aspx

我什至很幸运地找到了一个访问 SetWindowsHookEx 的示例来自 Python 的函数。 (这个特定的示例使用鼠标信号,但我可以更改常量来监听不同的消息)。 http://www.python-forum.org/pythonforum /viewtopic.php?f=2&t=11154

但是,上面的示例不起作用。无论我如何单击鼠标,回调函数都不会被调用,并且鼠标中键单击不会导致程序退出。

该示例来自 2009 年(Windows 7 之前的版本?),尽管我不知道这是否是问题所在。

所以,我的问题是,任何人都可以1.找出代码工作的原因,或者2.告诉我另一种方法来实现我正在做的事情(最好是用Python,尽管如果需要的话我会使用其他语言)。

编辑:是否可以使用 WMI 执行我想要的操作?我不太了解WMI,但我知道它确实有很好的Python 接口。

I'm trying to write a program in Python that is aware of when alert boxes/dialogues are shown. It's dealing with multiple monitors, and I want it to display a visualization on the secondary monitor when a taskbar icon flashes, an error/notification pops up, etc.

As far as I can tell, the way to do detect these events is using message hooks, as described here: http://msdn.microsoft.com/en-us/library/ms632589%28v=vs.85%29.aspx

I was even lucky enough to find an example that accesses the SetWindowsHookEx function from Python. (This particular example uses mouse signals, but I can just change the constants to listen for different messages).
http://www.python-forum.org/pythonforum/viewtopic.php?f=2&t=11154

However, the above example does not work. The callback function is never called, regardless of my mouse clicks, and a middle mouse click does not cause the program to exit.

The example is from 2009 (pre windows 7?),though I don't know if that's the issue.

So, my question is, can anybody 1. find out why the code works, or 2. tell me another way to achieve what I'm doing (preferably in Python, though I'll go to other languages if necesarry).

Edit: Is it possible to do what I'm wanting with WMI? I don't know WMI well, but I know that it does have very good Python interface.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半衬遮猫 2024-11-23 21:47:58

除 WH_KEYBOARD_LL 和 WH_MOUSE_LL 之外,Windows 挂钩必须在 Windows 注入每个应用程序进程的 DLL 中实现。因此,您不能简单地在程序中实现回调并期望 Windows 运行它。

我可以想到两种方法来解决这个问题:

  1. 用 C 或 C++ 编写一个 DLL,它实现钩子过程并通过某种形式的进程间通信通知您的主程序。然后在主程序中加载该 DLL,并将其模块句柄和该 DLL 中实现的挂钩过程传递给 SetWindowsHookEx。

  2. SetWinEventHook函数可能会给你你想要的。 WinEvent hooks 可以在你的主程序中实现。

With the exception of WH_KEYBOARD_LL and WH_MOUSE_LL, the Windows hooks must be implemented in a DLL that Windows injects into each application process. Therefore, you can't simply implement a callback in your program and expect Windows to run it.

I can think of two ways to solve this problem:

  1. Write a DLL in C or C++ that implements the hook procedure and notifies your main program through some form of inter-process communication. Then load this DLL in your main program and pass its module handle and the hook procedure implemented in that DLL to SetWindowsHookEx.

  2. The SetWinEventHook function may give you what you want. WinEvent hooks can be implemented in your main program.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文