Try this:


VB Code:
  1. 'Author: Serge
  2. 'Origin: [url]http://forums.vb-world.net/showthre...?threadid=16450[/url]
  3. 'Purpose: Determine if an app is running
  4. 'Version: VB5+
  5.  
  6.  
  7. Private Declare Function GetWindowsDirectory Lib "kernel32" _
  8. Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal _
  9. nSize As Long) As Long
  10.  
  11. Private Declare Function Process32First Lib "kernel32" ( _
  12.          ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
  13.  
  14. Private Declare Function Process32Next Lib "kernel32" ( _
  15.    ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
  16.  
  17. Private Declare Function CloseHandle Lib "Kernel32.dll" _
  18.    (ByVal Handle As Long) As Long
  19.  
  20. Private Declare Function OpenProcess Lib "Kernel32.dll" _
  21.   (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, _
  22.       ByVal dwProcId As Long) As Long
  23.  
  24. Private Declare Function EnumProcesses Lib "psapi.dll" _
  25.    (ByRef lpidProcess As Long, ByVal cb As Long, _
  26.       ByRef cbNeeded As Long) As Long
  27.  
  28. Private Declare Function GetModuleFileNameExA Lib "psapi.dll" _
  29.    (ByVal hProcess As Long, ByVal hModule As Long, _
  30.       ByVal strModuleName As String, ByVal nSize As Long) As Long
  31.  
  32. Private Declare Function EnumProcessModules Lib "psapi.dll" _
  33.    (ByVal hProcess As Long, ByRef lphModule As Long, _
  34.       ByVal cb As Long, ByRef cbNeeded As Long) As Long
  35.  
  36. Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" ( _
  37.    ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
  38.  
  39. Private Declare Function GetVersionExA Lib "kernel32" _
  40.    (lpVersionInformation As OSVERSIONINFO) As Integer
  41.  
  42. Private Type PROCESSENTRY32
  43.    dwSize As Long
  44.    cntUsage As Long
  45.    th32ProcessID As Long           ' This process
  46.    th32DefaultHeapID As Long
  47.    th32ModuleID As Long            ' Associated exe
  48.    cntThreads As Long
  49.    th32ParentProcessID As Long     ' This process's parent process
  50.    pcPriClassBase As Long          ' Base priority of process threads
  51.    dwFlags As Long
  52.    szExeFile As String * 260       ' MAX_PATH
  53. End Type
  54.  
  55. Private Type OSVERSIONINFO
  56.    dwOSVersionInfoSize As Long
  57.    dwMajorVersion As Long
  58.    dwMinorVersion As Long
  59.    dwBuildNumber As Long
  60.    dwPlatformId As Long           '1 = Windows 95.
  61.                                   '2 = Windows NT
  62.  
  63.    szCSDVersion As String * 128
  64. End Type
  65.  
  66. Private Const PROCESS_QUERY_INFORMATION = 1024
  67. Private Const PROCESS_VM_READ = 16
  68. Private Const MAX_PATH = 260
  69. Private Const STANDARD_RIGHTS_REQUIRED = &HF0000
  70. Private Const SYNCHRONIZE = &H100000
  71. 'STANDARD_RIGHTS_REQUIRED Or SYNCHRONIZE Or &HFFF
  72. Private Const PROCESS_ALL_ACCESS = &H1F0FFF
  73. Private Const TH32CS_SNAPPROCESS = &H2&
  74. Private Const hNull = 0
  75.  
  76. Private Enum ePlatform
  77.     eWin95_98 = 1
  78.     eWinNT = 2
  79. End Enum
  80.  
  81. Private gDBType As String
  82.  
  83. Private Function IsApplicationRunning(pEXEName As String) As Boolean
  84.  
  85.     On Error Resume Next
  86.    
  87.     Select Case getVersion()
  88.         Case eWin95_98
  89.             Dim lProc As Long, strName As String
  90.             Dim hSnap As Long, proc As PROCESSENTRY32
  91.            
  92.             hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
  93.             If hSnap = hNull Then Exit Function
  94.             proc.dwSize = Len(proc)
  95.             ' Iterate through the processes
  96.             lProc = Process32First(hSnap, proc)
  97.             Do While lProc
  98.                 strName = StrZToStr(proc.szExeFile)
  99.                 If InStr(UCase(strName), UCase(pEXEName)) Then
  100.                     IsApplicationRunning = True
  101.                     Exit Function
  102.                 End If
  103.                 lProc = Process32Next(hSnap, proc)
  104.             Loop
  105.     Case eWinNT
  106.         Dim cb As Long
  107.         Dim cbNeeded As Long
  108.         Dim NumElements As Long
  109.         Dim lProcessIDs() As Long
  110.         Dim cbNeeded2 As Long
  111.         Dim lNumElements2 As Long
  112.         Dim lModules(1 To 200) As Long
  113.         Dim lRet As Long
  114.         Dim strModuleName As String
  115.         Dim nSize As Long
  116.         Dim hProcess As Long
  117.         Dim i As Long
  118.        
  119.         'Get the array containing the process id's for each process object
  120.         cb = 8
  121.         cbNeeded = 96
  122.         Do While cb <= cbNeeded
  123.             cb = cb * 2
  124.             ReDim lProcessIDs(cb / 4) As Long
  125.             lRet = EnumProcesses(lProcessIDs(1), cb, cbNeeded)
  126.         Loop
  127.         NumElements = cbNeeded / 4
  128.         For i = 1 To NumElements
  129.             'Get a handle to the Process
  130.             hProcess = OpenProcess(PROCESS_QUERY_INFORMATION _
  131.             Or PROCESS_VM_READ, 0, lProcessIDs(i))
  132.             'Got a Process handle
  133.             If hProcess <> 0 Then
  134.                 'Get an array of the module handles for the specified
  135.                 'process
  136.                 lRet = EnumProcessModules(hProcess, lModules(1), 200, _
  137.                         cbNeeded2)
  138.                 'If the Module Array is retrieved, Get the ModuleFileName
  139.                 If lRet <> 0 Then
  140.                     strModuleName = Space(MAX_PATH)
  141.                     nSize = 500
  142.                     lRet = GetModuleFileNameExA(hProcess, lModules(1), _
  143.                     strModuleName, nSize)
  144.                     strModuleName = Left(strModuleName, lRet)
  145.                     'Check for the client application running
  146.                     If InStr(UCase(strModuleName), UCase(pEXEName)) Then
  147.                         IsApplicationRunning = True
  148.                         Exit Function
  149.                     End If
  150.                     'List1.AddItem Left(strModuleName, lRet)
  151.                 End If
  152.             End If
  153.             'Close the handle to the process
  154.             lRet = CloseHandle(hProcess)
  155.         Next
  156.     End Select
  157. End Function
  158.  
  159. Function StrZToStr(pString As String) As String
  160.    StrZToStr = Left$(pString, Len(pString) - 1)
  161. End Function
  162.  
  163. Private Function getVersion() As ePlatform
  164.    Dim osinfo As OSVERSIONINFO
  165.    Dim lRetVal As Integer
  166.    
  167.    osinfo.dwOSVersionInfoSize = 148
  168.    osinfo.szCSDVersion = Space$(128)
  169.    lRetVal = GetVersionExA(osinfo)
  170.    getVersion = osinfo.dwPlatformId
  171. End Function
  172.  
  173.  
  174. Private Sub Command1_Click()
  175.     If IsApplicationRunning("Calc.exe") Then
  176.          MsgBox "Application is running."
  177.     Else
  178.          MsgBox "Application is not running."
  179.     End If
  180. End Sub