CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    May 2001
    Posts
    14

    ShellExecute: Open and close IE browser

    Hi,

    I am trying launch the IE browser and call a certain asp page and then close the browser after a few seconds. I am successful in launching the browser and calling the asp page. But for some reason, I am not able to close the browser. I must be passing the wrong window handler? Any suggestions?


    private Declare Function ShellExecute _
    Lib "shell32.dll" Alias "ShellExecuteA" _
    (byval hwnd as Long, byval lpOperation _
    as string, byval lpFile as string, byval _
    lpParameters as string, byval lpDirectory _
    as string, byval nShowCmd as Long) as Long

    private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (byval hwnd as Long, byval wMsg as Long, byval wParam as Long, lParam as Any) as Long
    private Const WM_CLOSE = &H10


    private Sub Form_Load()
    Dim r as Long
    Dim lngResult as Long

    r = ShellExecute(0, "Open", "C:\PROGRA~1\INTERN~1\IEXPLORE.EXE", "http://localhost/members/include/Use...p?schedule=yes", 0, 1)
    lngResult = SendMessage(hwnd, WM_CLOSE, 0, 0)

    End Sub




    Thanks,

    Rajat


  2. #2
    Join Date
    Aug 2001
    Location
    New York, USA
    Posts
    169

    Re: ShellExecute: Open and close IE browser

    try this TerminateTask function:

    call it like this:

    TerminateTask ("Title") ' Where title is the title of the app that you see on the title bar. for example, "Internet Explorer" or the local page name.



    Put the following in a module:

    public Declare Function EnumWindows Lib "user32" (byval wndenmprc as Long, byval lParam as Long) as Long
    public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (byval hWnd as Long, byval lpString as string, byval cch as Long) as Long
    public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (byval hWnd as Long, byval wMsg as Long, byval wParam as Long, lParam as Any) as Long
    public Const WM_CLOSE = &H10

    private Target as string

    ' Check a returned task to see if we should kill it.
    public Function EnumCallback(byval app_hWnd as Long, byval param as Long) as Long
    Dim buf as string * 256
    Dim title as string
    Dim length as Long

    ' get the window's title.
    length = GetWindowText(app_hWnd, buf, len(buf))
    title = Left$(buf, length)

    ' See if this is the target window.
    If InStr(title, Target) <> 0 then
    ' Kill the window.
    SendMessage app_hWnd, WM_CLOSE, 0, 0
    End If

    ' Continue searching.
    EnumCallback = 1
    End Function

    ' Ask Windows for the list of tasks.
    public Sub TerminateTask(AppName as string)
    Target = AppName
    EnumWindows AddressOf EnumCallback, 0
    End Sub




    Here are some links for ShellExecute and ShellExecuteEx functions -

    More info on ShellExecute function:
    http://www.vbapi.com/ref/s/shellexecute.html

    Info on ShellExecuteEx function:
    http://www.vbapi.com/ref/s/shellexecuteex.html

    [email protected]

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

Featured