WIN32OLE and Shell.Application problem

  • Thread starter Eder Quiñones
  • Start date
E

Eder Quiñones

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
 
M

mully

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

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 Mullet
http://rubyonwindows.blogspot.com
 
M

Masaki Suketa

Hello,

In message "WIN32OLE and Shell.Application problem"
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 Suketa
 
M

mully

Hello,

In message "WIN32OLE and Shell.Application problem"



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 Suketa

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 Mullet
http://rubyonwindows.blogspot.com
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top