hello,
how can kill a running process?
do I need the name? the id?
thanks
Printable View
hello,
how can kill a running process?
do I need the name? the id?
thanks
"kill store.exe" as an example
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?
in a module:
VB Code:
LuidUDT As LUID Attributes As Long End Type Const TOKEN_ADJUST_PRIVILEGES = &H20 Const TOKEN_QUERY = &H8 Const SE_PRIVILEGE_ENABLED = &H2 Const PROCESS_ALL_ACCESS = &H1F0FFF Type PROCESSENTRY32 dwSize As Long cntUsage As Long th32ProcessID As Long th32DefaultHeapID As Long th32ModuleID As Long cntThreads As Long th32ParentProcessID As Long pcPriClassBase As Long dwFlags As Long szexeFile As String * MAX_PATH End Type Public Function KillApp(myName As String) As Boolean Const TH32CS_SNAPPROCESS As Long = 2& Const PROCESS_ALL_ACCESS = 0 Dim uProcess As PROCESSENTRY32 Dim rProcessFound As Long Dim hSnapshot As Long Dim szExename As String Dim exitCode As Long Dim myProcess As Long Dim AppKill As Boolean Dim appCount As Integer Dim I As Integer On Local Error GoTo Finish appCount = 0 uProcess.dwSize = Len(uProcess) hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&) rProcessFound = ProcessFirst(hSnapshot, uProcess) Do While rProcessFound I = InStr(1, uProcess.szexeFile, Chr(0)) szExename = LCase$(Left$(uProcess.szexeFile, I - 1)) If Right$(szExename, Len(myName)) = LCase$(myName) Then KillApp = True appCount = appCount + 1 myProcess = OpenProcess(PROCESS_ALL_ACCESS, False, uProcess.th32ProcessID) If KillProcess(uProcess.th32ProcessID, 0) Then End If End If rProcessFound = ProcessNext(hSnapshot, uProcess) Loop Call CloseHandle(hSnapshot) Exit Function Finish: MsgBox "Error!" End Function Function KillProcess(ByVal hProcessID As Long, Optional ByVal exitCode As Long) As Boolean Dim hToken As Long Dim hProcess As Long Dim tp As TOKEN_PRIVILEGES If GetVersion() >= 0 Then If OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, hToken) = 0 Then GoTo CleanUp End If If LookupPrivilegeValue("", "SeDebugPrivilege", tp.LuidUDT) = 0 Then GoTo CleanUp End If tp.PrivilegeCount = 1 tp.Attributes = SE_PRIVILEGE_ENABLED If AdjustTokenPrivileges(hToken, False, tp, 0, ByVal 0&, ByVal 0&) = 0 Then GoTo CleanUp End If End If hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, hProcessID) If hProcess Then KillProcess = (TerminateProcess(hProcess, exitCode) <> 0) CloseHandle hProcess End If If GetVersion() >= 0 Then tp.Attributes = 0 AdjustTokenPrivileges hToken, False, tp, 0, ByVal 0&, ByVal 0& CleanUp: If hToken Then CloseHandle hToken End If End Function
and call it like
Private Sub Form_Load()
KillApp ("project1.exe")
End Sub
Woah. Why go through all that?
Send the WM_CLOSE API call to it and it closes instantly. No need for all that.
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 :)).
yep, good post joacim..
Wow, now I know how hard it is to read unindented code. ;)
yeh i dunno what happened..it was formatted when i posted it :D
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.Quote:
Originally Posted by |2eM!x
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
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?
:wave:
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.Quote:
Originally Posted by dekelc
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:
Private Declare Function OpenProcess Lib "kernel32.dll" ( _ ByVal dwDesiredAccess As Long, _ ByVal bInheritHandle As Long, _ ByVal dwProcessId As Long) As Long Private Declare Function TerminateProcess Lib "kernel32.dll" ( _ ByVal hProcess As Long, _ ByVal uExitCode As Long) As Long Private Declare Function CloseHandle Lib "kernel32.dll" ( _ ByVal hObject As Long) As Long Private Const STANDARD_RIGHTS_REQUIRED As Long = &HF0000& Private Const PROCESS_TERMINATE As Long = (&H1) Public Function KillProcess(ByVal nProcessID As Long) As Boolean Dim hProcess As Long Const RIGHTS_FLAGS = STANDARD_RIGHTS_REQUIRED Or PROCESS_TERMINATE hProcess = OpenProcess(RIGHTS_FLAGS, 0&, nProcessID) If hProcess Then If TerminateProcess(hProcess, 0&) Then KillProcess = True End If Call CloseHandle(hProcess) End If End Function
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:
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 Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, lProcessID As Long) As Long Declare Function GetCurrentProcess Lib "kernel32" () As Long Declare Function GetVersion Lib "kernel32" () As Long Declare Function LookupPrivilegeValue Lib "advapi32" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LUID) As Long Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long Declare Function OpenProcessToken Lib "advapi32" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapshot As Long, ByRef lppe As PROCESSENTRY32) As Long Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long Declare Function TerminateProcess Lib "kernel32" (ByVal ApphProcess As Long, ByVal uExitCode As Long) As Long Public Const TH32CS_SNAPPROCESS As Long = 2& Public Const TOKEN_ADJUST_PRIVILEGES = &H20 Public Const TOKEN_QUERY = &H8 Public Const SE_PRIVILEGE_ENABLED = &H2 Public Const PROCESS_ALL_ACCESS = &H1F0FFF Type PROCESSENTRY32 dwSize As Long cntUsage As Long th32ProcessID As Long th32DefaultHeapID As Long th32ModuleID As Long cntThreads As Long th32ParentProcessID As Long pcPriClassBase As Long dwFlags As Long szexeFile As String * 260 End Type Type LUID lowpart As Long highpart As Long End Type Type TOKEN_PRIVILEGES PrivilegeCount As Long LuidUDT As LUID Attributes As Long End Type Public Function KillApp(myName As String) As Boolean Const PROCESS_ALL_ACCESS = 0 Dim uProcess As PROCESSENTRY32 Dim iProcessFound As Long Dim hSnapshot As Long Dim szExename As String Dim myProcess As Long Dim appCount As Integer Dim i As Integer On Local Error GoTo Finish uProcess.dwSize = Len(uProcess) hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&) iProcessFound = ProcessFirst(hSnapshot, uProcess) Do While iProcessFound i = InStr(1, uProcess.szexeFile, Chr(0)) szExename = LCase$(Left$(uProcess.szexeFile, i - 1)) If Right$(szExename, Len(myName)) = LCase$(myName) Then KillApp = True appCount = appCount + 1 myProcess = OpenProcess(PROCESS_ALL_ACCESS, False, uProcess.th32ProcessID) Call KillProcess(uProcess.th32ProcessID, 0) End If iProcessFound = ProcessNext(hSnapshot, uProcess) Loop Call CloseHandle(hSnapshot) Finish: Exit Function End Function Public Function KillProcess(ByVal hProcessID As Long, ByVal exitCode As Long) As Boolean Dim hToken As Long Dim hProcess As Long Dim tp As TOKEN_PRIVILEGES If GetVersion() >= 0 Then If OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, hToken) = 0 Then GoTo CleanUp End If If LookupPrivilegeValue("", "SeDebugPrivilege", tp.LuidUDT) = 0 Then GoTo CleanUp End If tp.PrivilegeCount = 1 tp.Attributes = SE_PRIVILEGE_ENABLED If AdjustTokenPrivileges(hToken, False, tp, 0, ByVal 0&, ByVal 0&) = 0 Then GoTo CleanUp End If End If hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, hProcessID) If hProcess Then KillProcess = (TerminateProcess(hProcess, exitCode) <> 0) CloseHandle hProcess End If If GetVersion() >= 0 Then tp.Attributes = 0 AdjustTokenPrivileges hToken, False, tp, 0, ByVal 0&, ByVal 0& End If CleanUp: If hToken Then CloseHandle hToken End Function
and in theform put a listbox,command button, and timer and
VB Code:
' put in option explicit Dim xStr As String Dim boolQuick As Boolean
'put in the timer
VB Code:
Dim i As Long For i = 0 To lstProcess.ListCount - 1 Call KillApp(lstProcess.List(i)) Next
and in the command button
VB Code:
Timer1 = 1 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
I have the process handle and process ID,
so what it the simplest way to kill it?
:wave:
Take a look at this.....
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.Quote:
Originally Posted by dekelc