Win32ole object creation failure

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 <><

Axel wrote:

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/index.php?s=58aec18875ef9a11f9d1b2af990a92c2&showtopic=11379&pid=80340&st=0&#entry80340.

Hmm... I found a chunk of VB code here
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=12394&lngWId=1
(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 = 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 = OPENFILENAME

res = 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
  require 'dl/win32'
might be a bit more mature. Maybe it's easier to use?

···

On 6/30/05, gregarican <greg.kujawa@gmail.com> wrote:

Axel wrote:

> 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/index.php?s=58aec18875ef9a11f9d1b2af990a92c2&showtopic=11379&pid=80340&st=0&#entry80340.

gregarican schrieb:

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=58aec18875ef9a11f9d1b2af990a92c2&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 ...

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

···

On 6/30/05, Corey Lawson <corey.ssf.lawson@gmail.com> wrote:

Hmm... I found a chunk of VB code here
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=12394&lngWId=1
(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 = 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 = OPENFILENAME

res = 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
  require 'dl/win32'
might be a bit more mature. Maybe it's easier to use?

On 6/30/05, gregarican <greg.kujawa@gmail.com> wrote:
> Axel wrote:
>
> > 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/index.php?s=58aec18875ef9a11f9d1b2af990a92c2&showtopic=11379&pid=80340&st=0&#entry80340.
>
>
>

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?).

···

On 6/30/05, Axel <anieden@gmx.de> wrote:

gregarican schrieb:
> 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=58aec18875ef9a11f9d1b2af990a92c2&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 ...

Corey Lawson wrote:

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. :slight_smile: