Article ID: 129796 - Last Review: July 13, 2004 - Revision: 2.1 How To Use a 32-Bit Application to Determine When a Shelled Process EndsThis article was previously published under Q129796 On This PageSUMMARY
Executing the Shell() function in a Visual Basic for Windows program starts another executable program asynchronously and returns control to the Visual Basic application. This shelled program continues to run independently of your application until the user closes it.
However, if your Visual Basic application needs to wait for the shelled process to terminate, you could use the Windows API to poll the status of the application, but this is not a very efficient technique. The example in this article demonstrates a better way. A 16-bit application would use a completely different technique to accomplish the same effect. For additional information, click the article number on the 16-bitprocess below
to view the article on the 16-bitprocess in the Microsoft Knowledge Base:
96844
(http://support.microsoft.com/kb/96844/EN-US/
)
How To Determine When a Shelled Process Has Terminated
MORE INFORMATION
The Win32 API has integrated functionality that enables an application to
wait until a shelled process has completed. To use these functions, you
need a handle to the shelled process. The easiest way to achieve this is to
use the CreateProcess() API function to launch your shelled program rather than Visual Basic's Shell() function.
Creating the Shelled ProcessIn a 32-bit application, you need to create an addressable process. To do this, use the CreateProcess() function to start your shelled application. The CreateProcess() function gives your program the process handle of the shelled process through one of its passed parameters.Waiting for the Shelled Process to TerminateHaving used CreateProcess() to get a process handle, pass that handle to the WaitForSingleObject() function. This causes your Visual Basic application to suspend execution until the shelled process terminates.Getting the Exit Code from the Shelled ApplicationIt was common for a DOS application to return an exit code indicating the status of the completed application. While Windows provides other ways to convey the same information, some applications only provide exit codes. Passing the process handle to the GetExitCodeProcess() API allows you to retrieve this information.Following are the steps necessary to build a Visual Basic for Windows program that uses the CreateProcess() function to execute the Windows Notepad (Notepad.exe) application. This code demonstrates how to use the Windows API CreateProcess() and WaitForSingleObject() functions to wait until a shelled process terminates before resuming execution. It also uses the GetExitCodeProcess() function to retrieve the exit code of the shelled process, if any. The syntax of the CreateProcess() function is extremely complicated, so in the example code, it is encapsulated into a function called ExecCmd(). ExecCmd() takes one parameter, the command line of the application to execute. Step-by-Step Example
REFERENCESFor additional information, click the article number below
to view the article in the Microsoft Knowledge Base:
288216
(http://support.microsoft.com/kb/288216/EN-US/
)
PRB: Call to ExitProcess() from Visual Basic Application Hinders Process Exit
For more information, please see the "CreateProcess" topic in the MSDN Library CD-ROM.
APPLIES TO
| Article Translations
|
Back to the top
