Killing Threads & Processes on Windows

X

x1

x =3D Thread.new { system("c:/program files/internet explorer/iexplore.exe"=
) }
x.alive # Works

why doesnt x.kill actually "kill" internet explorer? When I think of
kill, I think of "gone" as in *nix gone.

Yet in Windows, I get:
irb(main):004:0> x.kill
=3D> #<Thread:0x2cfd680 dead>
irb(main):005:0>

---and IE remains.

x =3D IO.popen("c:/program files/internet explorer/iexplore.exe")
x.close

--Just hangs until IE is manually closed.

From a to z, how can this be done?

If another module is needed, can it be installed via gems? Thanks guys.
 
N

nobu.nokada

Hi,

At Wed, 19 Oct 2005 07:44:35 +0900,
x1 wrote in [ruby-talk:161230]:
x = Thread.new { system("c:/program files/internet explorer/iexplore.exe") }
x.alive # Works

why doesnt x.kill actually "kill" internet explorer? When I think of
kill, I think of "gone" as in *nix gone.

A thread and a process spawned within it are not related.
x = IO.popen("c:/program files/internet explorer/iexplore.exe")
x.close

IO#close try to send a signal to the process, however, the
problem is no signal mechanism across processes on Windows,
unless the target has a console.
 
X

x1

Thank you for the response.

I did find a hack using wscript that id like to share:

x =3D IO.popen("c:/program files/internet explorer/iexplore.exe")
x.pid
=3D> 3672
system("wscript.exe test.vbs")

I know this is a hack but can win32api not be used to make a ruby
implementation of this?
------------------------test.vbs--------------------
strComputer =3D "."
Set objWMIService =3D GetObject _
("winmgmts:\\" & strComputer & "\root\cimv2")
Set colProcessList =3D objWMIService.ExecQuery _
("Select * from Win32_Process Where ProcessID =3D 3672")
For Each objProcess in colProcessList
objProcess.Terminate()
Next
-----------------------/test.vbs----------------------------


Thx again!!

Hi,

At Wed, 19 Oct 2005 07:44:35 +0900,
x1 wrote in [ruby-talk:161230]:
x =3D Thread.new { system("c:/program files/internet explorer/iexplore.= exe") }
x.alive # Works

why doesnt x.kill actually "kill" internet explorer? When I think of
kill, I think of "gone" as in *nix gone.

A thread and a process spawned within it are not related.
x =3D IO.popen("c:/program files/internet explorer/iexplore.exe")
x.close

IO#close try to send a signal to the process, however, the
problem is no signal mechanism across processes on Windows,
unless the target has a console.
 
D

dave.burt

Nobu said:
IO#close try to send a signal to the process, however, the
problem is no signal mechanism across processes on Windows,
unless the target has a console.

Could IO#close use ExitProcess() on Windows?
If another module is needed, can it be installed
via gems? Thanks guys.

win32-process will probably do what you need:
http://rubyforge.org/projects/win32utils

I don't believe you can get that via gem.

There may be an easier way, depending on what you're trying to do. For
example, if you just want Internet Explorer, you can use its COM
interface:

require 'win32ole'
ie = WIN32OLE.new("InternetExplorer.Application")
ie.visible = true
sleep 1
ie.quit

Cheers,
Dave
 
D

dave.burt

You can write the WMI script in Ruby:

require 'win32ole'
strComputer = "."
objWMIService = WIN32OLE.connect \
("winmgmts:\\#{strComputer}\root\cimv2")
colProcessList = objWMIService.ExecQuery \
("Select * from Win32_Process Where ProcessID = 3672")
colProcessList.each do |objProcess|
objProcess.Terminate()
end

Cheers,
Dave
 
X

x1

Dave, that my friend was EXACTLY what I wanted. Thank you!!!

Sean, I'll check that out also. Thanks again!
 
N

nobuyoshi nakada

Hi,

At Wed, 19 Oct 2005 11:16:57 +0900,
You can write the WMI script in Ruby:

require 'win32ole'
strComputer = "."
objWMIService = WIN32OLE.connect \
("winmgmts:\\#{strComputer}\root\cimv2")
colProcessList = objWMIService.ExecQuery \
("Select * from Win32_Process Where ProcessID = 3672")
colProcessList.each do |objProcess|
objProcess.Terminate()
end

Does it work on Win9X?
 
D

dave.burt

nobuyoshi said:
Hi,

At Wed, 19 Oct 2005 11:16:57 +0900,


Does it work on Win9X?

No, I don't think so -- I think all of WMI is WinNT family only, but
this is a direct translation of the VBScript given.

If you're asking about my suggestion of using TerminateProcess() as an
alternative to signals to close a popen'd process in IO#close, then the
answer is yes. (TerminateProcess(), not ExitProcess() which kills the
calling process.)

Documentation for TerminateProcess():
http://msdn.microsoft.com/library/en-us/dllproc/base/terminateprocess.asp

Cheers,
Dave
 
N

nobuyoshi nakada

Hi,

At Wed, 19 Oct 2005 17:11:58 +0900,
If you're asking about my suggestion of using TerminateProcess() as an
alternative to signals to close a popen'd process in IO#close, then the
answer is yes. (TerminateProcess(), not ExitProcess() which kills the
calling process.)

I know about TerminateProcess(), but think it is considered
critical for general purpose, like as SIGKILL.
 
T

tony summerfelt

x1 wrote on 10/18/2005 9:30 PM:
system("wscript.exe test.vbs")

or you can use a combination of sysinternals' pslist, and pskill. i
find it handy for those systems where wscript and cscript have been
disabled (like on my own w2k system)
 
D

Dave Burt

nobu said:
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?

Cheers,
Dave
 
D

daz

dave.burt... said:
nobuyoshi said:
Hi,

At Wed, 19 Oct 2005 11:16:57 +0900,
dave.burt... wrote in [ruby-talk:161280]:
You can write the WMI script in Ruby:

require 'win32ole'
strComputer = "."
objWMIService = WIN32OLE.connect \
("winmgmts:\\#{strComputer}\root\cimv2")
colProcessList = objWMIService.ExecQuery \
("Select * from Win32_Process Where ProcessID = 3672")
colProcessList.each do |objProcess|
objProcess.Terminate()
end

Does it work on Win9X?

No, I don't think so -- [...]

True - needs WMI core 1.5 add-on -- see Run-Time Requirements here:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/wmi_start_page.asp


I wanted to point out, though, that passing a Ruby string
to WIN32OLE.connect means those '\' must be doubled:

":\\\\.\\root\\cimv2"
# 000 3a 5c 5c 2e 5c 72 6f 6f 74 5c 63 69 6d 76 32 :\\.\root\cimv2

.... rather than ...

":\\.\root\cimv2"
# 000 3a 5c 2e 0d 6f 6f 74 09 6d 76 32 :\..oot.mv2


Alternatively, '/' appears to be accepted by WMI:
"winmgmts://./root/cimv2"


daz
 
W

Warren Seltzer

You can do WMI in Ruby. There's a library already built for this.

Warren Seltzer

P.S. Have you ever seen such a hellacious mess as WMI?
 
X

x1

for future reference to others and thanks to dave.

There was a small issue with backslashes and escapes in the example
dave gave us....

Very small prob but.. wanted to inform others who may stumble on this..

Here's what worked for me in the end:
require 'win32ole'

def start_process(command)
=09return IO.popen(command).pid
end

def kill_process(pid)
=09strComputer =3D "."
=09objWMIService =3D WIN32OLE.connect \
=09 ("winmgmts:\\\\#{strComputer}\\root\\cimv2")
=09colProcessList =3D objWMIService.ExecQuery \
=09 ("Select * from Win32_Process Where ProcessID =3D #{x.to_s}")
=09colProcessList.each do |objProcess|
=09 objProcess.Terminate()
=09end
=09return "done"
end

command =3D "c:/program files/internet explorer/iexplore.exe"
pid =3D start_process(command)
puts "killing pid now (#{pid}).."
sleep 1 #probably not needed
puts kill_process(pid)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top