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
derossAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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


0
mcriderCommented:
Unfortunately, manoj_r's code will only work for when the shelled command has completed execution an terminated...


If you have source to the app that your program is starting, you can set a semaphore to let you know when the app has been completed. You can do this with a registry entry or a file.


If you don't have the source to the app that your program is starting then about the only thing you can do is... Once you find the window, have your program go to sleep for a couple of seconds, and then terminate your program.



Cheers!



0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.