Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Launch URL and pause execution until the browser closes

Status
Not open for further replies.

mdav2

Programmer
Aug 22, 2000
363
GB
I am trying to write some code that will launch a URL and pause the execution in VB until the browser is closed.

I have code that will launch a program and pause that:

Code:
'---------- Functions for pausing shell execution ----------
Private Declare Function OpenProcess Lib "Kernel32" _
  (ByVal dwDesiredAccess As Long, _
   ByVal bInheritHandle As Long, _
   ByVal dwProcessId As Long) As Long

Private Declare Function GetExitCodeProcess Lib "Kernel32" _
  (ByVal hProcess As Long, lpExitCode As Long) As Long

Private Declare Function CloseHandle Lib "Kernel32" _
  (ByVal hObject As Long) As Long

Private Const PROCESS_QUERY_INFORMATION = &H400
Private Const STATUS_PENDING = &H103&
'---------- Functions for pausing shell execution ----------

Function RunShell(cmdline As String)

    Dim hProcess As Long
    Dim ProcessId As Long
    Dim exitCode As Long

    ' uses the original shell to run command
    ProcessId = Shell(cmdline, vbHide)
    hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, ProcessId)

    Do

        Call GetExitCodeProcess(hProcess, exitCode)
        DoEvents
   
    Loop While exitCode = STATUS_PENDING

    Call CloseHandle(hProcess)

End Function

This will happily sit and wait for the application and then continue with the code directly after the line that it is called from

I have code to launch a URL:

Code:
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

Public Function OpenBrowser(ByVal URL As String) As Boolean
    Dim res As Long
    
    ' it is mandatory that the URL is prefixed with http:// or https://
    If InStr(1, URL, "http", vbTextCompare) <> 1 Then
        URL = "[URL unfurl="true"]http://"[/URL] & URL
    End If
    
    res = ShellExecute(0&, "open", URL, vbNullString, vbNullString, vbNormalFocus)
    OpenBrowser = (res > 32)
    
End Function

Private Sub cmdTestUrl_Click()

    Call OpenBrowser(txtURL.Text)

    ' >>>> PAUSE HERE <<<<

    MsgBox "You're back in play"

End Sub

This will open the browser but then I need to put the pause in.

I thought if I could identify the browser process it opens I could modify my first lot of code to essentially use the process number but I am not sure how to do this. Could I use a createobject command to make this easier?

My second idea is to embed an IE control into the form and reference that. I think that it should be easy enough to check when it has an activate URL and when its been completed.

Any ideas would be appreciated.

Mark

Mark Davies
Warwickshire County Council
 
Another idea would be to simply prefix the URL with the internet explorer program and pass the URL as a prameter. This relies upon IE being installed and in the same place (although I could check that the executable exists first).

I could also query the default browser from the registry. Saying this IE will always be on someones machine that runs windows and all the certificates I have will be installed for use with IE so querying the registry wouldn't be as useful as doing a file search.

Mark Davies
Warwickshire County Council
 
What you are looking for is "Shell and Wait"... see thread222-1527750



Good Luck

 
vb5prgrmr, the tread you pointed me to opens an object and waits for a specified period. I actually want to wait indefinitely so unless I put in a massive millisecond pause value its not actually going to achieve what I want.

I have two solutions now:

Solution 1 basically opens an ie object and loops until its closed:

Code:
    ' Create a simple IE object and use a loop on readystate
    Set ie1 = CreateObject("InternetExplorer.Application")
    ie1.Visible = True
    ie1.Navigate2 "[URL unfurl="true"]http://www.google.com"[/URL]
    
    ' Wait for readystate to = 0 before proceding
    While ie1.readystate <> 0
    Wend

    MsgBox "back in play"

The other solution was to doctor the code previously posted code and just paste in the internet explorer and use a URL as a parameter:

Code:
    Dim hProcess As Long
    Dim ProcessId As Long
    Dim exitCode As Long
    Dim cmdline As String

    If Trim$(txtURL.Text) = "" Then Exit Function
    
    strie = "C:\Program Files\Internet Explorer\iexplore.exe"
    cmdline = Chr(34) + strie + Chr(34) + " " + Chr(34) + txtURL.Text + Chr(34)

    ' uses the original shell to run command
    ProcessId = Shell(cmdline, vbNormalFocus)
    hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, ProcessId)

    Do

        Call GetExitCodeProcess(hProcess, exitCode)
        DoEvents
   
    Loop While exitCode = STATUS_PENDING

    Call CloseHandle(hProcess)

This could be tidied up to query the registry for the default browser to make it more robust but thats easy easy enough to add.

One thing I did notice when running these was they both seemed to tie up the CPU usage quite badly.

Mark Davies
Warwickshire County Council
 
Your wrong there mdav, it will wait until the process has closed while it is in that loop. Each iteration of the loop, the call only waits for 10 milliseconds, then it gives way to a doevents call to keep the application alive, checks the return value from the call and if not equal to having closed down, it makes the call again...



Good Luck

 
>the tread you pointed me to opens an object and waits for a specified period

Did you read the entire, lengthy thread? The second post shows how to modify the first post to avoid this issue

>both seemed to tie up the CPU usage quite badly

The modification also deals with this issue.

Whoever posted it must have been quite prescient ... ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top