# Win32ole object creation failure

**URL:** <https://rubytalk.org/t/win32ole-object-creation-failure/19271>\
**Category:** ruby-talk\
**Created:** [30 June 2005 12:15 UTC](https://rubytalk.org/t/win32ole-object-creation-failure/19271 "2005-06-30T12:15:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Axel1](https://avatars.discourse-cdn.com/v4/letter/a/ecccb3/32.png) [@Axel1](https://rubytalk.org/u/Axel1)\
**Post date:** [30 June 2005 12:15 UTC](https://rubytalk.org/t/win32ole-object-creation-failure/19271/1 "2005-06-30T12:15:42Z")

</div>

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" +  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"|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)  
&nbsp;&nbsp;&nbsp;&nbsp;HRESULT error code:0x80040112  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Class is not licensed for use from C:/temp/bla.rb:3:in `new'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from C:/temp/bla.rb:3

Any ideas what could cause the problem?

Kind regards! ------------- Axel \<\>\<

---

<div class="post-metadata">

**Author:** ![gregarican](https://avatars.discourse-cdn.com/v4/letter/g/7feea3/32.png) [@gregarican](https://rubytalk.org/u/gregarican)\
**Post date:** [30 June 2005 16:35 UTC](https://rubytalk.org/t/win32ole-object-creation-failure/19271/2 "2005-06-30T16:35:42Z")

</div>

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)  
> &nbsp;&nbsp;&nbsp;HRESULT error code:0x80040112  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Class is not licensed for use from C:/temp/bla.rb:3:in `new'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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 --\>  
[Missing FileOpenDialog / FileSaveDialog - AutoItX Help and Support - AutoIt Forums](http://www.autoitscript.com/forum/index.php?s=58aec18875ef9a11f9d1b2af990a92c2&showtopic=11379&pid=80340&st=0&#entry80340%5C).

---

<div class="post-metadata">

**Author:** ![Corey\_Lawson1](https://avatars.discourse-cdn.com/v4/letter/c/3ec8ea/32.png) [@Corey\_Lawson1](https://rubytalk.org/u/Corey_Lawson1)\
**Post date:** [30 June 2005 17:57 UTC](https://rubytalk.org/t/win32ole-object-creation-failure/19271/3 "2005-06-30T17:57:15Z")

</div>

Hmm... I found a chunk of VB code here  
[http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=12394&lngWId=1](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  
&nbsp;&nbsp;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 --\>  
> > [Missing FileOpenDialog / FileSaveDialog - AutoItX Help and Support - AutoIt Forums](http://www.autoitscript.com/forum/index.php?s=58aec18875ef9a11f9d1b2af990a92c2&showtopic=11379&pid=80340&st=0&#entry80340%5C).

---

<div class="post-metadata">

**Author:** ![Axel1](https://avatars.discourse-cdn.com/v4/letter/a/ecccb3/32.png) [@Axel1](https://rubytalk.org/u/Axel1)\
**Post date:** [1 July 2005 06:40 UTC](https://rubytalk.org/t/win32ole-object-creation-failure/19271/4 "2005-07-01T06:40:42Z")

</div>

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 --\>  
> [Missing FileOpenDialog / FileSaveDialog - AutoItX Help and Support - AutoIt Forums](http://www.autoitscript.com/forum/index.php?s=58aec18875ef9a11f9d1b2af990a92c2&showtopic=11379&pid=80340&st=0&#entry80340%5C).

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

---

<div class="post-metadata">

**Author:** ![Corey\_Lawson1](https://avatars.discourse-cdn.com/v4/letter/c/3ec8ea/32.png) [@Corey\_Lawson1](https://rubytalk.org/u/Corey_Lawson1)\
**Post date:** [30 June 2005 18:05 UTC](https://rubytalk.org/t/win32ole-object-creation-failure/19271/5 "2005-06-30T18:05:20Z")

</div>

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](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](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  
> > &nbsp;&nbsp;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 --\>  
> > \> [Missing FileOpenDialog / FileSaveDialog - AutoItX Help and Support - AutoIt Forums](http://www.autoitscript.com/forum/index.php?s=58aec18875ef9a11f9d1b2af990a92c2&showtopic=11379&pid=80340&st=0&#entry80340%5C).  
> > \>  
> > \>  
> > \>

---

<div class="post-metadata">

**Author:** ![Corey\_Lawson1](https://avatars.discourse-cdn.com/v4/letter/c/3ec8ea/32.png) [@Corey\_Lawson1](https://rubytalk.org/u/Corey_Lawson1)\
**Post date:** [1 July 2005 21:13 UTC](https://rubytalk.org/t/win32ole-object-creation-failure/19271/6 "2005-07-01T21:13:16Z")

</div>

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 --\>  
> > \> [Missing FileOpenDialog / FileSaveDialog - AutoItX Help and Support - AutoIt Forums](http://www.autoitscript.com/forum/index.php?s=58aec18875ef9a11f9d1b2af990a92c2&showtopic=11379&pid=80340&st=0&#entry80340%5C).
> > 
> > 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 ...

---

<div class="post-metadata">

**Author:** ![gregarican](https://avatars.discourse-cdn.com/v4/letter/g/7feea3/32.png) [@gregarican](https://rubytalk.org/u/gregarican)\
**Post date:** [30 June 2005 18:40 UTC](https://rubytalk.org/t/win32ole-object-creation-failure/19271/7 "2005-06-30T18:40:42Z")

</div>

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](http://blade.nagaokaut.ac.jp/c%C2%ADgi-bin/scat.rb/ruby/ruby-talk/%C2%AD32861)

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