Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to detect if cmd.exe is running?

Status
Not open for further replies.

dreammaker888

Programmer
Jul 31, 2007
44
US
I have a vb routine that involves calling a few Shell commands. The problem is they will open one after another just like they are open simultaneously.

I want them to open one after the previous one is completed.

What is the code to detect if the previous cmd.exe is still open?

Thanks.

dmk
 
Nothing concise but you might look at:
Code:
Option Explicit

Private Const INFINITE = &HFFFF
Private Const SYNCHRONIZE = &H100000
Private Const PROCESS_QUERY_INFORMATION = &H400
  
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 Declare Function OpenProcess Lib "kernel32" ( _
    ByVal dwDesiredAccess As Long, _
    ByVal bInheritHandle As Long, _
    ByVal dwProcessId As Long) As Long
  
Private Declare Function WaitForSingleObject Lib "kernel32" ( _
    ByVal hHandle As Long, _
    ByVal dwMilliseconds As Long) As Long
    
Private Function SyncShell( _
    ByVal PathName As String, _
    ByVal WindowStyle As VbAppWinStyle) As Long
    'Shell and wait.  Return exit code result, raise an
    'exception on any error.
    Dim lngPid As Long
    Dim lngHandle As Long
    Dim lngExitCode As Long
                 
    lngPid = Shell(PathName, WindowStyle)
    If lngPid <> 0 Then
        lngHandle = OpenProcess(SYNCHRONIZE _
                             Or PROCESS_QUERY_INFORMATION, 0, lngPid)
        If lngHandle <> 0 Then
            WaitForSingleObject lngHandle, INFINITE
            If GetExitCodeProcess(lngHandle, lngExitCode) <> 0 Then
                SyncShell = lngExitCode
                CloseHandle lngHandle
            Else
                CloseHandle lngHandle
                Err.Raise &H8004AA00, "SyncShell", _
                          "Failed to retrieve exit code, error " _
                        & CStr(Err.LastDllError)
            End If
        Else
            Err.Raise &H8004AA01, "SyncShell", _
                      "Failed to open child process"
        End If
    Else
        Err.Raise &H8004AA02, "SyncShell", _
                  "Failed to Shell child process"
    End If
End Function

Private Sub Command1_Click()
    Dim lngReturn As Long
    
    On Error Resume Next
    lngReturn = SyncShell("wscript msgbox.vbs", vbNormalFocus)
    If Err.Number <> 0 Then
        MsgBox "Failed to Shell msgbox.vbs: " & vbNewLine _
             & Err.Description
    Else
        MsgBox "Success, return code: " & CStr(lngReturn)
    End If
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top