Results 1 to 4 of 4
  1. #1
    2 Star Lounger
    Join Date
    Jun 2002
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Detect Browser (Access 97)

    I?ve tried the archives and Google but I?m not asking the right question?

    Is there a way to detect if a browser has been launched? I need to know if Netscape or Internet Explorer is running. I need to close the browser.

    Thanks

  2. #2
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: Detect Browser (Access 97)

    One way to do this is to enumerate all top-level windows (using the Windows EnumWindows API function), test for Windows class name, and send msg to close any IE or Netscape Navigator top-level window found. Sample code:

    Option Explicit

    Declare Function EnumWindows Lib "user32" _
    (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long

    Declare Function GetClassName Lib "user32" Alias "GetClassNameA" _
    (ByVal hwnd As Long, ByVal lpClassName As String, _
    ByVal nMaxCount As Long) As Long

    Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
    (ByVal hwnd As Long, ByVal lpString As String, _
    ByVal aint As Long) As Long

    Declare Function PostMessage Lib "user32" Alias "PostMessageA" _
    (ByVal hwnd As Long, ByVal wMsg As Long, _
    ByVal wParam As Long, lParam As Any) As Long

    Const WM_CLOSE = &H10

    Public Sub KillBrowsers()

    Dim lngRet As Long
    Dim lParam As Long

    ' Enumerate top-level windows (callback function):
    Debug.Print "Class, Title, Hwnd"
    lngRet = EnumWindows(AddressOf KillBrowserFnc, lParam)

    End Sub

    Function KillBrowserFnc(ByVal hwnd As Long, ByVal lParam As Long) As Long

    Dim rtn As Long
    Dim sBuffer As String
    Dim strClass As String
    Dim strTitle As String

    sBuffer = Space$(255)
    rtn = GetClassName(hwnd, sBuffer, 255)
    strClass = StripNulls(sBuffer)
    sBuffer = Space$(255)
    rtn = GetWindowText(hwnd, sBuffer, 255)
    strTitle = StripNulls(sBuffer)

    ' Test for IE or Netscape:
    If strClass = "IEFrame" Or _
    strClass Like "Afx:400000:b:10011:6*" Then
    PostMessage hwnd, WM_CLOSE, 0, vbNullString
    Debug.Print strClass & " " & strTitle & " " & hwnd
    End If
    ' Always return value for callback function:
    KillBrowserFnc = True

    End Function

    Public Function StripNulls(ByVal strTxt As String) As String
    If (InStr(strTxt, Chr$(0)) > 0) Then
    strTxt = Left$(strTxt, InStr(strTxt, Chr(0)) - 1)
    End If
    StripNulls = strTxt
    End Function

    Note that when testing this, it appears that while the IE class name is always "IEFrame", the Netscape class name's last six hex characters are generated dynamically & are not always the same when Netscape closed & reopened. Sample output to Debug window:

    Class, Title, Hwnd
    Afx:400000:b:10011:6:5c60345 The page cannot be found - Netscape 2098152
    Afx:400000:b:10011:6:5c60345 The page cannot be found - Netscape 721280
    IEFrame Viewing list of forums - Microsoft Internet Explorer 1508606
    IEFrame Viewing list of forums - Microsoft Internet Explorer 2950194

    Class, Title, Hwnd
    Afx:400000:b:10011:6:1d80313 The page cannot be found - Netscape 2163688
    Afx:400000:b:10011:6:1d80313 The page cannot be found - Netscape 786802

    Note that Netscape class name not same in 2nd test. Note: am using Netscape 4.7 and IE 5.5 on this pc. You may want to test this if using other versions to ensure correct class name used. If necessary, modify the callback function to print out the class name and window text for all top-level windows found to assist in determining correct Windows class name for a given application (including other browsers if necessary). BTW - your users may not appreciate you closing the browser on them??

    HTH

  3. #3
    2 Star Lounger
    Join Date
    Jun 2002
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Detect Browser (Access 97)

    Thanks Mark. I'll this a try.

  4. #4
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: Detect Browser (VB/VBA)

    I had chance to test this at home, where I have different versions of IE & Netscape as well as some other browsers installed. The following additional info provided for anyone interested:

    - IE 6.0 still uses Windows class name "IEFrame"

    - Netscape 7.1 application window use Windows class name "MozillaWindowClass"

    - Mozilla Firefox 0.8 also uses "MozillaWindowClass" (both browsers based on Mozilla 5.0)

    - Opera 7.23 uses class name "OpWindow" for main app window

    Modified code to test for other browsers listed above. An error occurred when closing Opera - there are several top-level windows of OpWindow class, the one you see (with title text) and some w/o text. Opera crashed (but not VBA host app) probably when the function sent "Close" msg to one of the captionless windows, and Windows objected. So, modified code to close window only if window text length > 0:

    ' Test for IE, Opera, Mozilla Firefox or Netscape 7.x:
    Select Case strClass
    Case "IEFrame", "OpWindow", "MozillaWindowClass"
    bFound = True
    End Select

    ' Add test for Netscape 4.7 window class:
    If bFound Or strClass Like "Afx:400000:b:10011:6*" Then
    ' Test for window text (Opera, Mozilla have titleless windows):
    If Len(strTitle) > 0 Then
    PostMessage Hwnd, WM_CLOSE, 0, vbNullString
    Debug.Print "Class: " & strClass & vbCrLf & _
    "Title: " & strTitle & vbCrLf & _
    "Hwnd: " & Hwnd & vbCrLf
    End If
    End If

    See attached text file for complete revised code and API declarations, etc. The modified function (KillBrowserFnc) successfully closed all four open browsers w/o errors or crashes. To test, open one or more browsers and run KillBrowser sub. The same code can be adapted for closing any open Windows application. To determine an application's Windows class name, open one or more instances of the application, then run TestEnumWindows sub. This will print out the window class name, title text & hwnd to Debug window. The callback function (EnumWindowsFnc) includes test for both title text and window style flag (WS_VISIBLE) so that only visible, top-level windows with title text are printed out (otherwise you'll get hundreds of results for stuff like toolbars, taskbars, etc, that are not likely to be of interest or useful for this purpose).

    HTH

Posting Permissions

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