By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,688 Members | 1,862 Online
Bytes IT Community
Submit an Article
Got Smarts?
Share your bits of IT knowledge by writing an article on Bytes.

ShellWait() Function

NeoPa
Expert Mod 15k+
P: 31,277
Introduction :

The following code (module) provides the facility to execute a program, but synchronously. The Shell() function executes a command asynchronously, so returns control immediately after the command has been invoked, whether the command has completed execution or not. This is often not appropriate or convenient, so here is a version which causes the invoking code to wait until the command has terminated before resuming execution.

Solution :
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. 'Windows API Variable Prefixes
  5. 'cb = Count of Bytes (32-bit)
  6. 'w  = Word (16-bit)
  7. 'dw = Double Word (32-bit)
  8. 'lp = Long Pointer (32-bit)
  9. 'b  = Boolean (32-bit)
  10. 'h  = Handle (32-bit)
  11. 'ul = Unsigned Long (32-bit)
  12.  
  13. Private Const conUseShowWindow = &H1&
  14. Private Const conNormalPriority = &H20&
  15. Private Const conInfinite = -1&
  16.  
  17. Private Type typStartupInfo
  18.     cbLen As Long
  19.     lpReserved As String
  20.     lpDesktop As String
  21.     lpTitle As String
  22.     dwX As Long
  23.     dwY As Long
  24.     dwXSize As Long
  25.     dwYSize As Long
  26.     dwXCount As Long
  27.     dwYCount As Long
  28.     dwFillAtt As Long
  29.     dwFlags As Long
  30.     wShowWindow As Integer
  31.     cbReserved2 As Integer
  32.     lpReserved2 As Long
  33.     hStdIn As Long
  34.     hStdOut As Long
  35.     hStdErr As Long
  36. End Type
  37.  
  38. Private Type typProcInfo
  39.     hProc As Long
  40.     hThread As Long
  41.     dwProcID As Long
  42.     dwThreadID As Long
  43. End Type
  44.  
  45. Private Declare Function CreateProcessA Lib "kernel32" ( _
  46.     ByVal lpApplicationName As Long, _
  47.     ByVal lpCommandLine As String, _
  48.     ByVal lpProcessAttributes As Long, _
  49.     ByVal lpThreadAttributes As Long, _
  50.     ByVal bInheritHandles As Long, _
  51.     ByVal dwCreationFlags As Long, _
  52.     ByVal lpEnvironment As Long, _
  53.     ByVal lpCurrentDirectory As Long, _
  54.     lpStartupInfo As typStartupInfo, _
  55.     lpProcessInformation As typProcInfo) As Long
  56. Private Declare Function WaitForSingleObject Lib "kernel32" ( _
  57.     ByVal hHandle As Long, _
  58.     ByVal dwMilliseconds As Long) As Long
  59. Private Declare Function CloseHandle Lib "kernel32" ( _
  60.     ByVal hObject As Long) As Long
  61.  
  62. 'ShellWait() executes a command synchronously (Shell() works asynchronously).
  63. Public Sub ShellWait(strCommand As String, _
  64.                      Optional intWinStyle As Integer = vbNormalFocus)
  65.     Dim objProcInfo As typProcInfo
  66.     Dim objStart As typStartupInfo
  67.  
  68.     'Initialize the typStartupInfo structure:
  69.     With objStart
  70.         .cbLen = Len(objStart)
  71.         .dwFlags = conUseShowWindow
  72.         .wShowWindow = intWinStyle
  73.     End With
  74.     'Start the shelled application:
  75.     Call CreateProcessA(lpApplicationName:=0&, _
  76.                         lpCommandLine:=strCommand, _
  77.                         lpProcessAttributes:=0&, _
  78.                         lpThreadAttributes:=0&, _
  79.                         bInheritHandles:=1&, _
  80.                         dwCreationFlags:=conNormalPriority, _
  81.                         lpEnvironment:=0&, _
  82.                         lpCurrentDirectory:=0&, _
  83.                         lpStartupInfo:=objStart, _
  84.                         lpProcessInformation:=objProcInfo)
  85.     'Wait for the shelled application to finish
  86.     Call WaitForSingleObject(hHandle:=objProcInfo.hProc, _
  87.                              dwMilliseconds:=conInfinite)
  88.     Call CloseHandle(hObject:=objProcInfo.hProc)
  89. End Sub
Sep 29 '08 #1
Share this Article
Share on Google+