Article ID: 96844 - Last Review: July 1, 2004 - Revision: 1.2 How To Determine When a Shelled Process Has TerminatedThis article was previously published under Q96844 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. The shelled program continues to run indefinitely until
the user closes it -- not until your Visual Basic program terminates.
However, your program can wait until the shelled program has finished by
polling the return value of the Windows API GetModuleUsage() function. This
article describes the method and provides a code example. NOTE: The GetModuleUsage() function does not exist in Windows NT. There is a completely different process that would be used to accomplish the same thing from a 32-bit application. For additional information on the 32-bit implementation, please see the following article in the Microsoft Knowledge Base: 129796
(http://support.microsoft.com/kb/129796/EN-US/
)
How to Determine When a Shelled 32-bit Process Has Terminated
MORE INFORMATION
This information is included with the Help file provided with Microsoft
Visual Basic version 3.0 for Windows, and on the Microsoft Developer
Network (MSDN) Visual Basic Starter Kit provided with Microsoft Visual
Basic version 4.0, Professional and Enterprise Editions.
Technique Also Works with MS-DOS ProgramsThe technique described in this article also works for MS-DOS programs. The return value from the Visual Basic Shell() function is a unique instance handle to the MS-DOS session that was started in the Shell(). If you call GetModuleUsage() with that handle after the MS-DOS session in question has ended, GetModuleUsage() will return 0 because the handle is no longer valid. This can be verified with the following code:Monitoring the Status of a Shelled ProcessBy using the Windows API GetModuleUsage() function, your Visual Basic program can monitor the status of a shelled process. The return value from the Shell() function can be used to call the GetModuleUsage() function continuously within a loop to find out if the shelled program has finished. If the Shell() function is successful, the return value is the instance handle for the shelled program. This instance handle can be passed to the GetModuleUsage() function to determine the reference count for the module. When the GetModuleUsage() function returns a value of 0 or less, the shelled program has finished.This algorithm works correctly regardless of the WindowStyle used to shell the program. In addition, this method works correctly when:
Step-by-Step Example
APPLIES TO
| Article Translations
|

Back to the top
