CreateProcess problems with some applications
I am trying to use CreateProcess to launch firefox.exe. It works for some applications, such as Explorer.exe, but not for others, such as notepad.exe or firefox.exe.
CreateProcess returns True and Err.LastDllError reports success. I presume the app is opening and then closing again.
My code is:
Private Function SuperShell(ByVal App As String, ByVal WorkDir As String, dwMilliseconds As Long, ByVal _
start_size As enSW, ByVal Priority_Class As enPriority_Class) As Boolean
Dim pclass As Long
Dim sinfo As STARTUPINFO
Dim pinfo As PROCESS_INFORMATION
'Not used, but needed
Dim sec1 As SECURITY_ATTRIBUTES
Dim sec2 As SECURITY_ATTRIBUTES
'Set the structure size
sec1.nLength = Len(sec1)
sec1.lpSecurityDescriptor = 0
sec1.bInheritHandle = True
sec2.nLength = Len(sec2)
sec2.lpSecurityDescriptor = 0
sec2.bInheritHandle = True
sinfo.cb = Len(sinfo)
'Set the flags
sinfo.dwFlags = STARTF_USESHOWWINDOW
'Set the window's startup position
sinfo.wShowWindow = start_size
'Set the priority class
pclass = Priority_Class
'Start the program
If CreateProcess(vbNullString, App, sec1, sec2, False, pclass, 0&, WorkDir, sinfo, pinfo) Then '
'Wait
WaitForSingleObject pinfo.hProcess, dwMilliseconds
SuperShell = True
Else
SuperShell = False
MsgBox WinAPIError(Err.LastDllError), vbExclamation
End If
End Function
Re: CreateProcess problems with some applications
Sometimes it easier to use Shell or ShellExecute...
MSDN
Here on CodeGuru
Re: CreateProcess problems with some applications
Indeed, it is. But I need a window handle for my launched application. If there is more than one instance of Firefox running, I can't tell which one is the one I have just launched, since they all share the same ProcessID. I get more useful info out of the PROCESS_INFORMATION form CreateProcess.
Re: CreateProcess problems with some applications
I just tried your code. notepad and firefox is actually running in the background. Meaning their windows are not being shown.
My hint is you are passing an invalid value to the parameter ByVal _start_size As enSW which is then assigned to sinfo.wShowWindow
I modified your code to make it work. I removed the "ByVal _start_size As enSW" parameter and harcoded the right value to sinfo.wShowWindow
Code:
Private Const SW_HIDE As Long = 0
Private Const SW_SHOW As Long = 1
Private Function SuperShell(ByVal App As String, ByVal WorkDir As String, dwMilliseconds As Long, ByVal enPriority_Class As enPriority_Class) As Boolean
Dim pclass As Long
Dim sinfo As STARTUPINFO
Dim pinfo As PROCESS_INFORMATION
'Not used, but needed
Dim sec1 As SECURITY_ATTRIBUTES
Dim sec2 As SECURITY_ATTRIBUTES
'Set the structure size
sec1.nLength = Len(sec1)
sec1.lpSecurityDescriptor = 0
sec1.bInheritHandle = True
sec2.nLength = Len(sec2)
sec2.lpSecurityDescriptor = 0
sec2.bInheritHandle = True
sinfo.cb = Len(sinfo)
'Set the flags
sinfo.dwFlags = STARTF_USESHOWWINDOW
'Set the window's startup position
sinfo.wShowWindow = SW_SHOW 'Change this to SW_HIDE if you want to hide the process
'Set the priority class
pclass = Priority_Class
'Start the program
If CreateProcess(vbNullString, App, ByVal 0&, ByVal 0&, False, pclass, 0&, WorkDir, sinfo, pinfo) Then '
'Wait
WaitForSingleObject pinfo.hProcess, dwMilliseconds
SuperShell = True
Else
SuperShell = False
End If
End Function
PinoyAko