# WIN32OLE and Shell.Application problem

**URL:** <https://www.ruby-forum.com/t/win32ole-and-shell-application-problem/225947>\
**Category:** Ruby\
**Created:** [May 6, 2007, 2:15am UTC](https://www.ruby-forum.com/t/win32ole-and-shell-application-problem/225947 "2007-05-06T02:15:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![eder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/a698b9/32.png) [@eder](https://www.ruby-forum.com/u/eder)\
**Post date:** [May 6, 2007, 2:15am UTC](https://www.ruby-forum.com/t/win32ole-and-shell-application-problem/225947/1 "2007-05-06T02:15:21Z")

</div>

Hi, anyone knows what is the problem with this function, i believe i did  
everything right, but it just does not open the cd-rom, maybe the  
problem is in the part “InvokeVerb”. Any help would be highly  
appreciated.

def ejectDrives

@wbem = WIN32OLE.new(‘WbemScripting.SWbemLocator’)  
@a = @wbem.ConnectServer  
@b = @a.InstancesOf(‘Win32\_LogicalDisk’)  
cdroms = Array.new

@b.each do | object |  
if object.Description =~ /cd/i  
cdroms \<\< object.Name  
end  
end

@shell = WIN32OLE.new(‘Shell.Application’)

cdroms.each do | name |

```
@ej1 = @shell.NameSpace(name)
@ej2 = @ej1.Self
@ej3 = @ej2.InvokeVerb("Expu&lsar")

```

end

end

This is another version using “WMPlayer.OCX” that actually work.

def ejectDrivesWMP

@wmp = WIN32OLE.new(‘WMPlayer.OCX’)

@cdromCol = @wmp.cdromCollection  
@cdromCount = @cdromCol.Count

i = 1  
while i \<= @cdromCount  
@cdromCol.Item(i - 1).Eject  
i += 1  
end

end

---

<div class="post-metadata">

**Author:** ![eder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/a698b9/32.png) [@eder](https://www.ruby-forum.com/u/eder)\
**Post date:** [May 6, 2007, 6:00am UTC](https://www.ruby-forum.com/t/win32ole-and-shell-application-problem/225947/2 "2007-05-06T06:00:58Z")

</div>

On May 5, 8:15 pm, “Eder Quiñones” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> cdroms = Array.new
> 
> def ejectDrivesWMP  
> end
> 
> end
> 
> –  
> Posted viahttp://www.ruby-forum.com/.

You might try a variation of the following (where D is the CDROM  
drive)…

WIN32OLE.new(“Shell.Application”).NameSpace(17).ParseName("D:  
").InvokeVerb(“E&ject”)

…or perhaps…

WIN32OLE.new(“Shell.Application”).NameSpace(17).ParseName("D:  
").InvokeVerb(“Expu&lsar”)

David M.

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

---

<div class="post-metadata">

**Author:** ![eder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/a698b9/32.png) [@eder](https://www.ruby-forum.com/u/eder)\
**Post date:** [May 6, 2007, 1:49pm UTC](https://www.ruby-forum.com/t/win32ole-and-shell-application-problem/225947/3 "2007-05-06T13:49:32Z")

</div>

Hello,

In message “WIN32OLE and Shell.Application problem”  
on 07/05/06, “=?utf-8?Q?Eder\_Qui=c3=b1ones?=” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
writes:

> cdroms.each do | name |
> 
> ```
> @ej1 = @shell.NameSpace(name)
> @ej2 = @ej1.Self
> @ej3 = @ej2.InvokeVerb("Expu&lsar")
> 
> ```

It is known problem.  
InvokeVerb does not work in Win32OLE (in Ruby 1.8).  
Instead try to use doIt.  
@ej1 = @shell.NameSpace(name)  
@ej2 = @ej1.Self  
verbs = @ej2.verbs  
verb = nil  
verbs.each do |v|  
if v.name == “Expu&lsar”  
verb = v  
end  
end  
if verb  
verb.doIt  
end

FYI, in Ruby 1.9, InvokeVerb works by using WIN32OLE\_VARIANT class.

@ej3 = @ej2.InvokeVerb(WIN32OLE\_VARIANT.new(“Expu&lsar”))

Regards,  
Masaki S.

---

<div class="post-metadata">

**Author:** ![eder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/a698b9/32.png) [@eder](https://www.ruby-forum.com/u/eder)\
**Post date:** [May 6, 2007, 2:35pm UTC](https://www.ruby-forum.com/t/win32ole-and-shell-application-problem/225947/4 "2007-05-06T14:35:35Z")

</div>

On May 6, 7:49 am, Masaki S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> end
> 
> ```
> 
> Masaki S.  
> Thanks, Masaki!

I was unable to test my earlier code suggestion at the time of  
posting. As you say, InvokeVerb does not work as expected, but the  
[verb.doIt] method you suggested works well (for me)…

WIN32OLE.new(“Shell.Application”).NameSpace(17).ParseName("D:  
").Verbs.each do |verb|  
verb.doIt if verb.Name == “E&ject”  
end

Thanks again!

David M.

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