Search code examples
windowsinternet-explorervisual-c++comwindow-handles

Failed to get the handle of the Windows Internet Explorer main window


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:

  1. Why the call to get_HWND randomly fails?
  2. 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!


Solution

  • 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

    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.

    You probably want to verify this is indeed the issues you are facing. Here is how to run a quick test:

    1. Open IE
    2. Go to settings
    3. Security tab
    4. Local intranet
    5. Click the "Sites" button
    6. Uncheck “automatically detect intranet network”

    That should suppress the Virtual Tab switch

    Test IE integrity level

    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):

    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