Advertisement
2002VB Windows API Call/ Explanation #17379

LaunchAppSynchronous

Unlike the Shell command in VB which launches an application asynchronous, this will launch the program synchronous. What that means is that the shell execute command will launch an application but not wait for it to execute before processing the next line of code. This code will launch a program then wait until the executable has terminated before executing the next line of code.

AI

AI Summary: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Source Code
original-source
Option Explicit
'
'Unlike the Shell command in VB which launches an application
'asynchronous, this will launch the program synchronous.
'What that means is that the shell execute command will launch
'an application but not wait for it to execute before processing
'the next line of code. This code will launch a program then
'wait until the executable has terminated before executing the
'next line of code. Works great for launching DOS exe's such
'as batch files, reindexing old databases, and other executables
'which must perform their task before your code continues.
'Some versions don't work in Windows NT because of the added
'security but this version does work in Windows NT.
'I realize there are more elegant and sophisticated ways to do
'the same thing but this one works fine for what I needed in a
'professional application I was working on. I must credit Dan
'Appleman's Programmer's Guide To The Win32 API for this code.
'I also strongly suggest that anyone interested in understanding
'more about these kind of techniques, read his book. In fact,
'I recommend all of Dan Appleman's books when you are ready to
'go from novice to professional programmer.
'I appreciate your comments but please do your homework first!

Public Function LaunchAppSynchronous(strExecutablePathAndName As String) As Boolean
 
  'Launches an executable by starting it's process
  'then waits for the execution to complete.
  'INPUT: The executables full path and name.
  'RETURN: True upon termination if successful, false if not.
  
  Dim lngResponse As Long
  Dim typStartUpInfo As STARTUPINFO
  Dim typProcessInfo As PROCESS_INFORMATION
  
  LaunchAppSynchronous = False
  
  With typStartUpInfo
   .cb = Len(typStartUpInfo)
   .lpReserved = vbNullString
   .lpDesktop = vbNullString
   .lpTitle = vbNullString
   .dwFlags = 0
  End With
  
  'Launch the application by creating a new process
  lngResponse = CreateProcessByNum(strExecutablePathAndName, vbNullString, 0, 0, True, NORMAL_PRIORITY_CLASS, ByVal 0&, vbNullString, typStartUpInfo, typProcessInfo)
  
  If lngResponse Then
   'Wait for the application to terminate before moving on
   Call WaitForTermination(typProcessInfo)
   LaunchAppSynchronous = True
  Else
   LaunchAppSynchronous = False
  End If
  
End Function

Private Sub WaitForTermination(typProcessInfo As PROCESS_INFORMATION)
  'This wait routine allows other application events
  'to be processed while waiting for the process to
  'complete.
  Dim lngResponse As Long
  'Let the process initialize
  Call WaitForInputIdle(typProcessInfo.hProcess, INFINITE)
  'We don't need the thread handle so get rid of it
  Call CloseHandle(typProcessInfo.hThread)
  'Wait for the application to end
  Do
   lngResponse = WaitForSingleObject(typProcessInfo.hProcess, 0)
   If lngResponse <> WAIT_TIMEOUT Then
     'No timeout, app is terminated
     Exit Do
   End If
   DoEvents
  Loop While True
  
  'Kill the last handle of the process
  Call CloseHandle(typProcessInfo.hProcess)
  
End Sub
Original Comments (3)
Recovered from Wayback Machine