处理无效的窗口句柄
应用程序使用 Enum* 例程检索窗口句柄。
碰巧的是,当应用程序管理枚举/创建的窗口的句柄(获取类名、窗口统计信息...)时,该句柄不再有效。管理窗口句柄的代码使用 try/catch 块进行保护,但窗口句柄会被存储并连续用于管理所表示的窗口。
如何处理窗口句柄的生命周期?是否可以检测句柄无效?
我想避免每次应用程序使用窗口句柄时都出现 try/catch 块。
The application retrieve window handles, using Enum* routines.
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?
I'd like to avoid try/catch blocks every time the application uses the window handles.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
仅当从创建窗口的线程中使用窗口句柄时,窗口句柄才是安全的。从任何其他线程,您只能知道窗口句柄在过去的某个时间是有效的。现在,它可能是也可能不是,如果是,它可能指的是一个与预期完全不同的窗口。
Window handles are only safe if used from the thread that created the window. From any other thread, all you can know about a window handle is, it was valid sometime in the past. right now, it may or may not be, and if it is, it could refer to a different window than intended entirely.
我已经有了实际的解决方案...但直到现在我才知道!
感谢大家澄清窗口句柄生存期,但实际上有一种方法可以检测窗口句柄生存期: CbtProc。
如果钩子安装在系统范围内,则可以通知特定应用程序(这完全取决于 CBT 钩子的实际实现)有关窗口销毁的信息,这表明特定句柄在通知后将无效。
从文档中:
当然,使用WINAPI例程对句柄的访问必须与通知系统同步,这似乎没有提供很好的可行性(CBT钩子实际上阻止了窗口销毁,因为它与应用程序逻辑同步)。
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.
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.
From the documentation:
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).
您可以将其传递给
IsWindow()< /code>
来验证它。
有一些注意事项,但是两者都适用于几乎任何方法:
如果您对自己的外部应用程序之一的窗口执行此操作,则可以通过 Set/GetProp()'ing 某种唯一标识符来添加第二层验证。
You can pass it to
IsWindow()
to validate it.There are a couple of caveats, however both will apply to pretty much any approach to this:
If your doing this to a window in one of your own external apps, you could add a 2nd tier of validation by Set/GetProp()'ing a unique identifier of some kind.
您可以使用
GetWindowInfo
函数。如果句柄无效,则返回 0。You can use the
GetWindowInfo
function. It returns 0 if the handle is not valid.