# Killing Win32 process

**URL:** <https://www.ruby-forum.com/t/killing-win32-process/139380>\
**Category:** Ruby\
**Created:** [June 3, 2008, 11:47am UTC](https://www.ruby-forum.com/t/killing-win32-process/139380 "2008-06-03T11:47:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anukulsinghal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@anukulsinghal](https://www.ruby-forum.com/u/anukulsinghal)\
**Post date:** [June 3, 2008, 11:47am UTC](https://www.ruby-forum.com/t/killing-win32-process/139380/1 "2008-06-03T11:47:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![anukulsinghal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@anukulsinghal](https://www.ruby-forum.com/u/anukulsinghal)\
**Post date:** [June 3, 2008, 3:49pm UTC](https://www.ruby-forum.com/t/killing-win32-process/139380/2 "2008-06-03T15:49:17Z")

</div>

Anukul S. 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

> **[Ruby on Windows](http://rubyonwindows.blogspot.com)**
>
> On using the Ruby programming language on the Microsoft Windows platform

---

<div class="post-metadata">

**Author:** ![anukulsinghal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@anukulsinghal](https://www.ruby-forum.com/u/anukulsinghal)\
**Post date:** [June 3, 2008, 4:05pm UTC](https://www.ruby-forum.com/t/killing-win32-process/139380/3 "2008-06-03T16:05:37Z")

</div>

Are you guys sure that anything

object.Upcased.Name

will work, rather than

object.upcased.name

?

---

<div class="post-metadata">

**Author:** ![anukulsinghal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@anukulsinghal](https://www.ruby-forum.com/u/anukulsinghal)\
**Post date:** [June 3, 2008, 6:23pm UTC](https://www.ruby-forum.com/t/killing-win32-process/139380/4 "2008-06-03T18:23:36Z")

</div>

On Tue, Jun 3, 2008 at 4:46 AM, Anukul S.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> puts "Process ID: #{process.Pid}"
> 
> ```
> 
> 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
