Home

Created By Wesley Griffin

 
 
Determine the WebBrowser Controls HWND Property using the EnumChildWindow API.
 
  Home > Source Code > Forms and Controls > Retrieve WebBrowser Handle
 
 
What You Need:
Visual Basic 6
WebBrowser Control
Zip Extractor Utility(WinZip etc) if you want to download the zipped project.
  With nearly every other windowed control that you can drop onto a VB form you can retrieve the handle by simply calling:
 
[Control].HWND 
  But with the WebBrowser Control when you call this function you get the error message:

 <  Method 'HWND' of object 'IWebBrowser2' failed  >

Why? Well who knows, maybe this was a ploy by Microsoft to stop the standard programmer from subclassing the webbrowser control.  Whatever the reason for Microsoft blocking us from retrieving the handle, like in everything in life, there's a workaround.

The way we are going to approach this is to use the parent object of the webbrowser control and then enumerate every child object until we find the child with the class name of the webbrowser control and alas! We have found the handle.

Place this code into a module, it contains the API declarations, variables, call-back function to handle the enumurated child windows and the main function which returns the webbrowsers handle as a long value.

'Decalrations and variables
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private lngWebHandle As Long      
 
'Function to handle enumerated child windows
Private Function EnumChildProc(ByVal hWnd As Long, ByVal lParam As Long) As Long
    Dim lngReturnValue As Long, strClassName As String
    strClassName = Space(256)
    lngReturnValue = GetClassName(hWnd, strClassName, 256)
    If Left$(strClassName, lngReturnValue) = "Internet Explorer_Server" Then
        lngWebHandle = hWnd
    End If
    EnumChildProc = 1
End Function
 
'Main Function
Public Function GetHandle() As Long
    lngWebHandle = 0
    EnumChildWindows Me.hWnd, AddressOf EnumChildProc, ByVal 0&
    GetHandle = lngWebHandle
End Function
   
  Now all you have to do to find the handle of the webbrowser is use the following syntax in the code window of your form:
 
Dim lHandle as Long
lHandle = gethandle(WebBrowser1)
  Please remember the WebBrowser control will not return a handle until it has loaded a page. So in the Form_Load procedure in you form code window add code to navigate to a page for example:
 
WebBrowser1.Navigate "About:Blank"
 

Note: If for some reason you have two webbrowser controls on one form this code is not for you.  As it enumerates the child windows of the form it will only return the last window of the class "Internet Explorer_Server", therefore ignoring the first instance of the window.  I will post a solution to solving the solution of having two webbrowser controls soon.

To download the sample project which contains the above code click the picture below.

 

  Visual Basic 6 Example
 
  If you do not have an application to unzip zip archives please click here.
 

 Top

 
  Warning:
All material from this site must be used at own risk.  VB First Aid will take no responsibility for any loss of data or any other errors.  The material on this site was written for Visual Basic Version 6, therefore compatibility with other versions may cause errors.