win32ole object creation failure

A

Axel

Hello, Rubyists!

I have an example program from a German book on Ruby here, using
win32ole:

require "win32ole"

dlg = WIN32OLE.new("MSComDlg.CommonDialog")
# den Datei-Filter setzen
dlg.filter = "Word documents (*.doc)|*.doc" +
"|All Files(*.*)|*.*"
dlg.filterIndex = 1
dlg.maxFileSize = 128
file = ""
dlg.showOpen
file = dlg.fileName
exit if file == ""
print "#{file} wurde selektiert"

When I run this (on Windows XP with the Windows Ruby installation
package 1.8.2-15), I get the following error:

C:/temp/bla.rb:3:in `initialize': Failed to create WIN32OLE object from
`MSComDlg.CommonDialog' (WIN32OLERuntimeError)
HRESULT error code:0x80040112
Class is not licensed for use from C:/temp/bla.rb:3:in `new'
from C:/temp/bla.rb:3

Any ideas what could cause the problem?

Kind regards! ------------- Axel <><
 
G

gregarican

Axel said:
When I run this (on Windows XP with the Windows Ruby installation
package 1.8.2-15), I get the following error:


C:/temp/bla.rb:3:in `initialize': Failed to create WIN32OLE object from
`MSComDlg.CommonDialog' (WIN32OLERuntimeError)
HRESULT error code:0x80040112
Class is not licensed for use from C:/temp/bla.rb:3:in `new'
from C:/temp/bla.rb:3

I get the same result running this on a Windows 2000 box under Ruby
1.8.2. Googling around for this Windows API entry it appears as if it
should be referenced directly using VB and isn't readily available
through scripting means. At least this URL makes it seem as such -->
http://www.autoitscript.com/forum/i...c2&showtopic=11379&pid=80340&st=0&#entry80340.
 
C

Corey Lawson

Hmm... I found a chunk of VB code here
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=3D12394=
&lngWId=3D1
(look in the ComDlgEx.cls file) that shows what you need to do in VB
to use comdlg32.dll.

In Ruby, it looks like you can then use this via:

require 'WIN32API'

#If you don't have "Pickaxe" (see p 755) then...

require 'Win32API'
comdlgFileOpen =3D Win32API.new("comdlg32.dll", "GetOpenFileNameA", ['P'], =
'L')

#...and it goes downhill from there. You basically have to make a
(sorry for the C syntax)
struct OPENFILENAME {
...
}
#what is the ruby syntax for defining a struct?

#then use pack() and unpack() to smoosh and unsmoosh things

ofn =3D OPENFILENAME=20

res =3D comdlgFileOpen.Call(ofn)

#in the ofn instance of the OPENFILENAME struct, you set all sorts of
settings for the
#dialog, including file name patterns, etc.

It looks to be pretty messy, but it's messy for everyone not using
C++, unless your lang (i.e., Delphi) provides nice encapsulations of
the API calls...

Pickaxe also mentions that=20
require 'dl/win32'=20
might be a bit more mature. Maybe it's easier to use?
 
C

Corey Lawson

This URL probably has what looks like some good info on using
Win32API: http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/32861

Hmm... I found a chunk of VB code here
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=3D123= 94&lngWId=3D1
(look in the ComDlgEx.cls file) that shows what you need to do in VB
to use comdlg32.dll.
=20
In Ruby, it looks like you can then use this via:
=20
require 'WIN32API'
=20
#If you don't have "Pickaxe" (see p 755) then...
=20
require 'Win32API'
comdlgFileOpen =3D Win32API.new("comdlg32.dll", "GetOpenFileNameA", ['P']= , 'L')
=20
#...and it goes downhill from there. You basically have to make a
(sorry for the C syntax)
struct OPENFILENAME {
...
}
#what is the ruby syntax for defining a struct?
=20
#then use pack() and unpack() to smoosh and unsmoosh things
=20
ofn =3D OPENFILENAME
=20
res =3D comdlgFileOpen.Call(ofn)
=20
#in the ofn instance of the OPENFILENAME struct, you set all sorts of
settings for the
#dialog, including file name patterns, etc.
=20
It looks to be pretty messy, but it's messy for everyone not using
C++, unless your lang (i.e., Delphi) provides nice encapsulations of
the API calls...
=20
Pickaxe also mentions that
require 'dl/win32'
might be a bit more mature. Maybe it's easier to use?
=20
=20
=20
I get the same result running this on a Windows 2000 box under Ruby
1.8.2. Googling around for this Windows API entry it appears as if it
should be referenced directly using VB and isn't readily available
through scripting means. At least this URL makes it seem as such -->
http://www.autoitscript.com/forum/index.php?s=3D58aec18875ef9a11f9d1b2a= f990a92c2&showtopic=3D11379&pid=3D80340&st=3D0&#entry80340.
 
G

gregarican

Corey said:
This URL probably has what looks like some good info on using
Win32API: http://blade.nagaokaut.ac.jp/c­gi-bin/scat.rb/ruby/ruby-talk/­32861

Good stuff. I have worked with some of the win32api elements in Ruby.
For some admin scripts of checking for open files on the file server,
determining specific workstation Windows settings against Ruby app
requirements, etc. I can tell you that the Microsoft API is a slippery
slope for sure. If some operations can be accomplished some other way
by not getting involved in the API so much the better as far as I'm
concerned. :)
 
A

Axel

gregarican said:
Googling around for this Windows API entry it appears as if it
should be referenced directly using VB and isn't readily available
through scripting means. At least this URL makes it seem as such -->
http://www.autoitscript.com/forum/i...c2&showtopic=11379&pid=80340&st=0&#entry80340.

As the code in my earlier post comes from a book I thought it must have
worked at one point in time (authors test their stuff, don't they?).
But the above URL contains a telling phrase: "It will only be free if
you have an Application or some MS-Developer tools, like VBE on your
computer." This component seems to be available for scripting only if
certain MS products are installed. These might be MS Office or the
Visual Basic. Interesting. (MS VC++ does not help ... I have that on my
machine.) This may catch you when deploying your script to other
computers.

But I think the approach of going through the API will work, as that is
available everywhere. It will be fun figuring out the correct pack
statement for the OFNFILENAME structure, but I expect it can be done ...
 
C

Corey Lawson

calls to comdlg32.ocx only work thru vb/vba/etc. comdlg32.dll is avail
to all (but, as you can see, a PITA to use). Comdlg32.ocx is a nice
wrapper on the dialog functions in comdlg32.dll.

A <i>very</i> kludgy way around it, of course, would be to call
comdlg32.ocx via VBScript, which you invoke via WIN32OLE, but then you
would be able to pretty much paste all the VB/VBA code in verbatum...

The one reference for the TextPrintDlg for Ruby actually works, at
least for showing the dialog. But one thing that that dialog does not
require that GetOpenFileis an hWnd (you should be able to use 0) or
hInstance (what to use for irb, or do you try to get and use the
shell's hInstance?).
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top