2

I'm writing a C++ QT desktop application, intended to run on the Windows operating system.
This application should enumerate all running processes, and will kill a specific process (recognized by name). There are two ways I know to do it:

  1. Using a Windows API.
  2. Using the tasklist command to get the process, and taskkill to end the process.

Which option is better in terms of code style, efficiency and performance? Or is there a third option?

sara
  • 3,824
  • 9
  • 43
  • 71
  • 6
    Efficiency and performance are meaningless for an one-off operation. – Jon Jul 05 '12 at 07:44
  • I have experienced performance issues with this code at Java. since I'm doing this enumeration three times during the startup of the application... – sara Jul 05 '12 at 07:49
  • just courious, but why do you do enumeration three times at the start? – Kamil Klimek Jul 05 '12 at 08:51

3 Answers3

6

Efficiency and performance should not worry you, IMO. You're probably not going to kill 1000 processes a second, so if one takes 10ms and the other takes 100ms, I doubt anyone will care.

So, the arguments for choosing one of the other is mostly ease of programming and maintaining. The API for enumerating processes is not the easiest, but you can find many example on the web (like here). Using tasklist will require obtaining the output and parsing it.

I'd go for the API for these reasons:

  1. You can get more information in case the operation fails. You'll know exactly where the problem was, and what was it.
  2. I'm not sure tasklist and taskkill are guaranteed to be on every Windows machine. They do come with the OS, but someone might figure they enlarge the attack surface or whatever, and remove them.
  3. Parsing text is error prone. I have no idea, for instance, if tasklist's output depends on the OS locale. Do you?
Community
  • 1
  • 1
Eran
  • 21,632
  • 6
  • 56
  • 89
0

Actually I work with WMI and I read you can kill process with WMI

Terminate method of the Win32_Process Class

(Just for information :) )

I use winAPI in other application

Kariboo
  • 45
  • 10
0

Of course, you use Windows Api. Which is way better than killing process using external exe. You could get process Handle using OpenProcess that requires you a process id. Once you open you can use TerminateProcess giving a process handle.

crypted
  • 10,118
  • 3
  • 39
  • 52
  • Could you exxplain your reasoning behind the statement "is way better..." I'm not denying it - just suggesting a way to improve the answer. – Andrew Barber Jul 05 '12 at 08:49
  • One reason is if you can do this with few line of code why should you depend on some external lib/dll. – crypted Jul 05 '12 at 08:57