On Sunday, 31 March 2013 at 15:21:38 UTC, Lars T. Kyllingstad
wrote:
As you may already have guessed, it does *not* print
"TerminateProcess failed" and exit with code 0.
But does it print "TerminateProcess succeeded" and exit with
code 1? NO! It prints NOTHING and exits with code 123, because
TerminateProcess() terminates the CURRENT process when it is
passed INVALID_HANDLE_VALUE. Aaaaargh!
How did INVALID_HANDLE_VALUE get so far in the code as to reach
TerminateProcess? Shouldn't an enforce call have been in place to
validate whatever the source of the handle is?
Anyway, this is documented behavior. You can pass
GetCurrentProcess() to TerminateProcess to terminate the current
process.
Your plight was caused by the unfortunate (or perhaps,
unforesighted) coincidence that GetCurrentProcess() returns the
special (magic) value of (HANDLE)-1, the same value of
INVALID_HANDLE_VALUE.