Click to See Complete Forum and Search --> : How to start a program using CreateProcess


Shalini Shikha
October 11th, 1999, 08:46 PM
I need to start a program using CreateProcess and wait for the program to finish before the rest of the code of VB executes. Can you tell me how to do this?

Lothar Haensler
October 12th, 1999, 02:36 AM
this is a sample for starting MSPaint and wait until the user ends it.

Dim l as Long
Dim h as Long
Dim hp as Long
Dim sa as STARTUPINFO
Dim sec as SECURITY_ATTRIBUTES
Dim pi as PROCESS_INFORMATION
sa.cb = LenB(sa)
CreateProcess vbNullString, "mspaint", 0, 0, false, 0, byval 0, vbNullString, sa, pi
hp = OpenProcess(SYNCHRONIZE, 0&, pi.dwProcessId)
l = WaitForSingleObject(hp, 100)
Do While l = WAIT_TIMEOUT
DoEvents
l = WaitForSingleObject(hp, 100)
Loop




tested with VB 6 and NT 4

Lothar Haensler
October 12th, 1999, 02:53 AM
Oops, I forgot the Declare Statements.
I need to make clear that I modified the Declare Statement for CreateProcess. It's not the original from the APIViewer!


private Declare Function OpenProcess Lib "kernel32" (byval dwDesiredAccess as Long, byval bInheritHandle as Long, byval dwProcessId as Long) 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 SYNCHRONIZE = &H100000
Const WAIT_TIMEOUT = &H102

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 SECURITY_ATTRIBUTES
nLength as Long
lpSecurityDescriptor as Long
bInheritHandle 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 CreateProcess Lib "kernel32" Alias "CreateProcessA" _
(byval lpApplicationName as string, byval lpCommandLine as string, byval x as Long, byval y as Long, _
byval bInheritHandles as Long, byval dwCreationFlags as Long, _
lpEnvironment as Any, byval lpCurrentDriectory as string, lpStartupInfo as STARTUPINFO, _
lpProcessInformation as PROCESS_INFORMATION) as Long
private Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA" (byval dwFlags as Long, _
lpSource as Any, byval dwMessageId as Long, byval dwLanguageId as Long, byval lpBuffer as string, _
byval nSize as Long, Arguments as Long) as Long