Many developers who need to run or terminate an application from Visual Basic 6.0 rely on shell commands. However, you can also accomplish this task by using API functions.
Time to code
Let’s create a simple VB project to figure out the last and first day of any given date. Fire up Visual Studio and start a new project. Now, add three command buttons: cmdCheck, cmdClose, and cmdStart. Your screen should resemble the one shown in Figure A.
|Create a simple project with three command buttons.|
Now, we’ll add some simple code to flesh out our project. Add the following code to the general declaration section event:
Private sAppName As String, sAppPath As String
Add the code in Listing A to the cmdCheck_Click() event. Next, add the following code in to the cmdClose_Click() event:
Add the following code to the cmdStart_Click() event:
‘start an application
Shell sAppPath, vbMinimizedFocus
Then, add the following code to the Form_Load() event:
sAppName = “Adobe Photoshop”
sAppPath = “D:\program files\Adobe\Photoshop 6.0\Photoshp.exe”
We used Adobe Photoshop in our example; you’d simply point to the appropriate executable in your implementation. Add a module and the code in Listing B. Press [Ctrl][F5] to run the project.
Click the Check Status button, and you’ll get a message box stating that the application is not running. Now click the Start Application button, and you’ll see the application being launched. Click Check Status again, and you’ll get confirmation that the application is running. Click the Close Application button, and you will see the application window close up.
How does this work
On the Form_Load event, you assign values to two variables (sAppName and sAppPath) that store the name and path of the application you want to run. On the cmdStart_Click event, you employ the well-known Shell command to launch the application. On the cmdCheck_Click event, you call the function IsTaskRunning (defined in the module), passing to it the name of the application (window name). If the function returns a value of true, you show a message box stating that it is running. If the function returns false, you show a message box stating that the application is not running. On the cmdClose_Click event, you call EndTask (defined in the module) to terminate it.
Finding out if an application is running
Let’s now take a look at what happens in IsTaskRunning and EndTask. IsTaskRunning receives the name of the window as the parameter sWindowName. To find out whether a particular application is running, you call the FindWindow API function. If the FindWindow function returns 0, the window was not found and therefore the application is not running. If any other value is returned, the application is running.
The EndTask function also receives the name of the window as the sWindowName parameter. This function checks if the application with window name sWindowName is running, again using FindWindow API function. If the window is not found, you don’t have to do anything because that means the application is not running. If the application is running, call the IsWindow API function to determine if the window handler is valid. If it’s not, you just exit; otherwise, close the application. To close an application, you use GetWindowLong, an API function that gets information from the window structure for the specified window and determines if the window is not disabled (WS_DISABLED).
The PostMessage API function closes the application and posts a message to the queue of the specified window (TargetHwnd). In this example, PostMessage with the WM_CLOSE argument is equivalent to invoking the Close command on the system menu for that window.
In case of errors, the ShowError function shows user-friendly messages. The function expects two arguments:
- · sText: the text of the error message you would like to display
- · sProcName: the name of the procedure or function in which an error occurred
ShowError displays a message box with the error number and information provided by sText and sProcName.
In this article, I demonstrated a simple way to launch, check status, and terminate applications using API tools, which gives you the advantage of determining if the application is running. You can grab the code for this article here.
Subscribe to the Developer Insider Newsletter
From the hottest programming languages to commentary on the Linux OS, get the developer and open source news and tips you need to know. Delivered Tuesdays and Thursdays