Win32:获取其他应用程序关闭/退出的消息通知 [英] Win32: Get message notification of other application's close/exit
问题描述
我的应用程序需要监控系统上所有其他正在运行的应用程序.有什么方法可以让我在每个应用程序 exe 退出时得到通知?
My application needs to monitor all other running applications on the system. Is there some way I could get notified on exit of every application exe?
我能找到的方法:
1) 使用 PSAPI 函数获取频繁运行的 exe 列表.在每次轮询时与之前的列表进行比较,以找出退出的应用程序/进程.缺点:需要不断轮询,会占用 CPU 时间.
1) Use PSAPI functions to get the list of running exes at frequent intervals. At each poll compare with the previous list to find which application/process has exited. Disadvantage: Requires constant polling, will take CPU time.
2) 为 WM_CLOSE 消息设置一个全局钩子:使用这个我可以在任何应用程序通过标题栏上的关闭按钮关闭时收到通知
2) Set a global hook for WM_CLOSE message: Using this I would be able to get a notification when any application gets closed through the close button on the title bar
缺点:(-)并非所有应用程序都生成 WM_CLOSE 消息(例如:Total Video Player Exe)(-)如果应用程序是通过退出"菜单或按钮(例如文件->退出)关闭的,我无法捕获该消息
Disadvantage: (-)Not all the applications are generating a WM_CLOSE message(Ex: Total Video Player Exe) (-)If the application was closed through the "Exit" menu or button (e.g. File->Exit) , I can't trap that message
我错过了其他更好的方法吗?请指教.
Is there any other better way that I missed? Please advise.
推荐答案
- 使用 PSAPI 获取 PID 列表.
- 然后使用 OpenProcess() 处理每个进程.
- 使用 WaitForMultipleObjects() 发出信号当其中一个进程退出时.
- Get a list of PIDs using PSAPI.
- Then get a handle on each process using OpenProcess().
- Use WaitForMultipleObjects() to be signalled when one of the processes exits.
这篇关于Win32:获取其他应用程序关闭/退出的消息通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!