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