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:
- Why the call to get_HWND randomly fails?
- Is the usage of get_HWND done correctly? The expected type is SHANDLE_PTR.
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.”
For more information about the problem and how to solve it:
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
You probably want to verify this is indeed the issues you are facing. Here is how to run a quick test:
That should suppress the Virtual Tab switch
Here is an additional info about Protected Mode (taken from the book Pro Internet Explorer 8 & 9 Development: Developing Powerful Applications for the Next Generation of IE by Matthew Crowley):