Killing Threads & Processes on Windows

The win32/process package uses a CreateRemoteThread() + ExitProcess()
approach (except for signal 9, which then uses TerminateProcess). This
was recommended as a safer way to kill Windows processes (by Jeremy
Richter, among others).

Here's the C code (with a small bit elided out):

// hProcess is a HANDLE acquired by OpenProcess() earlier
if(hProcess){
   hThread = CreateRemoteThread(
      hProcess,
      NULL,
      0,
      (LPTHREAD_START_ROUTINE)(
         GetProcAddress(
            GetModuleHandle("KERNEL32.DLL"), "ExitProcess"
         )
      ),
      0,
      0,
      &dwThreadId
   );

   if(hThread){
      WaitForSingleObject(hThread, dwTimeout);
      CloseHandle(hProcess);
      ...
   }
   else{
      CloseHandle(hProcess);
      // Raise an error
   }
}

···

-----Original Message-----
From: Dave Burt [mailto:dave@burt.id.au]
Sent: Wednesday, October 19, 2005 8:07 AM
To: ruby-talk ML
Subject: Re: Killing Threads & Processes on Windows

I accidentally sent:
> nobu nakada wrote:
>> I know about TerminateProcess(), but think it is
considered critical
>> for general purpose, like as SIGKILL.
>
> You're probably right, it is severe, but rb_waitpid
shouldn't return
> with
> the process still running, should it?

.... or just hang without closing the process?

Would a call to TerminateProcess() go in wait() in win32/win32.c?

Cheers,
Dave

Hi,

At Thu, 20 Oct 2005 00:45:28 +0900,
Berger, Daniel wrote in [ruby-talk:161421]:

The win32/process package uses a CreateRemoteThread() + ExitProcess()
approach (except for signal 9, which then uses TerminateProcess). This
was recommended as a safer way to kill Windows processes (by Jeremy
Richter, among others).

It's also only for NT, isn't it? Well, to be honest, I'd like
to divide executable files for NT and 95.

···

--
Nobu Nakada