开发者

Kill process by name

开发者 https://www.devze.com 2023-02-07 17:23 出处:网络
How开发者_如何学JAVA can I kill a process by name on Windows with API functions only?In a cmd window you can use taskkill /F /im cmd.exe to kill all cmd.exe programs.

How开发者_如何学JAVA can I kill a process by name on Windows with API functions only?


In a cmd window you can use taskkill /F /im cmd.exe to kill all cmd.exe programs.

The /T flag tells taskkill to forcefully kill the processes, whereas taskkill /im cmd.exe gracefully shuts it down by sending a kill signal.

more information on taskkill can be found here

another great tool for terminating processes is wmic


killall yourprocess

http://en.wikipedia.org/wiki/Killall (note that this only applies on Linux, as you haven't specified your OS)


If by kill you mean terminate with extreme prejudice,theres's always TerminateProcess. I'd advise against using it if you can find another way (such as posting WM_CLOSE to the main window), because loaded DLLs don't get unloaded properly when you call TerminateProcess, so used resources might not get released.

The real problem is how to get from a name to a process ID, this normally involves

EnumProcesses()
EnumProcessModules()
GetModuleFileNameEx()
GetModuleBaseName()

and comparing the resulting base module name against what you're looking for. But there may be more than one instance of a module name executing on a system. How would you differentiate between instances in this circumstance?


Assuming you mean on Windows, use ZwQuerySystemInformation and TerminateProcess. See here for a detailed sample: How to kill a process given a name

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号