|
-
April 20th, 2011, 05:12 PM
#1
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
-
April 21st, 2011, 03:26 AM
#2
Re: CreateProcess problems with some applications
Sometimes it easier to use Shell or ShellExecute...
MSDN
Here on CodeGuru
Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
WPF Articles : 3D Animation 1 , 2 , 3
Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.
-
April 21st, 2011, 04:14 AM
#3
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.
-
May 22nd, 2011, 05:51 AM
#4
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
Last edited by PinoyAko; May 30th, 2011 at 02:24 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|