Microsoft KB Archive/90749

{|
 * width="100%"|

HOWTO: Implement a &quot;Kill&quot; Operation in Windows NT

 * }

Q90749

-

The information in this article applies to:


 * Microsoft Win32 Application Programming Interface (API), used with:
 * the operating system: Microsoft Windows NT, versions 3.1, 3.5, 4.0
 * the operating system: Microsoft Windows 2000

-

SUMMARY
The following sample demonstrates how to implement a &quot;kill&quot; operation, such as a UNIX ps/kill, under Windows NT. Note that PView gives you the PID you need.

The code sample makes use of the Win32 API TerminateProcess. While TerminateProcess does clean up the objects owned by the process, it does not notify any DLLs hooked to the process. Therefore, this can easily leave the DLL in an unstable state.

In general, the Task List is a much cleaner method of killing processes.

MORE INFORMATION
The following sample shows how to &quot;kill&quot; a process, given its process ID (PID).

Sample Code
  #include    #include 

void ErrorOut(char errstring[30]) /*  Purpose: Print out an meaningful error code by means of            GetLastError and printf.

Inputs: errstring - the action that failed, passed by the calling proc.

Returns: none

Calls:  GetLastError */

{     DWORD Error;

Error= GetLastError; printf(&quot;Error on %s = %d\n&quot;, errstring, Error); }

void main(int argc, char *argv[]) {     HANDLE hProcess; DWORD ProcId; BOOL TermSucc;

if (argc == 2) {        ProcId = atoi(argv[1]); hProcess= OpenProcess(PROCESS_ALL_ACCESS, TRUE, ProcId); if (hProcess == NULL) ErrorOut(&quot;OpenProcess&quot;); TermSucc= TerminateProcess(hProcess, 0); if (TermSucc == FALSE) ErrorOut(&quot;TerminateProcess&quot;); else printf(&quot;Process# %.0lx terminated successfully!\n&quot;, ProcId); }     else {        printf(&quot;\nKills an active Process\n&quot;); printf(&quot;Usage: killproc ProcessID\n&quot;); }  } Additional query words:

Keywords : kbAPI kbKernBase kbOSWinNT310 kbOSWinNT350 kbOSWinNT400 kbOSWin2000 kbThread kbDSupport kbGrpDSKernBase

Issue type : kbhowto

Technology : kbAudDeveloper kbWin32sSearch kbWin32API