Академический Документы
Профессиональный Документы
Культура Документы
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.
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
Microsoft Support
2011 Microsoft
http://support.microsoft.com/kb/129796
1-02-2011