如何获得IWebBrowser2的Internet Explorer_Server窗口句柄

ILscObject 2013-03-22 03:49:06
我们知道WebBrowser控件窗口是这样的:

--Shell Embedding
|-Shell DocObject View
|--Internet Explorer_Server

现在想获得Internet Explorer_Server窗口的句柄

原本想直接用IWebBrowser2的get_HWND方法来获取,结果失败并得到返回值0x80004005。

后来找到这篇文章http://support.microsoft.com/kb/244310,说是要通过IOleWindow来获取。

实验发现这种方法确实能得到Shell Embedding的窗口句柄,但随后试图通过GetWindow(hwnd, GW_CHILD)或者是FindWindowEx函数从Shell Embedding句柄来得到Internet Explorer_Server甚至是Shell DocObject View窗口句柄都会遭遇失败(返回值为NULL),更加令人匪夷所思的是用GetLastError得到的错误代码居然为0。使用管理员权限也无效

网上有说法说这可能是新版ie的保护模式搞的鬼(我现在正在使用IE9 + Win7 64位)。不知道有没有什么解决办法。
...全文
327 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
flying365 2014-12-12
  • 打赏
  • 举报
回复
本人使用Aceess2003编写的程序中的Webbrowser控件获取数据后保存到数据库,也出现不能获得Webbrowser句柄的问题 我的代码如下: Public Function GetBrowserWindow(窗体句柄 As Long) As Long Dim hwndPeer As Long hwndPeer = FindWindowEx(窗体句柄, 0, "OFormSub", vbNullString)‘根据窗体获取子窗体的句柄 hwndPeer = FindWindowEx(hwndPeer, 0, "Shell Embedding", vbNullString) hwndPeer = FindWindowEx(hwndPeer, 0, "Shell DocObject View", vbNullString) hwndPeer = FindWindowEx(hwndPeer, 0, "Internet Explorer_Server", vbNullString) GetBrowserWindow = hwndPeer End Function 在执行第二句hwndPeer = FindWindowEx(hwndPeer, 0, "Shell Embedding", vbNullString)时返回值是0,就是没有获得Shell Embedding句柄;但是上面的代码在VB6中就顺利获得的句柄(VB6中不需要这句:hwndPeer = FindWindowEx(窗体句柄, 0, "OFormSub", vbNullString)),令人百思不得其解。我根据楼主帖子中微软网站的代码,希望用枚举法获得句柄,也没有成功。 我不懂语言,谁能把二楼的代码用VB的写一下,不知能否解决我说的问题?
alexueQ 2013-10-17
  • 打赏
  • 举报
回复
我也刚写了一个,你用吧 #include <ShlGuid.h> HWND GetHwndFromIWebBrowser2(IWebBrowser2* pWebBrowser2) { if (pWebBrowser2 == NULL) return NULL; IServiceProvider* pServiceProvider = NULL; //1. --Shell Embedding if (SUCCEEDED(pWebBrowser2->QueryInterface( IID_IServiceProvider, (void**)&pServiceProvider))) { IOleWindow* pWindow = NULL; if (SUCCEEDED(pServiceProvider->QueryService( SID_SShellBrowser, IID_IOleWindow, (void**)&pWindow))) { HWND hwndBrowser = NULL; if (SUCCEEDED(pWindow->GetWindow(&hwndBrowser))) { //2.|-Shell DocObject View HWND hchildwnd = GetWindow(hwndBrowser, GW_CHILD); while (hchildwnd) { TCHAR wndname[MAX_PATH] = _T(""); GetClassName(hchildwnd, wndname, MAX_PATH); if ( wcscmp(wndname,_T("Shell DocObject View")) == 0 ) { //3.|--Internet Explorer_Server HWND hiewnd = GetWindow(hchildwnd, GW_CHILD); while (hiewnd) { TCHAR wndname[MAX_PATH] = _T(""); GetClassName(hiewnd, wndname, MAX_PATH); if ( wcscmp(wndname,_T("Internet Explorer_Server")) == 0 ) { return hiewnd; } hiewnd = GetNextWindow(hiewnd, GW_HWNDNEXT); } return hwndBrowser; } hchildwnd = GetNextWindow(hchildwnd, GW_HWNDNEXT); } return hwndBrowser; } pWindow->Release(); } pServiceProvider->Release(); } return NULL; }

3,245

社区成员

发帖
与我相关
我的任务
社区描述
ATL,Active Template Library活动(动态)模板库,是一种微软程序库,支持利用C++语言编写ASP代码以及其它ActiveX程序。
社区管理员
  • ATL/ActiveX/COM社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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