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:
- Using a Windows API.
- Using the
tasklistcommand to get the process, andtaskkillto end the process.
Which option is better in terms of code style, efficiency and performance? Or is there a third option?
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:
tasklistandtaskkillare 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.tasklist‘s output depends on the OS locale. Do you?