Results 1 to 2 of 2

Thread: Launch new process and wait for it... example in vb-world.net seems not to compile

  1. #1
    Guest

    Unhappy

    Launch new process and wait for it... example in vb-world.net seems not to compile

    1st of all, I'm a beginner so please bear with me incase there's something I'm missing completely...

    I wish to launch a process from within my program written in vb6. I am unable to use the code shown when searching for 'shell' on vb-world.com

    This uses the API process functions to launch and wait for it to return. It does launch, but does not wait for the process to terminate, leaving me no better off than 'Shell'.

    And.. I can only get the example to work if I place the declarations in the module declaration area rather than the frmMain declaration area, and even then I have to replace the private cast with public to allow my code to 'see' these functions from elsewhere. If the declarations are placed in my frmMain code, I am presented with an error such as 'cannot use private enums in a public call' or somesuch.

    Help!


    Regards

    Gary Thomlinson

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    I've posted this code a few times now:
    Code:
    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 WaitForSingleObject _
     Lib "kernel32" ( _
     ByVal hHandle As Long, _
     ByVal dwMilliseconds As Long) As Long
    
    Private Declare Function CreateProcess _
     Lib "kernel32" Alias "CreateProcessA" ( _
     ByVal lpApplicationName As Long, _
     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 Const NORMAL_PRIORITY_CLASS = &H20&
    Private Const INFINITE = -1&
    Private Const STARTF_USESHOWWINDOW = &H1
    
    Public Sub ShellAndWait(sCmd As String, Optional WindowStyle As VbAppWinStyle = vbNormalFocus)
        Dim udtStart As STARTUPINFO
        Dim udtProc As PROCESS_INFORMATION
        
        udtStart.cb = Len(udtStart)
        udtStart.dwFlags = STARTF_USESHOWWINDOW
        udtStart.wShowWindow = WindowStyle
        CreateProcess 0&, sCmd, 0&, 0&, 1&, _
         NORMAL_PRIORITY_CLASS, 0&, 0&, udtStart, udtProc
        WaitForSingleObject udtProc.hProcess, INFINITE
        CloseHandle udtProc.hProcess
        AppActivate Me.Caption
    End Sub
    Just call the ShellAndWait with the command line to start the application and optionally the window style.

    Good luck!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width