Visual Basic 6 function "StartProcess"

Go back

Below you'll find the source for the Visual Basic 6 function StartProcess.

Attribute VB_Name = "modStartProcess"
' This function is downloaded from:
' http://www.stefanthoolen.nl/archive/vb6-functions/
' 
' You may freely distribute this file but please leave all comments, including this one, in it.
' 
' @Author Stefan Thoolen <mail@stefanthoolen.nl>

Option Explicit

Private Declare Function CreateProcessA Lib "kernel32" (ByVal lpApplicationName As Any, ByVal lpCommandLine As String, ByVal lpProcessAttributes As Any, ByVal lpThreadAttributes As Any, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Any, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long

Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long

Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Private Const STARTF_USESHOWWINDOW = &H1

Private Const NORMAL_PRIORITY_CLASS = &H20&

Public Enum tShowWindow
    SW_HIDE = 0&
    SW_SHOWNORMAL = 1&
    SW_SHOWMINIMIZED = 2&
    SW_SHOWMAXIMIZED = 3&
    SW_MAXIMIZE = 3&
    SW_SHOWNOACTIVATE = 4&
    SW_SHOW = 5&
    SW_MINIMIZE = 6&
    SW_SHOWMINNOACTIVE = 7&
    SW_SHOWNA = 8&
    SW_RESTORE = 9&
    SW_SHOWDEFAULT = 10&
    SW_FORCEMINIMIZE = 11&
    SW_MAX = 11&
End Enum

Private Type PROCESS_INFORMATION
        hProcess As Long
        hThread As Long
        dwProcessId As Long
        dwThreadId As Long
End Type

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

''
' Starts a new process and returns the Process ID
' @param  String       cmdline            The commandline to execute
' @param  tShowWindow  windowstate        The initial windowstate
' @param  String       working_directory  The active directory for the commandline
' @param  Boolean      wait               If true, this function waits until the process is finished
' @return Long                            The process ID. Keep in mind that if wait is true, this value is useless
' @author   Stefan Thoolen <mail@stefanthoolen.nl>
Public Function StartProcess(ByVal cmdline As String, Optional ByVal windowstate As tShowWindow = SW_SHOWDEFAULT, Optional ByVal working_directory As String, Optional ByVal wait As Boolean) As Long
    Dim lpSI As STARTUPINFO, lpPI As PROCESS_INFORMATION
    Dim l As Long
    
    ' Defines that we use a window state
    lpSI.dwFlags = STARTF_USESHOWWINDOW
    lpSI.wShowWindow = windowstate
    lpSI.cb = Len(lpSI)
    
    ' For some reason it doesn't work with an empty string
    If working_directory = "" Then
        l = CreateProcessA(0&, cmdline, 0&, 0&, 1&, NORMAL_PRIORITY_CLASS, 0&, 0&, lpSI, lpPI)
    Else
        l = CreateProcessA(0&, cmdline, 0&, 0&, 1&, NORMAL_PRIORITY_CLASS, 0&, working_directory, lpSI, lpPI)
    End If
    ' Execution failed, we exit with no return value
    If l = 0 Then Exit Function
    
    ' We set the process ID as return value
    StartProcess = lpPI.dwProcessId
    
    ' If we need to wait, lets do that for infinite miliseconds
    If wait Then WaitForSingleObject lpPI.hProcess, -1&
    
    ' Closes our handle to the process (the process ID is still valid)
    CloseHandle lpPI.hProcess
    CloseHandle lpPI.hThread
End Function