Background:
I am creating a IWebBrowser2 and navigating to a URL. This is the short version of the code:
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
…
Problem:
Sometimes, the call to get_HWND fails with the 0x80004005 error code. It is failing only on some workstations.
Questions:
Some links that might be useful:
http://msdn.microsoft.com/en-us/library/aa752126%28v=vs.85%29.aspx http://codecentrix.blogspot.com/2007/11/when-iwebbrowser2gethwnd-returns-efail.html
Need additional information? Just let me know
Thank you!
The call to get_HWND is just fine and it randomly fails as the instance being killed. Why?
The call to CoCreateInstance with CLSID_InternetExplorer will instantiate a process with low integrity level. If the URL you navigate to is bound to a low integrity level (true for internet sites) there will be no problem. On the other hand, if you navigate to a URL that is bound to medium integrity level (true for intranet/trusted sites), then your process will be killed and a new process with medium integrity level will be created (seamlessly in IE8 and up). This operation is called a “Virtual Tab switch.”
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.
That should suppress the Virtual Tab switch
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