Solved

CreateProcess not working

Posted on 2004-09-09
2
744 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

749 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