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/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值