ACC: How to Determine When a Shelled Process Ends

ID: Q178116


The information in this article applies to:
  • Microsoft Access versions 7.0, 97


SUMMARY

Advanced: Requires expert coding, interoperability, and multiuser skills.

When you run the Shell() function in a Visual Basic for Applications procedure, it starts an executable program asynchronously and returns control to the procedure. This shelled program continues to run independently of your procedure until you close it.

If your procedure needs to wait for the shelled process to end, you can use the Windows API to poll the status of the application, but this is not very efficient. This article shows you a more efficient method.

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Solution Provider or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Solution Providers, please see the following page on the World Wide Web:

http://www.microsoft.com/mcsp/
For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

http://www.microsoft.com/support/supportnet/overview/overview.asp


MORE INFORMATION

The Win32 API has integrated functionality that enables your application to wait until a shelled process has completed. To use these functions, you need to have a handle to the shelled process. To accomplish this, you need to use the CreateProcess() function instead of the Shell() function to begin your shelled program.

Creating the Shelled Process

To create an addressable process, use the CreateProcess() function to start your shelled application. The CreateProcess() function gives your program the process handle of the shelled process via one of its passed parameters.

Waiting for the Shelled Process to End

Once you use the CreateProcess() function to get a process handle, you can pass that handle to the WaitForSingleObject() function. This causes your Visual Basic for Applications procedure to suspend execution until the shelled process ends.

Below are the steps necessary to build a Visual Basic for Applications procedure that uses the CreateProcess() function to run the Windows Notepad (NOTEPAD.EXE) application. This code shows by example how to use the Windows API CreateProcess() and WaitForSingleObject() functions to wait until a shelled process ends before resuming execution.

The syntax of the CreateProcess() function is complex, 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 Instructions on How to Create the CreateProcess() Procedure

  1. Create a module and type the following lines in the Declarations section:


  2. 
          Option Explicit
    
          Private Type STARTUPINFO
             cb As Long
             lpReserved As String
             lpDesktop As String
             lpTitle As String
             dwX As Long
             dwY As Long
             dwXSize As Long
             dwYSize As Long
             dwXCountChars As Long
             dwYCountChars As Long
             dwFillAttribute As Long
             dwFlags As Long
             wShowWindow As Integer
             cbReserved2 As Integer
             lpReserved2 As Long
             hStdInput As Long
             hStdOutput As Long
             hStdError As Long
          End Type
    
          Private Type PROCESS_INFORMATION
             hProcess As Long
             hThread As Long
             dwProcessID As Long
             dwThreadID As Long
          End Type
    
          Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal _
             hHandle As Long, ByVal dwMilliseconds As Long) As Long
    
          Private Declare Function CreateProcessA Lib "kernel32" (ByVal _
             lpApplicationName As Long, ByVal lpCommandLine As String, ByVal _
             lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _
             ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
             ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, _
             lpStartupInfo As STARTUPINFO, lpProcessInformation As _
             PROCESS_INFORMATION) As Long
    
          Private Declare Function CloseHandle Lib "kernel32" (ByVal _
             hObject As Long) As Long
    
          Private Const NORMAL_PRIORITY_CLASS = &H20&
          Private Const INFINITE = -1& 
  3. Type the following two procedures in the module:


  4. 
          Public Sub ExecCmd(cmdline$)
             Dim proc As PROCESS_INFORMATION
             Dim start As STARTUPINFO
             Dim ReturnValue As Integer
    
             ' Initialize the STARTUPINFO structure:
             start.cb = Len(start)
    
             ' Start the shelled application:
             ReturnValue = CreateProcessA(0&, cmdline$, 0&, 0&, 1&, _
                NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
    
             ' Wait for the shelled application to finish:
             Do
                ReturnValue = WaitForSingleObject(proc.hProcess, 0)
                DoEvents
                Loop Until ReturnValue <> 258
    
             ReturnValue = CloseHandle(proc.hProcess)
          End Sub
    
          Sub Testing()
             ExecCmd "NOTEPAD.EXE"
             MsgBox "Process Finished"
          End Sub 
  5. Type "Testing" (without the quotation marks) in the Debug window, and then press Enter.

    Note that Notepad starts.


  6. On the File menu, click Exit.

    Note that "Process Finished" appears in a message box in Microsoft Access.



REFERENCES

For more information about getting help with Visual Basic for Applications, please see the following article in the Microsoft Knowledge Base:

Q163435 VBA: Programming Resources for Visual Basic for Applications

Additional query words: vba

Keywords : PgmHowto
Version : WINDOWS:7.0,97
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: July 6, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.