Results 1 to 17 of 17

Thread: how to kill a process?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2000
    Location
    I'm right here!
    Posts
    849

    Question how to kill a process?

    hello,

    how can kill a running process?
    do I need the name? the id?

    thanks
    Dekel C.

  2. #2
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951

    Re: how to kill a process?

    "kill store.exe" as an example

  3. #3
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: how to kill a process?

    The Kill statement will delete a file on disk, not exit a running process. What you can do is to call TerminateProcess, and you do need the Process handle to do that. Do you know which process you want to end?

  4. #4
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: how to kill a process?

    in a module:
    VB Code:
    1. LuidUDT As LUID
    2. Attributes As Long
    3. End Type
    4. Const TOKEN_ADJUST_PRIVILEGES = &H20
    5. Const TOKEN_QUERY = &H8
    6. Const SE_PRIVILEGE_ENABLED = &H2
    7. Const PROCESS_ALL_ACCESS = &H1F0FFF
    8. Type PROCESSENTRY32
    9. dwSize As Long
    10. cntUsage As Long
    11. th32ProcessID As Long
    12. th32DefaultHeapID As Long
    13. th32ModuleID As Long
    14. cntThreads As Long
    15. th32ParentProcessID As Long
    16. pcPriClassBase As Long
    17. dwFlags As Long
    18. szexeFile As String * MAX_PATH
    19. End Type
    20. Public Function KillApp(myName As String) As Boolean
    21. Const TH32CS_SNAPPROCESS As Long = 2&
    22. Const PROCESS_ALL_ACCESS = 0
    23. Dim uProcess As PROCESSENTRY32
    24. Dim rProcessFound As Long
    25. Dim hSnapshot As Long
    26. Dim szExename As String
    27. Dim exitCode As Long
    28. Dim myProcess As Long
    29. Dim AppKill As Boolean
    30. Dim appCount As Integer
    31. Dim I As Integer
    32. On Local Error GoTo Finish
    33. appCount = 0
    34.     uProcess.dwSize = Len(uProcess)
    35.     hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
    36.     rProcessFound = ProcessFirst(hSnapshot, uProcess)
    37.         Do While rProcessFound
    38.             I = InStr(1, uProcess.szexeFile, Chr(0))
    39.             szExename = LCase$(Left$(uProcess.szexeFile, I - 1))
    40.             If Right$(szExename, Len(myName)) = LCase$(myName) Then
    41.             KillApp = True
    42.             appCount = appCount + 1
    43.             myProcess = OpenProcess(PROCESS_ALL_ACCESS, False, uProcess.th32ProcessID)
    44.             If KillProcess(uProcess.th32ProcessID, 0) Then
    45.             End If
    46.             End If
    47.             rProcessFound = ProcessNext(hSnapshot, uProcess)
    48.         Loop
    49. Call CloseHandle(hSnapshot)
    50. Exit Function
    51. Finish:
    52. MsgBox "Error!"
    53. End Function
    54. Function KillProcess(ByVal hProcessID As Long, Optional ByVal exitCode As Long) As Boolean
    55. Dim hToken As Long
    56. Dim hProcess As Long
    57. Dim tp As TOKEN_PRIVILEGES
    58. If GetVersion() >= 0 Then
    59.         If OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, hToken) = 0 Then
    60.             GoTo CleanUp
    61.         End If
    62.     If LookupPrivilegeValue("", "SeDebugPrivilege", tp.LuidUDT) = 0 Then
    63.         GoTo CleanUp
    64.     End If
    65. tp.PrivilegeCount = 1
    66. tp.Attributes = SE_PRIVILEGE_ENABLED
    67.     If AdjustTokenPrivileges(hToken, False, tp, 0, ByVal 0&, ByVal 0&) = 0 Then
    68.         GoTo CleanUp
    69.     End If
    70. End If
    71. hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, hProcessID)
    72.     If hProcess Then
    73.         KillProcess = (TerminateProcess(hProcess, exitCode) <> 0)
    74.         CloseHandle hProcess
    75.     End If
    76.     If GetVersion() >= 0 Then
    77.         tp.Attributes = 0
    78.         AdjustTokenPrivileges hToken, False, tp, 0, ByVal 0&, ByVal 0&
    79. CleanUp:
    80.     If hToken Then CloseHandle hToken
    81.     End If
    82. End Function

    and call it like
    Private Sub Form_Load()
    KillApp ("project1.exe")
    End Sub
    Last edited by |2eM!x; May 18th, 2005 at 04:32 PM.

  5. #5
    Addicted Member
    Join Date
    Feb 2005
    Location
    Cleveland, Ohio
    Posts
    255

    Re: how to kill a process?

    Woah. Why go through all that?

    Send the WM_CLOSE API call to it and it closes instantly. No need for all that.

  6. #6
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: how to kill a process?

    WM_CLOSE will close a window, a process might have several windows or in fact no window at all (in which case you don't have anything to send the WM_CLOSE message to ).

  7. #7
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: how to kill a process?

    yep, good post joacim..

  8. #8
    Hyperactive Member
    Join Date
    Jul 2002
    Location
    WGTN, New Zealand
    Posts
    338

    Re: how to kill a process?

    Wow, now I know how hard it is to read unindented code.

  9. #9
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: how to kill a process?

    yeh i dunno what happened..it was formatted when i posted it

  10. #10
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: how to kill a process?

    Quote Originally Posted by |2eM!x
    yeh i dunno what happened..it was formatted when i posted it
    Sorry, but I find that a bit hard to believe unless you posted it without the [vbcode][/vbcode] tags first and edited it afterward to add them . But you can always edit the post again and paste the formatted code.

  11. #11
    Banned ThaRubby's Avatar
    Join Date
    Apr 2005
    Location
    127.0.0.1
    Posts
    356

    Re: how to kill a process?

    C:\Documents and Settings\Administrator>TaskKill /IM IExplore.exe /F
    SUCCESS: The process "iexplore.exe" with PID 2904 has been terminated.

    C:\Documents and Settings\Administrator>

    Xp and posibly win2k only though

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2000
    Location
    I'm right here!
    Posts
    849

    Re: how to kill a process?

    I have the process handle.
    how do I use TerminateProcess? how do I call it?

    how do I use the uExitCode ( GetExitCodeProcess )?
    what do I put there?

    Last edited by dekelc; May 19th, 2005 at 04:37 PM.
    Dekel C.

  13. #13
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: how to kill a process?

    Quote Originally Posted by dekelc
    I have the process handle.
    how do I use TerminateProcess? how do I call it?
    Do you have the process handle or the process ID, there is a difference. The process handle can be retrieved from the process ID by calling the OpenProcess API function to which you specify what kind of access you need for the handle, in your case you need the PROCESS_TERMINATE + the STANDARD_RIGHTS_REQUIRED access flag. When you have this handle you may call the TerminateProcess function.

    However you should know that calling the TerminateProcess function is an abnormal way of killing a process. It's equal to clicking the End Process button on the Processes tab in Task Manager. The reason it's abnormal is because TerminateProcess does kill the process and all of its threads but DLLs that are attached to the process will not be notified that the process has been terminated. You should only use this during extreme circumstances since the state of the global data maintained by DLLs may be comprimised.

    However if it's the Process ID you have you can use the following code:
    VB Code:
    1. Private Declare Function OpenProcess Lib "kernel32.dll" ( _
    2.  ByVal dwDesiredAccess As Long, _
    3.  ByVal bInheritHandle As Long, _
    4.  ByVal dwProcessId As Long) As Long
    5.  
    6. Private Declare Function TerminateProcess Lib "kernel32.dll" ( _
    7.  ByVal hProcess As Long, _
    8.  ByVal uExitCode As Long) As Long
    9.  
    10. Private Declare Function CloseHandle Lib "kernel32.dll" ( _
    11.  ByVal hObject As Long) As Long
    12.  
    13. Private Const STANDARD_RIGHTS_REQUIRED As Long = &HF0000&
    14. Private Const PROCESS_TERMINATE As Long = (&H1)
    15.  
    16. Public Function KillProcess(ByVal nProcessID As Long) As Boolean
    17.     Dim hProcess As Long
    18.     Const RIGHTS_FLAGS = STANDARD_RIGHTS_REQUIRED Or PROCESS_TERMINATE
    19.     hProcess = OpenProcess(RIGHTS_FLAGS, 0&, nProcessID)
    20.     If hProcess Then
    21.         If TerminateProcess(hProcess, 0&) Then
    22.             KillProcess = True
    23.         End If
    24.         Call CloseHandle(hProcess)
    25.     End If
    26. End Function

  14. #14
    Fanatic Member dark_shadow's Avatar
    Join Date
    Feb 2005
    Location
    Igloo
    Posts
    900

    Re: how to kill a process?

    here try this its a small thing u can use for more than one process it stops the process from running

    in a moduel putthis



    VB Code:
    1. Declare Function AdjustTokenPrivileges Lib "advapi32" (ByVal TokenHandle As Long, ByVal DisableAllPrivileges As Long, NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Long, PreviousState As Any, ReturnLength As Any) As Long
    2. Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    3. Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, lProcessID As Long) As Long
    4. Declare Function GetCurrentProcess Lib "kernel32" () As Long
    5. Declare Function GetVersion Lib "kernel32" () As Long
    6. Declare Function LookupPrivilegeValue Lib "advapi32" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LUID) As Long
    7. Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    8. Declare Function OpenProcessToken Lib "advapi32" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
    9. Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapshot As Long, ByRef lppe As PROCESSENTRY32) As Long
    10. Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
    11. Declare Function TerminateProcess Lib "kernel32" (ByVal ApphProcess As Long, ByVal uExitCode As Long) As Long
    12.  
    13. Public Const TH32CS_SNAPPROCESS As Long = 2&
    14. Public Const TOKEN_ADJUST_PRIVILEGES = &H20
    15. Public Const TOKEN_QUERY = &H8
    16. Public Const SE_PRIVILEGE_ENABLED = &H2
    17. Public Const PROCESS_ALL_ACCESS = &H1F0FFF
    18.  
    19. Type PROCESSENTRY32
    20.     dwSize As Long
    21.     cntUsage As Long
    22.     th32ProcessID As Long
    23.     th32DefaultHeapID As Long
    24.     th32ModuleID As Long
    25.     cntThreads As Long
    26.     th32ParentProcessID As Long
    27.     pcPriClassBase As Long
    28.     dwFlags As Long
    29.     szexeFile As String * 260
    30. End Type
    31.  
    32. Type LUID
    33.    lowpart As Long
    34.    highpart As Long
    35. End Type
    36.  
    37. Type TOKEN_PRIVILEGES
    38.     PrivilegeCount As Long
    39.     LuidUDT As LUID
    40.     Attributes As Long
    41. End Type
    42.  
    43. Public Function KillApp(myName As String) As Boolean
    44.  
    45. Const PROCESS_ALL_ACCESS = 0
    46. Dim uProcess As PROCESSENTRY32
    47. Dim iProcessFound As Long
    48. Dim hSnapshot As Long
    49. Dim szExename As String
    50. Dim myProcess As Long
    51. Dim appCount As Integer
    52. Dim i As Integer
    53. On Local Error GoTo Finish
    54.  
    55. uProcess.dwSize = Len(uProcess)
    56. hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
    57. iProcessFound = ProcessFirst(hSnapshot, uProcess)
    58. Do While iProcessFound
    59.     i = InStr(1, uProcess.szexeFile, Chr(0))
    60.     szExename = LCase$(Left$(uProcess.szexeFile, i - 1))
    61.     If Right$(szExename, Len(myName)) = LCase$(myName) Then
    62.         KillApp = True
    63.         appCount = appCount + 1
    64.         myProcess = OpenProcess(PROCESS_ALL_ACCESS, False, uProcess.th32ProcessID)
    65.         Call KillProcess(uProcess.th32ProcessID, 0)
    66.     End If
    67.     iProcessFound = ProcessNext(hSnapshot, uProcess)
    68. Loop
    69.  
    70. Call CloseHandle(hSnapshot)
    71.  
    72. Finish:
    73.     Exit Function
    74.  
    75. End Function
    76.  
    77. Public Function KillProcess(ByVal hProcessID As Long, ByVal exitCode As Long) As Boolean
    78.  
    79. Dim hToken As Long
    80. Dim hProcess As Long
    81. Dim tp As TOKEN_PRIVILEGES
    82.  
    83. If GetVersion() >= 0 Then
    84.     If OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, hToken) = 0 Then
    85.         GoTo CleanUp
    86.     End If
    87.     If LookupPrivilegeValue("", "SeDebugPrivilege", tp.LuidUDT) = 0 Then
    88.         GoTo CleanUp
    89.     End If
    90.     tp.PrivilegeCount = 1
    91.     tp.Attributes = SE_PRIVILEGE_ENABLED
    92.     If AdjustTokenPrivileges(hToken, False, tp, 0, ByVal 0&, ByVal 0&) = 0 Then
    93.         GoTo CleanUp
    94.     End If
    95. End If
    96.  
    97. hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, hProcessID)
    98. If hProcess Then
    99.     KillProcess = (TerminateProcess(hProcess, exitCode) <> 0)
    100.     CloseHandle hProcess
    101. End If
    102.  
    103. If GetVersion() >= 0 Then
    104.     tp.Attributes = 0
    105.     AdjustTokenPrivileges hToken, False, tp, 0, ByVal 0&, ByVal 0&
    106. End If
    107.  
    108. CleanUp:
    109.     If hToken Then CloseHandle hToken
    110.    
    111. End Function


    and in theform put a listbox,command button, and timer and
    VB Code:
    1. ' put in option explicit
    2.  
    3.  Dim xStr As String
    4. Dim boolQuick As Boolean

    'put in the timer
    VB Code:
    1. Dim i As Long
    2.  
    3. For i = 0 To lstProcess.ListCount - 1
    4.     Call KillApp(lstProcess.List(i))
    5. Next

    and in the command button
    VB Code:
    1. Timer1 = 1
    2. add_process 'used in my example may vary


    also you will need to make a sub or function that allows you to put in the process
    ex.
    private sub add_process()
    process.additem "iexplore.exe
    end sub

  15. #15

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2000
    Location
    I'm right here!
    Posts
    849

    Re: how to kill a process?

    I have the process handle and process ID,
    so what it the simplest way to kill it?

    Dekel C.

  16. #16
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: how to kill a process?

    Take a look at this.....
    Attached Files Attached Files
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  17. #17
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: how to kill a process?

    Quote Originally Posted by dekelc
    I have the process handle and process ID,
    so what it the simplest way to kill it?
    Hmmm... Did you even bothered to read this? If you have the process handle with the correct rights you may call the TerminateProcess. If you only have the ID you need to use that to open the running process with the correct rights flag, and then terminate it. Please let me know what it is you didn't understand.

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