Link to home
Start Free TrialLog in
Avatar of deross
deross

asked on

Shell command

I have a small program in VB5 that launches another program.  I have included the following code to end the program once the other application has started:

RetVal = Shell("c:\otherprogram.exe", 1)
window_name = "otherprogram"
Running = Format$(FindWindow(class_name, window_name))
Do
DoEvents
Loop While Running = 0
End

My problem is, the application I am trying to launch is quite large and takes a lot of time to load.  My application finds the window name before the program has actually started causing it to end pre-maturely and showing any existing windows beneath.  Is there a way to make sure my application does not end until the program it is launching is fully loaded?


Thanks
Avatar of manoj_r
manoj_r

I have a small function which I use to check if my shelled program has completed or not.

Include the following in the header of module.
Declare Function WaitForSingleObject Lib "kernel32" (ByVal _
                       hHandle As Long, ByVal dwMilliseconds As Long) As Long

Public Const STANDARD_RIGHTS_ALL = &H1F0000
Public Const INFINITE = &HFFFF

Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Call the following function with the handle that is returned from the Shell command.

Function:
Public Function CommandExecuted(hAppHandle As Long) As Boolean
    Dim hProc As Long
    Dim ReturnValue As Integer
     ' Wait for the shelled application to finish:
    hProc = OpenProcess(STANDARD_RIGHTS_ALL, False, hAppHandle)
    If hProc <> 0 Then
        ReturnValue = WaitForSingleObject(hProc, 65535)
        CloseHandle hProc
    End If
    CommandExecuted = True
    Exit Function
CommandExecuted_Err:
    CommandExecuted = True
End Function


ASKER CERTIFIED SOLUTION
Avatar of mcrider
mcrider

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial