Solved

CreateProcess not working

Posted on 2004-09-09
2
735 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:PLavelle
2 Comments
 
LVL 32

Accepted Solution

by:
Erick37 earned 500 total points
ID: 12023504
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
 

Author Comment

by:PLavelle
ID: 12026034
That did it! Thanks.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now