如何确定句柄是否有效

liuyun1983 2009-06-14 08:57:02
我使用FindWindowEx得到另外几个窗口的HWND,然后使用HWND获取一些信息。

但是目前有个问题,就是使用HWND获取信息是在获取HWND之后的一段时间内进行的,窗口有可能关闭了,这样程序就出错了。

我想知道怎样根据HWND来判断该窗口是否关闭。

我不希望每次使用HWND获取信息都要重新使用FindWindowEx来进行,可以吗?
...全文
547 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
Eleven 2009-06-15
  • 打赏
  • 举报
回复
ASSERT_VALID()
用户 昵称 2009-06-15
  • 打赏
  • 举报
回复
每次使用之前用iswindow判断。
maple_zhj 2009-06-15
  • 打赏
  • 举报
回复
BOOL IsWindow(HWND hWnd);
不过也不是很有效。。。
maple_zhj 2009-06-15
  • 打赏
  • 举报
回复
BOOL IsWindow(HWND hWnd);
不过也不是很有效。。。
woneedjob 2009-06-15
  • 打赏
  • 举报
回复
UP
oyljerry 2009-06-14
  • 打赏
  • 举报
回复
如果是自己的程序,那就创建窗口的时候保存对应的窗口句柄,这样就不用查找了
码侬 2009-06-14
  • 打赏
  • 举报
回复
顶,学习
liuyun1983 2009-06-14
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 zwfgdlc 的回复:]
理论上说有很多种方法。像fishion说的GetWindowText(),还有
C/C++ codeHDC GetWindowDC(
HWND hWnd//handle to window);

BOOL GetWindowRect( HWND hWnd,
LPRECT lpRect
);
等等,不止这些。如果执行函数失败,马上用GetLastError()判断错误代码是不是等于ERROR_INVALID_WINDOW_HANDLE
,如果是就里窗口句柄无效了。
[/Quote]

我的程序是获取句柄以后,然后得到个内存地址,对内存地址中存放的内容进行读取的,这样执行函数失败程序就出错了。而读内存这个步骤是长时间执行的,而如果我在每次读内存之前使用FindWindow的话,又有别的问题:我想要读取内存的窗口开了非常多个。如果检查的话挺麻烦的。
zwfgdlc 2009-06-14
  • 打赏
  • 举报
回复
理论上说有很多种方法。像fishion说的GetWindowText(),还有

HDC GetWindowDC(
HWND hWnd // handle to window
);

BOOL GetWindowRect( HWND hWnd,
LPRECT lpRect
);

等等,不止这些。如果执行函数失败,马上用GetLastError()判断错误代码是不是等于ERROR_INVALID_WINDOW_HANDLE
,如果是就里窗口句柄无效了。
liuyun1983 2009-06-14
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 fishion 的回复:]
引用 2 楼 zwfgdlc 的回复:


C/C++ code
BOOL IsWindow(HWND hWnd);

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.

所以最好还是在使用时再用FindWindow,或者如果是有标题的窗口,用之前就用GetWindowText得到…
[/Quote]

这种情况发生的可能性有多大?如果概率很低的话,我就省点事就用IsWindow(HWND hWnd)了
IORI915189 2009-06-14
  • 打赏
  • 举报
回复
ISWINDOW 可以判断 句柄 是否是窗口 也可以说 句柄当前是否还有效

程序会出错?
用API 就算句柄失效 也只是返回0 操作没有成功 加个判断就可以了
fishion 2009-06-14
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zwfgdlc 的回复:]


C/C++ code
BOOL IsWindow(HWND hWnd);
[/Quote]
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.

所以最好还是在使用时再用FindWindow,或者如果是有标题的窗口,用之前就用GetWindowText得到标题来判断是否为你要的窗口
oysoft 2009-06-14
  • 打赏
  • 举报
回复
ASSERT(hWnd&&IsWindow(hWnd));
zwfgdlc 2009-06-14
  • 打赏
  • 举报
回复


BOOL IsWindow(HWND hWnd);
  • 打赏
  • 举报
回复
使用之前查找呗,效率也不会很低的。
作者: 三角猫 出处: http://www.zu14.cn/ 版权归 三角猫 和 真有意思网 所有,转载请注明出处 using System; using System.Collections.Generic; using System.Text; namespace QQAutoMsg { /// /// 消息发送 /// internal static class QQMsgSender { /// /// 发送消息 /// /// 所以已打开的QQ窗体的列表 /// 消息内容 internal static void Go(List qqChatWindows, string msg) { foreach (EnumQQChatWindows.QQChatWindow win in qqChatWindows) { SendMsg(win.WindowHwnd, msg); } } /// /// 根据窗体句柄,找到输入框和发送按钮,发送消息出去 /// /// 聊天窗口句柄 /// 消息内容 private static void SendMsg(IntPtr hWnd, string msg) { if (NativeMethods.IsWindow(hWnd)) //确认该聊天窗口仍然有效 { ////找到 发送 按钮 IntPtr hwndButton = NativeMethods.FindWindowEx(hWnd, IntPtr.Zero, "Button", "发送(S)"); if (IntPtr.Zero != hwndButton) { ////找到窗体顺序上的第一个RichEdit20A控件,其实就是消息显示框 IntPtr hwndRichEdit = NativeMethods.FindWindowEx(hWnd, IntPtr.Zero, "RichEdit20A", null); ////利用spy++,可以看到消息输入框的父窗体是类名为 AfxWnd42 的控件 ////在顺序上是显示框的下一个窗体 if (IntPtr.Zero != hwndRichEdit) { ////找到 AfxWnd42 这个窗体 hwndRichEdit = NativeMethods.GetWindow(hwndRichEdit, NativeMethods.GW_HWNDNEXT); if (IntPtr.Zero != hwndRichEdit) { ////这才是真正的消息输入框 hwndRichEdit = NativeMethods.FindWindowEx(hwndRichEdit, IntPtr.Zero, "RichEdit20A", null); if (hwndRichEdit != IntPtr.Zero) { ////发送消息,因为QQ屏蔽了 WM_SETTEXT, WM_PASTE 命令,所有采用 EM_REPLACESEL 来实现 NativeMethods.SendMessage(hwndRichEdit, NativeMethods.EM_REPLACESEL, IntPtr.Zero, msg); ////给发送按钮发 鼠标单击消息 NativeMethods.SendMessage(hwndButton, NativeMethods.BM_CLICK, IntPtr.Zero, IntPtr.Zero); } } } } } } } }

15,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧