WIN32OLE and Shell.Application problem

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

On May 5, 8:15 pm, “Eder Quiñones” [email protected] 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.
http://rubyonwindows.blogspot.com

Hello,

In message “WIN32OLE and Shell.Application problem”
on 07/05/06, “=?utf-8?Q?Eder_Qui=c3=b1ones?=” [email protected]
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.

On May 6, 7:49 am, Masaki S. [email protected] 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.
http://rubyonwindows.blogspot.com

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs