我如何判断给定的 hWnd 是否仍然有效?

发布于 2024-08-30 09:27:07 字数 192 浏览 2 评论 0原文

我正在使用生成 Internet Explorer 实例的第三方类。该类有一个属性 hWnd,它返回进程的 hWnd。

稍后,我可能想重用应用程序的实例(如果它仍然存在),因此我需要告诉我的帮助器类附加到它。在此之前,我想知道给定的 hWnd 是否仍然有效,否则我将生成另一个实例。

我怎样才能在 C# 中做到这一点? .NET 3.5?

I'm using a third-party class that spawns an instance of Internet Explorer. This class has a property, hWnd, that returns the hWnd of the process.

Later on down the line, I may want to reuse the instance of the application if it still exists, so I need to tell my helper class to attach to it. Prior to doing that, I'd like to know if the given hWnd is still valid, otherwise I'll spawn another instance.

How can I do this in C# & .NET 3.5?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

神也荒唐 2024-09-06 09:27:07

如果是窗口句柄,可以调用 isWindow(hWnd);

来自 msdn:

返回值

布尔

如果窗口句柄标识
现有窗口,返回值为
非零。

如果窗口句柄无法识别
现有窗口,返回值
为零。备注

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

顺便说一句,由于您使用的是 .NET,因此您必须执行以下操作:

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool IsWindow(IntPtr hWnd);

If it is a window handle, you can call isWindow(hWnd);

From msdn:

Return Value

BOOL

If the window handle identifies an
existing window, the return value is
nonzero.

If the window handle does not identify
an existing window, the return value
is zero. Remarks

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.

By the way since you are in .NET you'll have to do something like:

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool IsWindow(IntPtr hWnd);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文