Вы находитесь на странице: 1из 4

Article ID: 129796 - Last Review: July 13, 2004 - Revision: 2.

How To Use a 32-Bit Application to Determine When a Shelled Process Ends


This article was previously published under Q129796

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

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 Process


In 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 Terminate


Having 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 Application


It 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.

http://support.microsoft.com/kb/129796

1-02-2011

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
1. 2. Start a new project in Visual Basic. Form1 is created by default. Add the following code to the General Declarations section of Form1:

http://support.microsoft.com/kb/129796

1-02-2011

PrivateTypeSTARTUPINFO cbAsLong lpReservedAsString lpDesktopAsString lpTitleAsString dwXAsLong dwYAsLong dwXSizeAsLong dwYSizeAsLong dwXCountCharsAsLong dwYCountCharsAsLong dwFillAttributeAsLong dwFlagsAsLong wShowWindowAsInteger cbReserved2AsInteger lpReserved2AsLong hStdInputAsLong hStdOutputAsLong hStdErrorAsLong EndType PrivateTypePROCESS_INFORMATION hProcessAsLong hThreadAsLong dwProcessIDAsLong dwThreadIDAsLong EndType PrivateDeclareFunctionWaitForSingleObjectLib"kernel32" (ByVal_ hHandleAsLong,ByValdwMillisecondsAsLong)AsLong PrivateDeclareFunctionCreateProcessALib"kernel32" (ByVal_ lpApplicationNameAsString,ByVallpCommandLineAs String,ByVal_ lpProcessAttributesAsLong,ByVallpThreadAttributesAs Long,_ ByValbInheritHandlesAsLong,ByValdwCreationFlagsAs Long,_ ByVallpEnvironmentAsLong,ByVallpCurrentDirectoryAs String,_ lpStartupInfoAsSTARTUPINFO,lpProcessInformationAs_ PROCESS_INFORMATION)AsLong PrivateDeclareFunctionCloseHandleLib"kernel32"_ (ByValhObjectAsLong)AsLong PrivateDeclareFunctionGetExitCodeProcessLib"kernel32"_ (ByValhProcessAsLong,lpExitCodeAsLong)AsLong PrivateConstNORMAL_PRIORITY_CLASS=&H20& PrivateConstINFINITE=1& PublicFunctionExecCmd(cmdline$) DimprocAsPROCESS_INFORMATION DimstartAsSTARTUPINFO

http://support.microsoft.com/kb/129796

1-02-2011

3. 4. 5.

Press the F5 key to run the application. Using the mouse, click the Form1 window. At this point the NotePad application is started. Quit NotePad. A MsgBox appears indicating termination of the NotePad application and an exit code of 0. NOTE: The MsgBox statement following the ExecCmd() function is not executed because the WaitForSingleObject() function prevents it. The message box does not appear until Notepad is closed when the user chooses Exit from Notepad's File menu (ALT, F, X).

For 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
Keywords: kbhowto kbtophit KB129796

Get Help Now


Contact a support professional by E-mail, Online, or Phone

Microsoft Support

2011 Microsoft

http://support.microsoft.com/kb/129796

1-02-2011

Вам также может понравиться