我怎样才能知道,如果给定的hWnd是仍然有效? [英] How can I tell if a given hWnd is still valid?

查看:147
本文介绍了我怎样才能知道,如果给定的hWnd是仍然有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用一个第三方类派生的Internet Explorer的一个实例。这个类有一个属性,的hWnd,返回过程中的hWnd。

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.

后来就下了线,我可能想,如果它重用应用程序实例仍存在,所以我要告诉我的助手类附加到它。在此之前这样做,我想知道,如果给定的hWnd仍然是有效的,否则我会产生另一个实例。

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.

我怎样才能做到这在C#和放大器; ?.NET 3.5

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

推荐答案

如果它是一个窗口句柄,可以调用的 isWindow(HWND);

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

从MSDN:

返回值

BOOL

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

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

如果窗口句柄不标识
现有的窗口,则返回值
为0。备注

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

一个线程不应该为它没有创造,因为
窗口可以
在此之后被摧毁一个
窗口中使用IsWindow函数被调用。此外,
,因为窗口句柄被回收
手柄甚至可能指向
不同的窗口。

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.

顺便说一句,因为你是在.NET中你必须做一些事情,如:

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

这篇关于我怎样才能知道,如果给定的hWnd是仍然有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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