Kill Process by Name

Kill Process by Name
(works in: Microsoft Windows 95/98/ME/NT/2000/XP)
It is sometimes necessary to terminate a running process in MS-Windows. The Win32 API provides the TerminateProcess function for this, but it requires a "handle to the process". In many cases it is not apparent how to get this handle if all you know is the process name. The following routine lets you get around this problem.

The routine is named KILL_PROC_BY_NAME, and is available as part of the C++ source file EXAM28.CPP. The routine has the following calling parameters:

int KILL_PROC_BY_NAME(const char *)
where the process name is supplied as a zero-terminated C char string and it returns an integer completion code.
You can download the file in ZIP format by clicking on the following link.

Download Exam28.zip (3 kB, last modified: 3/10/2002)
This works for Microsoft Windows 95/98/ME/NT/2000/XP. I have tested with Visual C++ v6.0 but it should work with other versions also. Please let me know of exceptions.

After downloading, unzip the file Exam28.cpp into any folder. It contains a short console mode program which demonstrates how to call KILL_PROC_BY_NAME to terminate the NotePad program.

Change history:

3/10/2002 - Fixed memory leaks as suggested by Jonathan Richard-Brochu and also made Borland C compatible as suggested by Bob Christensen.
Note: This routine uses the Windows API function TerminateProcess to terminate the process. This may not be suitable for all purposes. In particular, note the following remarks from the MSDN description of TerminateProcess.

Remarks (on TerminateProcess)

The TerminateProcess function is used to unconditionally cause a process
to exit. Use it only in extreme circumstances. The state of global data
maintained by dynamic-link libraries (DLLs) may be compromised if
TerminateProcess is used rather than ExitProcess.

TerminateProcess causes all threads within a process to terminate,
and causes a process to exit, but DLLs attached to the process are not
notified that the process is terminating.

Terminating a process causes the following:

1. All of the object handles opened by the process are closed.

2. All of the threads in the process terminate their execution.

3. The state of the process object becomes signaled, satisfying any
threads that had been waiting for the process to terminate.

4. The states of all threads of the process become signaled, satisfying
any threads that had been waiting for the threads to terminate.

5. The termination status of the process changes from STILL_ACTIVE to
the exit value of the process.

Terminating a process does not cause child processes to be terminated.

Terminating a process does not necessarily remove the process object
from the system. A process object is deleted when the last handle to the
process is closed.
If you have any questions please send them to the e-mail address below.

Further Reading:

Howto Enumerate Applications in Win32 (Microsoft KB article)

Disclaimer:
This site contains some software that I've written over the years. It is believed to work, but there is no warranty. Please use at your own risk. If you notice any bugs please let me know by email at kochhar@physiology.wisc.edu.

Return to Ravi's Free Software Page
Return to Ravi's Home Page
Back to The Basement
This page last modified on : Mar. 10, 2002

http://www.physiology.wisc.edu/ravi/software/killproc/

上一篇:Out of memory: Kill process 6033 (mysqld) score 85 or sacrifice child


下一篇:[故障公告]14:39-15:39博客站点部分负载均衡遭遇3次20G以上的流量攻击