PRB: Obtaining the HWND for the WebBrowser Control

ID: Q244310


The information in this article applies to:
  • Microsoft Internet Explorer (Programming) versions 4.0, 4.01, 4.01 SP1, 4.01 SP2, 5


SYMPTOMS

Hosting the WebBrowser control in either a Visual Basic or Visual C++ application and calling its HWND property usually returns the following HRESULT of E_FAIL:

Method HWND of IWebBrowser2 failed.


CAUSE

The HWND property is not a valid property of the WebBrowser control.


RESOLUTION

WARNING: These techniques may not work for frame windows in future versions of Internet Explorer after 5.01. The advice that follows is only guaranteed to work for the top-level WebBrowser control.

Visual C++ developers can use the IOleWindow interface of the WebBrowser control and call its GetWindow() method to retrieve the WebBrowser's HWND:


IOleWindow *pOWin;
HWND hBWnd;

HRESULT hRes = m_pBrowserApp->QueryInterface(IID_IOleWindow, (void **)&pOWin);
if (SUCCEEDED(hRes)) {
    hRes = pOWin->GetWindow(&hBWnd);
    if (SUCCEEDED(hRes)) {
        // Place hBWnd-manipulating code here
    }
}	 
Visual Basic developers can use the following code, which takes the HWND of the hosting form and examines all its children until it finds the window with a window class of HTML_InternetExplorer. This is the topmost window of the WebBrowser control.

CAUTION: The Internet Explorer window class name may change in future browser versions. Use this workaround with care.

Option Explicit

Public Const GW_CHILD = 5
Public Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Public Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long

Public Function GetBrowserWindow(hwndBrowserContainer As Long) As Long
   Dim RetVal As Long
   Dim Result As Long
   Dim hwndChild As Long
   Dim hwnd As Long
   Dim ClassString As String * 256
  
   hwnd = hwndBrowserContainer
   hwndChild = hwnd
   
   While (Result = 0) And (hwndChild <> 0)
       hwndChild = GetWindow(hwnd, GW_CHILD)
       If hwndChild <> 0 Then
           hwnd = hwndChild
           RetVal = GetClassName(hwnd, ClassString, 256)
           If Left$(ClassString, InStr(ClassString, Chr$(0)) - 1) = "HTML_Internet Explorer" Then
               Result = 1
           End If
       End If
   Wend
   
   GetBrowserWindow = hwnd
End Function 
Please note that neither of these techniques apply if you are automating Internet Explorer. For automation, C++ developers can QueryInterface their browser reference for IWebBrowserApp, whose get_HWND() property is valid whenever Microsoft Internet Explorer is instantiated as a running executable. No such workaround exists for Visual Basic.


STATUS

This behavior is by design.

Additional query words:

Keywords : kbIE400 kbIE401 kbWebBrowser kbIE401sp2 kbGrpInet kbIE500
Version : WINDOWS:4.0,4.01,4.01 SP1,4.01 SP2,5
Platform : WINDOWS
Issue type : kbprb


Last Reviewed: December 10, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.