Killing Threads & Processes on Windows

Discussion in 'Ruby' started by x1, Oct 18, 2005.

  1. x1

    x1 Guest

    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.
     
    x1, Oct 18, 2005
    #1
    1. Advertising

  2. x1

    Guest

    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.

    --
    Nobu Nakada
     
    , Oct 19, 2005
    #2
    1. Advertising

  3. x1

    x1 Guest

    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!!

    On 10/18/05, <> wrote:
    > 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.
    >
    > --
    > Nobu Nakada
    >
    >
     
    x1, Oct 19, 2005
    #3
  4. x1

    Guest

    Nobu wrote:
    > 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?

    OP wrote:
    > 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
     
    , Oct 19, 2005
    #4
  5. x1

    Guest

    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
     
    , Oct 19, 2005
    #5
  6. Sean O'Halpin, Oct 19, 2005
    #6
  7. x1

    x1 Guest

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

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

    On 10/18/05, <> wrote:
    > You can write the WMI script in Ruby:
    >
    > require 'win32ole'
    > strComputer =3D "."
    > objWMIService =3D WIN32OLE.connect \
    > ("winmgmts:\\#{strComputer}\root\cimv2")
    > colProcessList =3D objWMIService.ExecQuery \
    > ("Select * from Win32_Process Where ProcessID =3D 3672")
    > colProcessList.each do |objProcess|
    > objProcess.Terminate()
    > end
    >
    > Cheers,
    > Dave
    >
    >
    >
     
    x1, Oct 19, 2005
    #7
  8. Hi,

    At Wed, 19 Oct 2005 11:16:57 +0900,
    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?

    --
    Nobu Nakada
     
    nobuyoshi nakada, Oct 19, 2005
    #8
  9. x1

    Guest

    nobuyoshi nakada wrote:
    > Hi,
    >
    > At Wed, 19 Oct 2005 11:16:57 +0900,
    > 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 -- 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
     
    , Oct 19, 2005
    #9
  10. Hi,

    At Wed, 19 Oct 2005 17:11:58 +0900,
    wrote in [ruby-talk:161338]:
    > 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.

    --
    Nobu Nakada
     
    nobuyoshi nakada, Oct 19, 2005
    #10
  11. tony summerfelt, Oct 19, 2005
    #11
  12. x1

    Dave Burt Guest

    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?

    Cheers,
    Dave
     
    Dave Burt, Oct 19, 2005
    #12
  13. x1

    daz Guest

    dave.burt... wrote:
    > nobuyoshi nakada wrote:
    > > 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
     
    daz, Oct 20, 2005
    #13
  14. 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?
     
    Warren Seltzer, Oct 27, 2005
    #14
  15. x1

    x1 Guest

    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)

    On 10/27/05, Warren Seltzer <> wrote:
    > 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?
    >
    >
    >
    >
     
    x1, Nov 10, 2005
    #15
  16. x1

    x1 Guest

    whoops..

    replace x.to_s with pid.to_s in the kill method..
    ProcessID =3D #{pid.to_s}"


    On 11/9/05, x1 <> wrote:
    > 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)
    > return IO.popen(command).pid
    > end
    >
    > def kill_process(pid)
    > strComputer =3D "."
    > objWMIService =3D WIN32OLE.connect \
    > ("winmgmts:\\\\#{strComputer}\\root\\cimv2")
    > colProcessList =3D objWMIService.ExecQuery \
    > ("Select * from Win32_Process Where ProcessID =3D #{x.to_s}")
    > colProcessList.each do |objProcess|
    > objProcess.Terminate()
    > end
    > return "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)
    >
    > On 10/27/05, Warren Seltzer <> wrote:
    > > 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?
    > >
    > >
    > >
    > >

    >
    >
     
    x1, Nov 10, 2005
    #16
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stu
    Replies:
    2
    Views:
    487
  2. bugbear
    Replies:
    1
    Views:
    1,087
    bugbear
    Nov 4, 2005
  3. hepp

    Killing processes

    hepp, Aug 30, 2004, in forum: C++
    Replies:
    1
    Views:
    438
    Victor Bazarov
    Aug 30, 2004
  4. Jeff Rodriguez
    Replies:
    23
    Views:
    1,200
    David Schwartz
    Dec 9, 2003
  5. Andrei

    killing processes on win xp

    Andrei, Feb 11, 2004, in forum: Python
    Replies:
    1
    Views:
    554
    Yen-Ting Chen
    Feb 13, 2004
Loading...

Share This Page