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!

determine if a process is running

Status
Not open for further replies.

keithf01

Programmer
Jun 24, 2002
13
0
0
US
Can anyone supply some VB sample code or know of an API that can determine if a certain process is currently running?

I am kicking off a remote exec session from a mainframe job using Entire X, which in turn starts a client based MES program, logs in and starts a specified job which queries a database and starts lots in the MES.

I do not want to start the MES program multiple times.

Thanks,
Keith
 
If you know what the exe name of the process is this code will work for you:

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

'** WaitForSingleObject Return Values.
Public Const WAIT_FAILED = &HFFFFFFFF
Public Const WAIT_OBJECT_0 = &H0
Public Const WAIT_TIMEOUT = &H102

'** API declarations.
Public Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long

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 Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode 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

you send this function your exe name and if it is running it will return the Process ID of that instance. If it is not running the return value will be 0
 
If App.PrevInstance = True Then
'Application is already running.
'<--- Insert your code here...kill the app.
End If
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top