Results 1 to 9 of 9

Thread: [RESOLVED] webbrowser HWND

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    111

    Resolved [RESOLVED] webbrowser HWND

    hello

    i have a problem when i try to get the webbrowser's hwnd within my form

    Code:
    Private Sub Form_Load()
    Dim x As Long
    x = WebBrowser1.hWnd
    End Sub
    it tells me
    runtimererror
    Method 'HWND' of object 'IWebBrowser2' failed

    So please can someone help me to get the hwnd of the webbrowser control?

    thanx
    Adel

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: webbrowser HWND

    Actually it depends on which window you actually wants to get the hWnd for... The WebBrowser control is actually just a wrapper called "Shell Embedding" that in turn contains the "Internet Explorer_Server". You can get the hWnd of the "Shell Embedding" window with a simple call to the FindWindowEx API function. However if you need the handle of the "Internet Explorer_Server" you'll need to enumerate the child windows using the (surprise, surprise) EnumChildWindow API function, that requires a callback function.

    What do you want to do with the handle once you've got it?

  3. #3
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: webbrowser HWND

    Here's two examples. The first gets the hWnd of the "Shell Embedding" window, and the second for the "Internet Explorer_Server".
    vb Code:
    1. Private Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" ( _
    2.     ByVal hWnd1 As Long, _
    3.     ByVal hWnd2 As Long, _
    4.     ByVal lpsz1 As String, _
    5.     ByVal lpsz2 As String _
    6. ) As Long
    7.  
    8. Private Sub Command1_Click()
    9.     Dim hWnd As Long
    10.     hWnd = FindWindowEx(Me.hWnd, 0, "Shell Embedding", vbNullString)
    11.     MsgBox hWnd
    12. End Sub
    For this second example, the code must reside in a regular BAS module.
    vb Code:
    1. Private Declare Function GetClassName Lib "user32.dll" Alias "GetClassNameA" ( _
    2.     ByVal hWnd As Long, _
    3.     ByVal lpClassName As String, _
    4.     ByVal nMaxCount As Long _
    5. ) As Long
    6.  
    7. Private Declare Function EnumChildWindows Lib "user32.dll" ( _
    8.     ByVal hWndParent As Long, _
    9.     ByVal lpEnumFunc As Long, _
    10.     ByVal lParam As Long _
    11. ) As Long
    12.  
    13. Private hWndIE As Long
    14.  
    15. Private Function EnumChildProc(ByVal hWnd As Long, ByVal lParam As Long) As Long
    16.     Dim sClassName As String
    17.     sClassName = String(255, vbNullChar)
    18.     Call GetClassName(hWnd, sClassName, 255)
    19.     sClassName = Left$(sClassName, InStr(sClassName, vbNullChar) - 1)
    20.     If sClassName <> "Internet Explorer_Server" Then
    21.         EnumChildProc = 1
    22.     Else
    23.         hWndIE = hWnd
    24.     End If
    25. End Function
    26.  
    27. Public Function GetBrowserHandle(ByVal hWndParent) As Long
    28.     hWndIE = 0
    29.     Call EnumChildWindows(hWndParent, AddressOf EnumChildProc, 1)
    30.     GetBrowserHandle = hWndIE
    31. End Function
    To get the hWnd of the "Internet Explorer_Server" simply call the GetBrowserHandle function passing the hWnd of the Form containing the WebBrowser control:
    vb Code:
    1. Private Sub Command1_Click()
    2.     Dim hWnd As Long
    3.     hWnd = GetBrowserHandle(Me.hWnd)
    4.     MsgBox hWnd
    5. End Sub

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    111

    Re: webbrowser HWND

    thanx alot u really really helped me

    that was the code i was looking for thanx

  5. #5
    New Member
    Join Date
    Jul 2008
    Posts
    1

    Re: [RESOLVED] webbrowser HWND

    i have the same problem,thanks !!

  6. #6
    New Member
    Join Date
    Feb 2009
    Posts
    1

    Re: [RESOLVED] webbrowser HWND

    i have the same problem tooooo!! thanks !!

  7. #7
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: webbrowser HWND

    Quote Originally Posted by Joacim Andersson
    To get the hWnd of the "Internet Explorer_Server" simply call the GetBrowserHandle function passing the hWnd of the Form containing the WebBrowser control:
    Code:
    Private Sub Command1_Click()
        Dim hWnd As Long
        hWnd = GetBrowserHandle(Me.hWnd)
        MsgBox hWnd
    End Sub
    Always returns zero.

  8. #8
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: webbrowser HWND

    Quote Originally Posted by jmsrickland
    Always returns zero.
    For the GetBrowserHandle function to return anything but zero you need to have a webbrowser component on the form and navigated to some web site (it must have something loaded). Otherwise there are no "Internet Explorer_Server" loaded and you will only be able to find the "Shell Embedding" window.

  9. #9
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: [RESOLVED] webbrowser HWND

    OK, thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width