Solved

CreateProcess not working

Posted on 2004-09-09
2
746 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

738 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