Python ctypes:SetWindowsHookEx回调函数从未调用 [英] Python ctypes: SetWindowsHookEx callback function never called

查看:1613
本文介绍了Python ctypes:SetWindowsHookEx回调函数从未调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在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.

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

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

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

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.

这个例子是从2009年(预Windows 7 ?),虽然我不知道这是否是这个问题。

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

所以,我的问题是,任何人都可以1.找出代码为什么工作,或者说我另一种方式来实现我在做的事情(最好在Python中,虽然我会去其他语言,如果需要的话)。

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).

编辑:有可能做我想要的WMI吗?我不知道WMI好,但我知道它确实有非常好的Python界面。

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.

推荐答案

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

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. 在C或C ++中编写一个实现挂钩过程的DLL,并通过某种形式的进程间通信来通知您的主程序。然后将该DLL加载到主程序中,并将其模块句柄和该DLL中实现的钩子过程传递给SetWindowsHookEx。

  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.

SetWinEventHook函数可能会给你什么想。 WinEvent钩子可以在你的主程序中实现。

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

这篇关于Python ctypes:SetWindowsHookEx回调函数从未调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆