[Webinar] Streamline your web hosting managementRegister Today


Shell command

Posted on 1999-12-22
Medium Priority
Last Modified: 2010-05-02
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))
Loop While Running = 0

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?

Question by:deross

Expert Comment

ID: 2299903
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.

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 = True
End Function

LVL 14

Accepted Solution

mcrider earned 200 total points
ID: 2300070
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.



Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

591 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question