CreateProcess not working

I'm trying to get CreateProcess to open a new internet explorer window to view an xml file. I have tried ShellExecute and ShellExecuteEx, so I would like to try this method if I can get it to work. The CreateProcess call doesn't return an error code but it doesn't open up Internet Explorer.

Here is the code I am using:

Public Function ExecuteProcess(ByVal i_sFileName As String) As Long
       
    Dim sExecutable As String * 255
    Dim lResult As Long
    Dim start As STARTUPINFO
    Dim proc As PROCESS_INFORMATION
    Dim sAtt As SECURITY_ATTRIBUTES
   
On Error GoTo FAILED

    lResult = FindExecutable(i_sFileName, "", sExecutable)
    If lResult <= 32 Or sExecutable = "" Then GoTo FAILED
   
    With start
        .cb = Len(start)
        .dwFlags = STARTF_USESHOWWINDOW
        .wShowWindow = SW_SHOWNORMAL
    End With
   
    lResult = CreateProcess(vbNullString, Replace(Trim(sExecutable), Chr$(0), "") & " " & i_sFileName, sAtt, sAtt, 0&, NORMAL_PRIORITY_CLASS, 0&, vbNullString, start, proc)
    If lResult = 0 Then MsgBox ApiErrorText(Err.LastDllError)
    Do
        DoEvents
        lResult = WaitForSingleObject(proc.hProcess, 0)
    Loop While lResult = WAIT_TIMEOUT
   
    CloseHandle proc.hProcess
    CloseHandle proc.hThread


    Exit Function
   
FAILED:

    ExecuteProcess = -1
   
End Function
PLavelleAsked:
Who is Participating?
 
Erick37Connect With a Mentor Commented:
Some slight modifications which seem to clear up the problems...  with the help from VBNet at this site:
http://vbnet.mvps.org/code/internet/browserstart.htm

'Note the TrimNull function, and the CreateProcess Declare


Option Explicit

'Private Const CREATE_NEW_CONSOLE As Long = &H10
Private Const NORMAL_PRIORITY_CLASS As Long = &H20
'Private Const INFINITE As Long = -1
Private Const STARTF_USESHOWWINDOW As Long = &H1
Private Const SW_SHOWNORMAL As Long = 1

'Private Const MAX_PATH As Long = 260
'Private Const ERROR_FILE_NO_ASSOCIATION As Long = 31
'Private Const ERROR_FILE_NOT_FOUND As Long = 2
'Private Const ERROR_PATH_NOT_FOUND As Long = 3
'Private Const ERROR_FILE_SUCCESS As Long = 32 'my constant
'Private Const ERROR_BAD_FORMAT As Long = 11
Private 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 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 lpAppName As String, _
   ByVal lpCommandLine As String, _
   ByVal lpProcessAttributes As Long, _
   ByVal lpThreadAttributes As Long, _
   ByVal bInheritHandles As Long, _
   ByVal dwCreationFlags As Long, _
   ByVal lpEnvironment As Long, _
   ByVal lpCurrentDirectory As Long, _
   lpStartupInfo As STARTUPINFO, _
   lpProcessInformation As PROCESS_INFORMATION) As Long
     
Private Declare Function CloseHandle Lib "kernel32" _
  (ByVal hObject As Long) As Long

Private Declare Function FindExecutable Lib "shell32" _
   Alias "FindExecutableA" _
  (ByVal lpFile As String, _
   ByVal lpDirectory As String, _
   ByVal sResult As String) As Long

'Private Declare Function GetTempPath Lib "kernel32" _
   Alias "GetTempPathA" _
  (ByVal nSize As Long, _
   ByVal lpBuffer As String) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long

Public Function ExecuteProcess(ByVal i_sFileName As String) As Long
       
    Dim sExecutable As String
    Dim lResult As Long
    Dim start As STARTUPINFO
    Dim proc As PROCESS_INFORMATION
    'Dim sAtt As SECURITY_ATTRIBUTES
   
On Error GoTo FAILED
    sExecutable = Space(255)
    lResult = FindExecutable(i_sFileName, "", sExecutable)
    sExecutable = TrimNull(sExecutable)
    Debug.Print sExecutable
    If lResult <= 32 Or sExecutable = "" Then GoTo FAILED
   
    With start
        .cb = Len(start)
        .dwFlags = STARTF_USESHOWWINDOW
        .wShowWindow = SW_SHOWNORMAL
    End With
   
    lResult = CreateProcess(vbNullString, sExecutable & " " & i_sFileName, 0&, 0&, 0&, NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
    If lResult = 0 Then MsgBox CStr(Err.LastDllError)
    Do
        DoEvents
        lResult = WaitForSingleObject(proc.hProcess, 0)
    Loop While lResult = WAIT_TIMEOUT
    Debug.Print "Closed"
    CloseHandle proc.hProcess
    CloseHandle proc.hThread


    Exit Function
   
FAILED:

    ExecuteProcess = -1
    Debug.Print Err.Number & " " & Err.Description
   
End Function

Private Function TrimNull(item As String)

  'remove string before the terminating null(s)
   Dim pos As Integer
   
   pos = InStr(item, Chr$(0))
   
   If pos Then
      TrimNull = Left$(item, pos - 1)
   Else
      TrimNull = item
   End If
   
End Function


Private Sub Command1_Click()
    Call ExecuteProcess("c:\windows\web\tip.htm")
End Sub
0
 
PLavelleAuthor Commented:
That did it! Thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.