1

Possible Duplicate:
How to effectively kill a process in C++ (Win32)?


I am creating a program that has to do an important task - and it may (rarely) have a resource conflict with another program (let's say a.exe) that users may have inadvertently started.
What I would like to do is kill a.exe if it is running, before doing the rest of the work.
Is there any way to stop another program/process from running, from c++ (running under Windows) ?
I was hoping that there may be something in the beautiful boost...
Thank you.

Community
  • 1
  • 1
Thalia
  • 13,637
  • 22
  • 96
  • 190

3 Answers3

3

If you're sure the image name is a.exe then it's very simple:

system("taskkill /F /IM a.exe >nul 2>&1");
user703016
  • 37,307
  • 8
  • 87
  • 112
  • +1: Does what the OP requested. Worth bearing in mind that this will try and kill all processes matching a.exe – Jon Cage May 29 '12 at 22:38
  • The only problem with this is that `taskkill.exe` is not guaranteed to be the original one, and might not work as expected. `TerminateProcess` would be better if you need that guarantee. – chris May 29 '12 at 22:53
  • The worst case of my last comment would be starting a `taskkill.exe` that's in the same directory as the program without knowing it, and possibly giving it admin (or even beyond that depending on what type of program yours is) privileges to do whatever it wants. – chris May 29 '12 at 23:02
  • Is there a way to check if the process is still running ? I am getting an error message if I am killing it and it is already dead (or hasn't been running). – Thalia May 29 '12 at 23:28
  • I am also getting an error on the killed program, after I re-open, that it didn't shut down correctly. – Thalia May 29 '12 at 23:59
  • I edited my answer to remove the error message. For the "X didn't shutdown correctly" this is an issue with Windows. Maybe removing the `/F` would fix that. – user703016 May 30 '12 at 00:03
1

Assuming you are working on Windows.

You can use .NET methods. I will link you a post. https://stackoverflow.com/a/116098/1424790

If you need, I could translate the post into C++ (but I need some time for that).

Community
  • 1
  • 1
  • If there is something similar to System.Diagnostics.Proccess.GetProcessesByName in c++, I would love to know. I would like to target my process better (actually by its target folder, but that is probably asking too much), and in any extent I would like to not get errors when the process is not running (I caught all exceptions but I am still getting an error message). – Thalia May 29 '12 at 23:31
1

It depends, if you're talking about Windows then you can either use taskkill as already stated, or you can use kernel32.dll's TerminateProcess method.

More info on that - http://msdn.microsoft.com/en-us/library/windows/desktop/ms686714(v=vs.85).aspx

If you're on *nix, it's as simple as using POSIX kill method found in signal.h.

More info on that - http://linux.die.net/man/2/kill oops, just properly read the question mentioning Windows

Rudi Visser
  • 21,350
  • 5
  • 71
  • 97