Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

CreateProcess not working

Posted on 2004-09-09
2
Medium Priority
?
753 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Suggested Courses

636 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