[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Killing Win32 process

Anukul Singhal

6/3/2008 9:46:00 AM

Hi,

I tried the following code snippet to kill a Win32 process:

def killQTP
wmi = WIN32OLE.connect("winmgmts://")
processes = wmi.ExecQuery("select * from Win32_process")
for process in processes do
if process.Name.include? "QTPro.exe" then
puts "Name: #{process.Name}"
puts "Process ID: #{process.Pid}"
puts process.Terminate
elsif process.Name.include? "star.exe" then
puts "Name: #{process.Name}"
puts "Process ID: #{process.Pid}"
puts process.Terminate
end
end
puts "done killing QTP"
end

But the process is not getting killed when I execute the above and
simply gives "exit code: 0"

Can anyone help in case I am missing anything to execute the above code
or is there any other way of killing a Win32 process?

Thanks,
Anukul
--
Posted via http://www.ruby-....

3 Answers

David Mullet

6/3/2008 1:49:00 PM

0

Anukul Singhal wrote:
> Hi,
>
> I tried the following code snippet to kill a Win32 process:
>
> def killQTP
> wmi = WIN32OLE.connect("winmgmts://")
> processes = wmi.ExecQuery("select * from Win32_process")
> for process in processes do
> if process.Name.include? "QTPro.exe" then
> puts "Name: #{process.Name}"
> puts "Process ID: #{process.Pid}"
> puts process.Terminate
> elsif process.Name.include? "star.exe" then
> puts "Name: #{process.Name}"
> puts "Process ID: #{process.Pid}"
> puts process.Terminate
> end
> end
> puts "done killing QTP"
> end
>
> But the process is not getting killed when I execute the above and
> simply gives "exit code: 0"
>
> Can anyone help in case I am missing anything to execute the above code
> or is there any other way of killing a Win32 process?
>
> Thanks,
> Anukul

Change

if process.Name.include? "QTPro.exe" then

to

if process.Name.downcase.include? "qtpro.exe" then

and change

process.Pid

to

process.ProcessId

FYI, This code will output a list of properties for a given process, and
their values:

for property in process.Properties_ do
puts property.Name + ': ' + property.Value.to_s
end

Hope that helps.

David
http://rubyonwindows.bl...
--
Posted via http://www.ruby-....

Marc Heiler

6/3/2008 2:05:00 PM

0

Are you guys sure that anything

object.Upcased.Name

will work, rather than

object.upcased.name

?
--
Posted via http://www.ruby-....

Gordon Thiesfeld

6/3/2008 4:22:00 PM

0

On Tue, Jun 3, 2008 at 4:46 AM, Anukul Singhal <anukul.singhal@gmail.com> wrote:
> Hi,
>
> I tried the following code snippet to kill a Win32 process:
>
> def killQTP
> wmi = WIN32OLE.connect("winmgmts://")
> processes = wmi.ExecQuery("select * from Win32_process")
> for process in processes do
> if process.Name.include? "QTPro.exe" then
> puts "Name: #{process.Name}"
> puts "Process ID: #{process.Pid}"
> puts process.Terminate
> elsif process.Name.include? "star.exe" then
> puts "Name: #{process.Name}"
> puts "Process ID: #{process.Pid}"
> puts process.Terminate
> end
> end
> puts "done killing QTP"
> end
>
> But the process is not getting killed when I execute the above and
> simply gives "exit code: 0"
>
> Can anyone help in case I am missing anything to execute the above code
> or is there any other way of killing a Win32 process?

You might look at win32-process, but if you want to use WMI, remember
WMI queries are basically just SQL.

# this code

require 'win32ole'

wmi = WIN32OLE.connect("winmgmts://")

# I only mixed case to illustrate that case is not important
query = "select * from Win32_process where name='NoTePaD.exe' OR
name='cAlC.exe'"

processes = wmi.ExecQuery(query)

processes.each do |process|
puts "Name: #{process.Name}"
puts "Process ID: #{process.ProcessID}"
puts process.Terminate
end

puts "done killing things"


# returns

Name: notepad.exe
Process ID: 3564
0
Name: calc.exe
Process ID: 5712
0
done killing things


> Thanks,
> Anukul
> --
> Posted via http://www.ruby-....
>
>