处理无效的窗口句柄

发布于 2024-09-12 14:39:47 字数 214 浏览 12 评论 0原文

应用程序使用 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 技术交流群。

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

发布评论

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

评论(4

谁许谁一生繁华 2024-09-19 14:39:47

仅当从创建窗口的线程中使用窗口句柄时,窗口句柄才是安全的。从任何其他线程,您只能知道窗口句柄在过去的某个时间是有效的。现在,它可能是也可能不是,如果是,它可能指的是一个与预期完全不同的窗口。

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.

栩栩如生 2024-09-19 14:39:47

我已经有了实际的解决方案...但直到现在我才知道!

感谢大家澄清窗口句柄生存期,但实际上有一种方法可以检测窗口句柄生存期: CbtProc

如果钩子安装在系统范围内,则可以通知特定应用程序(这完全取决于 CBT 钩子的实际实现)有关窗口销毁的信息,这表明特定句柄在通知后将无效。

从文档中:

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

当然,使用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:

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

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

陪你搞怪i 2024-09-19 14:39:47

您可以将其传递给 IsWindow()< /code>来验证它。
有一些注意事项,但是两者都适用于几乎任何方法:

线程不应该使用 IsWindow
它没有创建的窗口,因为
窗户可能会被摧毁后
这个函数被调用了。更远,
因为窗把手是回收的
句柄甚至可以指向
不同的窗口。

如果您对自己的外部应用程序之一的窗口执行此操作,则可以通过 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:

A thread should not use IsWindow for a
window that it did not create because
the window could be destroyed after
this function was called. Further,
because window handles are recycled
the handle could even point to a
different window.

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.

羞稚 2024-09-19 14:39:47

您可以使用 GetWindowInfo 函数。如果句柄无效,则返回 0。

You can use the GetWindowInfo function. It returns 0 if the handle is not valid.

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