Results 1 to 3 of 3

Thread: Kill runing process

  1. #1

    Thread Starter
    Addicted Member E-Link's Avatar
    Join Date
    Nov 2001
    Location
    INA
    Posts
    242

    Kill runing process

    hi ,
    how to determine , is an application running or not ?
    and if it is running , how to kill it ?

    thanks.

  2. #2
    Frenzied Member oh1mie's Avatar
    Join Date
    Sep 2001
    Location
    Finland
    Posts
    1,043
    Try this
    VB Code:
    1. Option Explicit
    2.  
    3. Public Const PROCESS_QUERY_INFORMATION = 1024
    4. Public Const PROCESS_VM_READ = 16
    5. Public Const MAX_PATH = 260
    6. Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
    7. Public Const SYNCHRONIZE = &H100000
    8. 'STANDARD_RIGHTS_REQUIRED Or SYNCHRONIZE Or &HFFF
    9. Public Const PROCESS_ALL_ACCESS = &H1F0FFF
    10. Public Const TH32CS_SNAPPROCESS = &H2&
    11. Public Const hNull = 0
    12.  
    13. Public Enum ePlatform
    14.   eWin95_98 = 1
    15.   eWinNT = 2
    16. End Enum
    17.  
    18. 'Usage
    19.  
    20. 'Then call the routine like this:
    21. 'If IsApplicationRunning("MyProgram.exe") Then
    22. '   MsgBox "Application is running."
    23. 'Else
    24. '   MsgBox "Application is not running."
    25. 'End If
    26.  
    27. Public Type PROCESSENTRY32
    28.   dwSize              As Long
    29.   cntUsage            As Long
    30.   th32ProcessID       As Long ' This process
    31.   th32DefaultHeapID   As Long
    32.   th32ModuleID        As Long ' Associated exe
    33.   cntThreads          As Long
    34.   th32ParentProcessID As Long ' This process's parent process
    35.   pcPriClassBase      As Long ' Base priority of process threads
    36.   dwFlags             As Long
    37.   szexeFile           As String * 260
    38. End Type
    39.  
    40. Private Declare Function ProcessFirst _
    41. Lib "kernel32" Alias "Process32First" (ByVal hSnapshot _
    42. As Long, uProcess As PROCESSENTRY32) As Long
    43.  
    44. Private Declare Function ProcessNext Lib "kernel32" _
    45. Alias "Process32Next" (ByVal hSnapshot As Long, _
    46. uProcess As PROCESSENTRY32) As Long
    47.  
    48. Private Declare Function CreateToolhelpSnapshot _
    49. Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal _
    50. lFlags As Long, lProcessID As Long) As Long
    51.  
    52. Private Declare Function TerminateProcess _
    53. Lib "kernel32" (ByVal hProcess As Long, ByVal _
    54. uExitCode As Long) As Long
    55.  
    56. Private Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
    57.  
    58. Public Declare Function Process32First Lib "kernel32" ( _
    59. ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
    60.  
    61. Public Declare Function Process32Next Lib "kernel32" ( _
    62. ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
    63.  
    64. Public Declare Function CloseHandle Lib "Kernel32.dll" _
    65. (ByVal Handle As Long) As Long
    66.  
    67. Public Declare Function OpenProcess Lib "Kernel32.dll" _
    68. (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, _
    69. ByVal dwProcId As Long) As Long
    70.  
    71. Public Declare Function EnumProcesses Lib "psapi.dll" _
    72. (ByRef lpidProcess As Long, ByVal cb As Long, _
    73. ByRef cbNeeded As Long) As Long
    74.  
    75. Public Declare Function GetModuleFileNameExA Lib "psapi.dll" _
    76. (ByVal hProcess As Long, ByVal hModule As Long, _
    77. ByVal strModuleName As String, ByVal nSize As Long) As Long
    78.  
    79. Public Declare Function EnumProcessModules Lib "psapi.dll" _
    80. (ByVal hProcess As Long, ByRef lphModule As Long, _
    81. ByVal cb As Long, ByRef cbNeeded As Long) As Long
    82.  
    83. Public Declare Function CreateToolhelp32Snapshot Lib "kernel32" ( _
    84. ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
    85.  
    86. Public gDBType As String
    87. Public Function IsApplicationRunning(pEXEName As String) As Boolean
    88.  
    89.     On Error Resume Next
    90.    
    91.     Select Case getVersion()
    92.     Case eWin95_98
    93.    
    94.         Dim lProc As Long
    95.         Dim strName As String
    96.         Dim hSnap As Long
    97.         Dim proc As PROCESSENTRY32
    98.        
    99.         hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
    100.         If hSnap = hNull Then Exit Function
    101.         proc.dwSize = Len(proc)
    102.         ' Iterate through the processes
    103.         lProc = Process32First(hSnap, proc)
    104.        
    105.         Do While lProc
    106.            strName = StrZToStr(proc.szexeFile)
    107.            If InStr(UCase(strName), UCase(pEXEName)) Then
    108.               IsApplicationRunning = True
    109.               Exit Function
    110.            End If
    111.            lProc = Process32Next(hSnap, proc)
    112.         Loop
    113.    
    114.     Case eWinNT
    115.    
    116.         Dim cb                 As Long
    117.         Dim cbNeeded           As Long
    118.         Dim NumElements        As Long
    119.         Dim lProcessIDs()      As Long
    120.         Dim cbNeeded2          As Long
    121.         Dim lNumElements2      As Long
    122.         Dim lModules(1 To 200) As Long
    123.         Dim lRet               As Long
    124.         Dim strModuleName      As String
    125.         Dim nSize              As Long
    126.         Dim hProcess           As Long
    127.         Dim i                  As Long
    128.        
    129.         'Get the array containing the process id's for each process object
    130.         cb = 8
    131.         cbNeeded = 96
    132.        
    133.         Do While cb <= cbNeeded
    134.            cb = cb * 2
    135.            ReDim lProcessIDs(cb / 4) As Long
    136.            lRet = EnumProcesses(lProcessIDs(1), cb, cbNeeded)
    137.         Loop
    138.        
    139.         NumElements = cbNeeded / 4
    140.        
    141.         For i = 1 To NumElements
    142.            
    143.             'Get a handle to the Process
    144.             hProcess = OpenProcess(PROCESS_QUERY_INFORMATION _
    145.             Or PROCESS_VM_READ, 0, lProcessIDs(i))
    146.            
    147.             'Got a Process handle
    148.             If hProcess <> 0 Then
    149.                'Get an array of the module handles for the specified process
    150.                lRet = EnumProcessModules(hProcess, lModules(1), 200, cbNeeded2)
    151.                'If the Module Array is retrieved, Get the ModuleFileName
    152.                If lRet <> 0 Then
    153.                   strModuleName = Space(MAX_PATH)
    154.                   nSize = 500
    155.                   lRet = GetModuleFileNameExA(hProcess, lModules(1), strModuleName, nSize)
    156.                   strModuleName = Left(strModuleName, lRet)
    157.                   'Check for the client application running
    158.                   If InStr(UCase(strModuleName), UCase(pEXEName)) Then
    159.                       IsApplicationRunning = True
    160.                       Exit Function
    161.                   End If
    162.                   'List1.AddItem Left(strModuleName, lRet)
    163.                 End If
    164.            End If
    165.            'Close the handle to the process
    166.            lRet = CloseHandle(hProcess)
    167.            
    168.         Next
    169.     End Select
    170.    
    171. End Function
    172. Private Function StrZToStr(pString As String) As String
    173.     StrZToStr = Left$(pString, Len(pString) - 1)
    174. End Function
    175. Private Function KillApp(myName As String) As Boolean
    176.  
    177.     Const PROCESS_ALL_ACCESS = 0
    178.     Dim uProcess As PROCESSENTRY32
    179.  
    180.     Dim rProcessFound As Long
    181.     Dim hSnapshot As Long
    182.     Dim szExename As String
    183.     Dim exitCode As Long
    184.     Dim myProcess As Long
    185.     Dim AppKill As Boolean
    186.     Dim appCount As Integer
    187.     Dim i As Integer
    188.  
    189.     On Local Error GoTo Finish
    190.    
    191.     appCount = 0
    192.  
    193.     Const TH32CS_SNAPPROCESS As Long = 2&
    194.  
    195.     uProcess.dwSize = Len(uProcess)
    196.     hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
    197.     rProcessFound = ProcessFirst(hSnapshot, uProcess)
    198.  
    199.     Do While rProcessFound
    200.        
    201.        i = InStr(1, uProcess.szexeFile, Chr(0))
    202.        szExename = LCase(Left(uProcess.szexeFile, i - 1))
    203.        
    204.        If Right(szExename, Len(myName)) = LCase$(myName) Then
    205.           KillApp = True
    206.           appCount = appCount + 1
    207.           myProcess = OpenProcess(PROCESS_ALL_ACCESS, False, uProcess.th32ProcessID)
    208.           AppKill = TerminateProcess(myProcess, exitCode)
    209.           Call CloseHandle(myProcess)
    210.        End If
    211.    
    212.        rProcessFound = ProcessNext(hSnapshot, uProcess)
    213.    
    214.     Loop
    215.  
    216.     Call CloseHandle(hSnapshot)
    217.  
    218. Finish:
    219.  
    220. End Function
    oh1mie/Vic


  3. #3

    Thread Starter
    Addicted Member E-Link's Avatar
    Join Date
    Nov 2001
    Location
    INA
    Posts
    242
    thanks oh1mie , that's exactly i want.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width