4

In C++ is there a way to unconditionally kill a process?

No matter what state this process is in.

I know about TerminateProcess, but it can still fail. What if you don't want it to fail.

Like when you kill a process in Task Manager, it dies; no matter what. That's the kind of killing I'm looking for.

sbi
  • 219,715
  • 46
  • 258
  • 445
Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
  • 2
    Do you know of any circumstances other than permissions failure under which `TerminateProcess` will fail? – James McNellis Aug 23 '10 at 13:36
  • 1
    @James: If the process is running kernel mode code, it may not be terminated. Add a dash of poorly written driver code and you can definitely get into infinite loops that can't be recovered from. – Ben Voigt Aug 23 '10 at 13:48

1 Answers1

9

Not every process can be killed from Task Manager. This depends both on permissions and on process state. Some processes, hung in winsock can't be killed (and even Task Manager will hang).

joekoyote
  • 221
  • 1
  • 5
  • 5
    True. And killing some processes (esp. some system services, like RPC server) will bring down your whole system (artificially yet automatically), for stability reasons (a 30-sec shutdown prompt pops up). But other than that, `TerminateProcess` is the best bet you have in Windows (incidentally, that's what Task Manager does). – Piskvor left the building Aug 23 '10 at 13:41
  • How about process explorer (from sysinternals)? I've even killed svchost from procexpl (not wise) – default Aug 23 '10 at 13:54
  • 2
    process explorer uses undocumented internals of the system (maybe even kernel-mode stuff, I didn't check). There's a lot you can this way, yet process explorer sometimes also can't kill a process (if the process is blocked in the kernel). svchost is a user-mode process, so it's just a permissions question. – joekoyote Aug 23 '10 at 14:07