windows - 无法获取 Windows Internet Explorer 主窗口的句柄

标签 windows internet-explorer visual-c++ com window-handles

背景:

我正在创建一个 IWebBrowser2 并导航到一个 URL。这是代码的简短版本:

IWebBrowser2* pWebBrowser = NULL;
hr = ::CoCreateInstance(CLSID_InternetExplorer, NULL, CLSCTX_LOCAL_SERVER, IID_IWebBrowser2, (void **)&pWebBrowser);
hr = pWebBrowser->Navigate(bstrURL, &varFlags, &varEmptyStr, &varEmptyStr, &varEmptyStr);
hr = pWebBrowser->put_Visible(VARIANT_TRUE);
// Get the handle of the IE window 
HWND handle = NULL;
hr = pWebBrowser->get_HWND((long*)&handle); // this could be done better with reinterpret_cast
…

问题:

有时,对 get_HWND 的调用会失败并返回 0x80004005 错误代码。它仅在某些工作站上失败。

问题:

  1. 为什么调用 get_HWND 随机失败?
  2. get_HWND 的使用是否正确?预期类型为 SHANDLE_PTR。

一些可能有用的链接:

http://msdn.microsoft.com/en-us/library/aa752126%28v=vs.85%29.aspx http://codecentrix.blogspot.com/2007/11/when-iwebbrowser2gethwnd-returns-efail.html

需要更多信息?请告诉我

谢谢!

最佳答案

get_HWND 的调用很好,它随机失败,因为实例被杀死。为什么?

使用 CLSID_InternetExplorer 调用 CoCreateInstance 将实例化一个低完整性级别的进程。如果您导航到的 URL 绑定(bind)到低完整性级别(对于 Internet 站点为真),则不会有问题。另一方面,如果您导航到绑定(bind)到中等完整性级别的 URL(对于 Intranet/受信任的站点为真),那么您的进程将被终止,并且将创建一个具有中等完整性级别的新进程(在 IE8 及更高版本中是无缝的) ).此操作称为“虚拟 Tab 开关”。

有关问题及其解决方法的更多信息:

http://blogs.msdn.com/b/ieinternals/archive/2011/08/03/internet-explorer-automation-protected-mode-lcie-default-integrity-level-medium.aspx

http://msdn.microsoft.com/en-us/library/aa752084%28v=vs.85%29.aspx

Windows Internet Explorer 8. On Windows Vista, to create an instance of Internet Explorer running at a medium integrity level, pass CLSID_InternetExplorerMedium (defined in exdisp.idl) to CoCreateInstance. The resulting InternetExplorerMedium object supports the same events, methods, and properties as the InternetExplorer object.

您可能想验证这确实是您面临的问题。以下是如何运行快速测试:

  1. 打开IE
  2. 转到设置
  3. 安全标签
  4. 本地内联网
  5. 点击“网站”按钮
  6. 取消选中“自动检测 Intranet 网络”

那应该抑制虚拟标签开关

Test IE integrity level

这是有关保护模式的附加信息(摘自 Matthew Crowley 的 Pro Internet Explorer 8 & 9 Development:Developing Powerful Applications for the Next Generation of IE 一书):

Windows Vista introduced the concept of MICs, which use integrity levels (ILs) to differentiate filesystem objects, registry locations, and APIs by trust and privilege levels and User Account Control (UAC) to funnel elevation requests to users. This architecture helps to protect system and user files against malicious access by untrusted applications.

The integrity levels present on Windows Vista and higher fall into four major categories:

System: Core operating system (NTAUTHORITY); system components, files, and data

High: Machine-level access (administrators); program files and local machine registry hive

Medium: User-level access (users); user files and settings and current user registry hive

Low: Untrusted content, temporary files, and data

关于windows - 无法获取 Windows Internet Explorer 主窗口的句柄,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27558028/

相关文章:

linux - D可以编译成平台独立代码吗?

c++ - CMake:在 Windows 上找不到编译器

IE 的 CSS 定位问题

internet-explorer - 在 IE 和 Webkit 中按预期解释 Accept Headers

c++ - 错误表达

c++ - 如何生成唯一的文件名或路径?

PHP 环境变量

javascript - IE9 中远程文件的 window.print() 问题

c++ - 不能在函数声明中使用模板参数

c++ - 使用 openCV VC++ 读取文件 I/O