• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 125
  • Last Modified:

starting new program from another program

I have to start another program from VB program and wait until it is finished. Shell command just runs it and I can't tell when its done and the exit codes. How can I test wheter it has finished execution or is it still running?
0
BobSamonik
Asked:
BobSamonik
1 Solution
 
fibdevCommented:
I had a similar problem and someone on the ee gave me this (so I can't take credit for the code :)

Put this into a module:
=======================================

    Public Const INFINITE = -1&

    Private Declare Function OpenProcess Lib "kernel32" (ByVal _
        dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
        ByVal dwProcessID As Long) As Long

   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 CreateProcessA Lib "kernel32" (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 Declare Function GetExitCodeProcess Lib "kernel32" _
      (ByVal hProcess As Long, lpExitCode As Long) As Long

   Private Const NORMAL_PRIORITY_CLASS = &H20&
     
  Public Function ExecCmd(cmdline$)
      Dim proc As PROCESS_INFORMATION
      Dim start As STARTUPINFO

      ' Initialize the STARTUPINFO structure:
      start.cb = Len(start)

      ' Start the shelled application:
      ret& = CreateProcessA(0&, cmdline$, 0&, 0&, 1&, _
         NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)


      ' Wait for the shelled application to finish:
         ret& = WaitForSingleObject(proc.hProcess, INFINITE)
         Call GetExitCodeProcess(proc.hProcess, ret&)
         Call CloseHandle(proc.hThread)
         Call CloseHandle(proc.hProcess)
         ExecCmd = ret&
    End Function

=====================================

This will shell notepad and wait for the shelled process to finish before closing.


Sub Command1_Click()

Dim comPath As String
comPath = "c:\windows\notepad.exe"
ExecCmd (Environ("Shell") & comPath)
End

End Sub
0
 
USHASHICommented:
U can put a timer loop and keep checking if shell has finished execution. if it has finished exit from time loop.
 
0
 
BobSamonikAuthor Commented:
How can I check if shell has finished execution?
0
 
T3000Commented:

Use the CreateProcess(sNullStr, sCmdLine, ByVal pNull, ByVal pNull,_APITRUE, 0&, pNull,sInitDir, start, proc) to create process and run it, then do a loop

Do
GetExitCodeProcess proc.hprocess, iExit
DoEvents
Loop Until iExit

Above sample code are from the book "Hardcore Visual Basic" publish by microsoft press.
ISBN 1-57231-422-2
0

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

Tackle projects and never again get stuck behind a technical roadblock.
Join Now