处理无效的窗口句柄 [英] Handling invalid window handle

查看:731
本文介绍了处理无效的窗口句柄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应用程序检索窗口使用Enum *例程进行处理.

The application retrieve window handles, using Enum* routines.

碰巧,在应用程序管理枚举/创建的窗口的句柄(获取类名,窗口统计信息...)的同时,该句柄不再有效.代码管理窗口句柄使用try/catch块进行保护,但是该窗口句柄已存储,并随后用于管理所表示的窗口.

It happens that in the while the application manage the handle (get class name, window statistics...) of an enumerated/created window, the handle is no more valid. The code managing window handles are protected using a try/catch block, but the window handle is stored and the successively used for managing the represented window.

如何处理窗口句柄的生存期?是否可以检测到句柄无效?

How to handle the window handle lifetime? It is possible to detect the handle invalidity?

我希望每次应用程序使用窗口句柄时都避免使用try/catch块.

I'd like to avoid try/catch blocks every time the application uses the window handles.

推荐答案

我已经有了实际的解决方案...但是直到现在我还不知道这一点!

I already have the actual solution... but I didn't know about this until now!

谢谢大家澄清了窗口句柄的寿命,但实际上有一种方法可以检测到窗口句柄的寿命:

Thank you all for having clarified about the window handle lifetime, but there is actually a method for being detected about window handle lifetime: CbtProc.

如果挂钩是在系统范围内安装的,则有可能将某个窗口破坏通知给特定的应用程序(这完全取决于CBT挂钩的实际实现),这表明特定的句柄将在窗口终止后失效.通知.

In the case the hook is installed system wide, it's possible to notify specific applications (it depends all on real implementation of the CBT hook) about a window destroy, which indicates that the a specific handle won't be valid after the notification.

从文档中:

HCBT_DESTROYWND指定要销毁的窗口的句柄.

HCBT_DESTROYWND Specifies the handle to the window about to be destroyed.

当然,使用WINAPI例程对句柄的访问必须与通知系统同步,这似乎不太可行(CBT钩子实际上阻止了窗口销毁,因为它与应用程序逻辑同步了.)

Of course the access of the handles using WINAPI routines must be synchronized with the notification system, which doesn't seem to give good feasibility (CBT hook actually blocks window destroy because it is synchronized with the application logic).

这篇关于处理无效的窗口句柄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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