gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 10:35:48 38 4
gpt4 key购买 nike

背景:

我正在创建一个 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/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com