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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

see if program is running using handle

Status
Not open for further replies.

ctlin

Technical User
Apr 17, 2002
77
US
i have the handle of a program that i shelled and am controlling with my VB gui. sometimes this other non VB program exits of its own accord. is there a way i check in every once in a while to see if a program is still running? i have the handle of the program to work with. there may be other instances of the program running so i can't use findWindow("program.exe"). also, i don't think WaitForSingleObject will work either, because i need to be running things in the gui while this program is running, not waiting for it to exit before i run more code.
thanks
 
Here is code that I use to return the PID of a running exe. It uses Get

Code:
Public Const MAX_PATH As Integer = 260

'** OpenHandle Constants.
Public Const PROCESS_QUERY_INFORMATION = 1024
Public Const PROCESS_VM_READ = 16
Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
Public Const SYNCHRONIZE = &H100000

'** API declarations.
Public Declare Sub CloseHandle Lib "kernel32.dll" (ByVal hPass As Long)

Public Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long

Public Declare Function EnumProcesses Lib "psapi.dll" (ByRef lpidProcess As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long

Public Declare Function GetModuleBaseNameA Lib "psapi.dll" (ByVal lngProcessHandle As Long, ByVal lngModuleHandle As Long, ByVal strModuleName As String, ByVal lngSize As Long) As Long

Public Declare Function EnumProcessModules Lib "psapi.dll" (ByVal hProcess As Long, ByRef lphModule As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long

Public Function GetProcessHandle(strName As String) As Long
    

    'This Function accepts the name of the EXE (i.e. SomeProgram.exe) and scans
    'the process list for it. When it is found, the Process ID is returned. If
    'it is not found, then the function returns 0.

    Dim lngProcessID()  As Long
    Dim lngCB As Long
    Dim lngCBNeeded As Long
    Dim lngRetCode As Long
    Dim lngLoop As Long
    Dim lngProcessHandle As Long
    Dim strModuleName As String
    Dim strTemp() As String
    Dim lngModules(1 To 200) As Long
    Dim lngCbNeeded2 As Long
    Dim intTemp As Integer
    
    '** Get the current list of running Processes IDs
    '******************************************************************************
    lngCB = 8
    lngCBNeeded = 96
    Do While lngCB <= lngCBNeeded
        lngCB = lngCB * 2
        ReDim lngProcessIDs(lngCB / 4) As Long
        lngRetCode = EnumProcesses(lngProcessIDs(1), lngCB, lngCBNeeded)
        
        If lngRetCode = 0 Then
            MsgBox Err.LastDllError
            Exit Do
        End If
    Loop
    '******************************************************************************


    '** Get the name of the Base Module in each Process Space.
    '** NOTE : If you wanted to, you could also use the GetModuleBaseNameA API
    '**        to get every DLL and OCX loaded in the process space.
    '******************************************************************************
   
    For lngLoop = 1 To lngCBNeeded / 4
       'Get a handle to the Process
       lngProcessHandle = OpenProcess(PROCESS_QUERY_INFORMATION _
          Or PROCESS_VM_READ, 0, lngProcessIDs(lngLoop))
       'Got a Process handle
       If lngProcessHandle <> 0 Then
           'Get an array of the module handles for the specified
           'process
           lngRetCode = EnumProcessModules(lngProcessHandle, lngModules(1), 200, lngCbNeeded2)
           'If the Module Array is retrieved, Get the ModuleFileName
           If lngRetCode <> 0 Then
              strModuleName = Space(MAX_PATH)
              lngRetCode = GetModuleBaseNameA(lngProcessHandle, lngModules(1), strModuleName, 500)
              ReDim Preserve strTemp(intTemp)
              strTemp(intTemp) = Trim$(strModuleName)
              
            
              '** Check to see if the current process is the target process.
              If UCase$(Trim$(strModuleName)) = UCase$(Trim$(strName & Chr$(0))) Then
                GetProcessHandle = lngProcessIDs(lngLoop)
                Exit For
              End If
              
              intTemp = intTemp + 1
           End If
       End If
    Next lngLoop
    '******************************************************************************
End Function
 
thanks,

i ended up using:

Public Sub checkExit()
temphandle = FindWindow(vbNullString, windowstring)

If temphandle = 0 Then
dummyvar = MsgBox(&quot;Fortran program died&quot;, vbOKOnly + vbCritical, &quot;DOH!&quot;)
End If

End Sub

i'll just stop multiple instances of the prog.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top