The following code checks if a particular EXE file is currently running.
VB Code:
  1. Private Declare Function EnumProcesses Lib "PSAPI.DLL" ( _
  2.  idProcess As Long, _
  3.  ByVal cBytes As Long, _
  4.  cbNeeded As Long) As Long
  5.  
  6. Private Declare Function EnumProcessModules Lib "PSAPI.DLL" ( _
  7.  ByVal hProcess As Long, _
  8.  hModule As Long, _
  9.  ByVal cb As Long, _
  10.  cbNeeded As Long) As Long
  11.  
  12. Private Declare Function GetModuleBaseName _
  13.  Lib "PSAPI.DLL" Alias "GetModuleBaseNameA" ( _
  14.  ByVal hProcess As Long, _
  15.  ByVal hModule As Long, _
  16.  ByVal lpBaseName As String, _
  17.  ByVal nSize As Long) As Long
  18.  
  19. Private Declare Function GetModuleFileNameEx _
  20.  Lib "PSAPI.DLL" Alias "GetModuleFileNameExA" ( _
  21.  ByVal hProcess As Long, _
  22.  ByVal hModule As Long, _
  23.  ByVal lpFilename As String, _
  24.  ByVal nSize As Long) As Long
  25.  
  26. Private Declare Function OpenProcess Lib "kernel32" ( _
  27.  ByVal dwDesiredAccess As Long, _
  28.  ByVal bInheritHandle As Long, _
  29.  ByVal dwProcessId As Long) As Long
  30.  
  31. Private Declare Function CloseHandle Lib "kernel32" ( _
  32.  ByVal hObject As Long) As Long
  33.  
  34. Private Const MAX_PATH = 260&
  35. Private Const STANDARD_RIGHTS_REQUIRED = &HF0000
  36. Private Const SYNCHRONIZE = &H100000
  37. Private Const PROCESS_VM_READ = &H10&
  38. Private Const PROCESS_QUERY_INFORMATION = &H400&
  39. Private Const PROCESS_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED Or _
  40.  SYNCHRONIZE Or &HFFF&
  41.  
  42. Private Function TrimNull(ByVal sStr As String) As String
  43.     Dim nPos As Long
  44.     nPos = InStr(sStr, vbNullChar)
  45.     If nPos Then
  46.         TrimNull = Left$(sStr, nPos - 1)
  47.     Else
  48.         TrimNull = sStr
  49.     End If
  50. End Function
  51.  
  52. Public Function IsProcessRunning(ByVal sEXEName As String) As Boolean
  53.     Dim i As Integer, j As Integer, l As Long
  54.     Dim nNeeded As Long
  55.     Dim hEXE As Long
  56.     Dim hProcess As Long
  57.     Dim lret As Long
  58.     Dim nProcesses As Long
  59.     Dim nProcessIDs() As Long
  60.     Dim sEXENames() As String
  61.     Dim sFQEXENames() As String
  62.    
  63.     If LCase$(Right$(sEXEName, 4)) <> ".exe" Then
  64.         sEXEName = sEXEName & ".exe"
  65.     End If
  66.     ' First get the array of process IDs
  67.     ' Initial guess on the number of running processes
  68.     nProcesses = 25
  69.     Do
  70.         ' Size array
  71.         ReDim nProcessIDs(1 To nProcesses)
  72.         ' Enumerate
  73.         lret = EnumProcesses(nProcessIDs(1), nProcesses * 4, nNeeded)
  74.         If lret = 0 Then
  75.             Exit Function
  76.         End If
  77.         ' Compare needed bytes with array size in bytes.
  78.         ' If less, then we got them all.
  79.         If nNeeded < nProcesses * 4 Then
  80.             Exit Do
  81.         Else
  82.             nProcesses = nProcesses * 2
  83.         End If
  84.     Loop
  85.    
  86.     nProcesses = nNeeded / 4
  87.     ReDim Preserve nProcessIDs(1 To nProcesses)
  88.     ReDim sEXENames(1 To nProcesses)
  89.     ReDim sFQEXENames(1 To nProcesses)
  90.    
  91.     'Get EXE names
  92.     For i = 1 To nProcesses
  93.         ' Use OpenProcess to get a handle to each process
  94.         hProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0&, nProcessIDs(i))
  95.         ' Watch out for special processes
  96.         Select Case nProcessIDs(i)
  97.             Case 0   ' System Idle Process
  98.                 sEXENames(i) = "Idle Process"
  99.                 sFQEXENames(i) = "Idle Process"
  100.             Case 2
  101.                 sEXENames(i) = "System"
  102.                 sFQEXENames(i) = "System"
  103.             Case 28
  104.                 sEXENames(i) = "csrss.exe"
  105.                 sFQEXENames(i) = "csrss.exe"
  106.         End Select
  107.         ' If error skip this process
  108.         If hProcess Then
  109.             ' Now get the handle of the first module
  110.             ' in this process, since first module is EXE
  111.             hEXE = 0
  112.             lret = EnumProcessModules(hProcess, hEXE, 4&, nNeeded)
  113.             If hEXE Then
  114.                 ' Get the name of the module
  115.                 sEXENames(i) = String$(MAX_PATH, 0)
  116.                 lret = GetModuleBaseName(hProcess, hEXE, sEXENames(i), Len(sEXENames(i)))
  117.                 sEXENames(i) = TrimNull(sEXENames(i))
  118.                
  119.                 ' Get full path name
  120.                 sFQEXENames(i) = String$(MAX_PATH, 0)
  121.                 lret = GetModuleFileNameEx(hProcess, hEXE, sFQEXENames(i), Len(sFQEXENames(i)))
  122.                 sFQEXENames(i) = TrimNull(sFQEXENames(i))
  123.             End If
  124.         End If
  125.         ' Close handle
  126.         lret = CloseHandle(hProcess)
  127.     Next
  128.    
  129.     ' Check for match
  130.     For i = 1 To nProcesses
  131.         If LCase$(sFQEXENames(i)) = LCase$(sEXEName) Then
  132.             IsProcessRunning = True
  133.             Exit Function
  134.         End If
  135.     Next
  136.     For i = 1 To nProcesses
  137.         If LCase$(sEXENames(i)) = LCase$(sEXEName) Then
  138.             IsProcessRunning = True
  139.             Exit Function
  140.         End If
  141.     Next
  142. End Function
Just call the IsProcessRunning function and pass either a fully qualified path to an EXE file or just the name of the exe.
VB Code:
  1. If IsProcessRunning("c:\Winnt\Notepad.exe") Then
  2.     MsgBox "Notepad is running"
  3. End If
  4. 'or without the path:
  5. If IsProcessRunning("Notepad.exe") Then
  6.     MsgBox "Notepad is running"
  7. End If