WIN32OLE error with Ruby 1.9.1

1,490 views
Skip to first unread message

pedrom

unread,
Dec 19, 2009, 4:02:16 PM12/19/09
to RubyInstaller
With Ruby 1.9.1-p243 I get this error:
WIN32OLERuntimeError at /db
failed to create WIN32OLE object from `ADODB.Connection' HRESULT error
code:0x800401f0 CoInitialize has not been called.

Same error in a Windows XP SP3 and Windows Server 2003 SP2.
Gems installed: sinatra 0.9.4 rack 1.0.1

Code:
require 'rubygems'
require 'sinatra'
require 'win32ole'

get '/db' do
connection = WIN32OLE.new('ADODB.Connection')
connection.Open('Provider=Microsoft.Jet.OLEDB.4.0; Data
Source=Libros.mdb')
recordset = WIN32OLE.new('ADODB.Recordset')
sql = 'SELECT * FROM Editoriales'
recordset.Open(sql, connection)
data = recordset.GetRows.transpose
recordset.Close
connection.close
data.inspect
end

No errors in Ruby 1.8.6-p383

Sorry my english

Luis Lavena

unread,
Dec 19, 2009, 4:22:25 PM12/19/09
to rubyin...@googlegroups.com
On Sat, Dec 19, 2009 at 6:02 PM, pedrom <p...@axon.es> wrote:

Hello.

> With Ruby 1.9.1-p243 I get this error:

Please provide the full version: ruby -v

> WIN32OLERuntimeError at /db
> failed to create WIN32OLE object from `ADODB.Connection' HRESULT error
> code:0x800401f0 CoInitialize has not been called.
>
> Same error in a Windows XP SP3 and Windows Server 2003 SP2.
> Gems installed: sinatra 0.9.4 rack 1.0.1
>

Please also isolate the Win32OLE from your web application.

Then provide the full backtrace.

> Code:
>  require 'rubygems'
>  require 'sinatra'
>  require 'win32ole'
>
>  get '/db' do
>    connection = WIN32OLE.new('ADODB.Connection')
>    connection.Open('Provider=Microsoft.Jet.OLEDB.4.0; Data
> Source=Libros.mdb')
>    recordset = WIN32OLE.new('ADODB.Recordset')
>    sql = 'SELECT * FROM Editoriales'
>    recordset.Open(sql, connection)
>    data = recordset.GetRows.transpose
>    recordset.Close
>    connection.close
>    data.inspect
>  end
>
> No errors in Ruby 1.8.6-p383

So that could mean Win32OLE differs, which may indicate a bug in Win32OLE.

Please create a minimal example that exposes this bug and can be
recreated by others.

Once you do that, we can determine if this is a bug and if so, report
properly to Ruby Redmine:

http://redmine.ruby-lang.org/
--
Luis Lavena
AREA 17
-
Perfection in design is achieved not when there is nothing more to add,
but rather when there is nothing more to take away.
Antoine de Saint-Exupéry

pete

unread,
Jan 20, 2010, 3:24:24 AM1/20/10
to RubyInstaller

I have been able to reproduce this error in a small threaded script:

require 'win32ole'

Thread.new do
begin
ie = WIN32OLE.new( "InternetExplorer.Application" )
ie.visible = true
ie.navigate( "http://google.com" )
rescue Exception => e
puts e.class
puts e
puts e.backtrace
end
end

gets

The output is:
C:\test>ruby win32ole_test.rb
WIN32OLERuntimeError
failed to create WIN32OLE object from `InternetExplorer.Application'


HRESULT error code:0x800401f0
CoInitialize has not been called.

win32ole_test.rb:5:in `initialize'
win32ole_test.rb:5:in `new'
win32ole_test.rb:5:in `block in <main>'
win32ole_test.rb:13:in `gets': Interrupt
from win32ole_test.rb:13:in `gets'
from win32ole_test.rb:13:in `<main>'


If I put the "require 'WIN32OLE'" line inside the Thread block or do
not use threads the exception is not raised.

pete

pete

unread,
Jan 20, 2010, 3:25:26 AM1/20/10
to RubyInstaller
On Dec 19 2009, 1:22 pm, Luis Lavena <luislav...@gmail.com> wrote:

Oops, I forgot to include my ruby version:

C:\test>ruby -v
ruby 1.9.1p243 (2009-07-16 revision 24175) [i386-mingw32]

pete

Luis Lavena

unread,
Jan 20, 2010, 8:29:34 AM1/20/10
to rubyin...@googlegroups.com
On Wed, Jan 20, 2010 at 5:24 AM, pete <fourt...@gmail.com> wrote:
> [...]

Script: http://pastie.org/786354

I've run this using Pik [1], results:

C:\Users\Luis\Desktop>pik ruby -w t.rb
IronRuby 0.9.3.0 on .NET 2.0.0.0

OK

jruby 1.4.0 (ruby 1.8.7 patchlevel 174) (2009-11-02 69fbfa3) (Java
HotSpot(TM) Client VM 1.6.0_17) [x86-java]

t.rb:1:in `require': no such file to load -- win32ole (LoadError)
from t.rb:1

ruby 1.8.6 (2009-06-08 patchlevel 369) [i386-mswin32]

OK

ruby 1.8.6 (2009-08-04 patchlevel 383) [i386-mingw32]

OK

ruby 1.8.7 (2009-12-24 patchlevel 248) [i386-mingw32]

OK

ruby 1.8.7 (2009-12-24 patchlevel 248) [i386-mswin32]

OK

ruby 1.9.1p376 (2009-12-07 revision 26041) [i386-mingw32]

WIN32OLERuntimeError
failed to create WIN32OLE object from `InternetExplorer.Application'
HRESULT error code:0x800401f0
CoInitialize has not been called.

t.rb:5:in `initialize'
t.rb:5:in `new'


t.rb:5:in `block in <main>'

ruby 1.9.1p376 (2009-12-07 revision 26041) [i386-mswin32]

WIN32OLERuntimeError
failed to create WIN32OLE object from `InternetExplorer.Application'
HRESULT error code:0x800401f0
CoInitialize has not been called.

t.rb:5:in `initialize'
t.rb:5:in `new'


t.rb:5:in `block in <main>'

ruby 1.9.2dev (2010-01-02 trunk 26229) [i386-mingw32]

WIN32OLERuntimeError
failed to create WIN32OLE object from `InternetExplorer.Application'
HRESULT error code:0x800401f0
CoInitialize has not been called.

t.rb:5:in `initialize'
t.rb:5:in `new'


t.rb:5:in `block in <main>'

=====

Got 5 OK's and 5 IE windows.

Windows 7, x64 bits.

None of 1.9.x series worked.

Reporting this right now to Ruby Redmine with all these results, as
this is a bug of Ruby.

Luis Lavena

unread,
Jan 20, 2010, 9:31:23 AM1/20/10
to rubyin...@googlegroups.com
On Wed, Jan 20, 2010 at 10:29 AM, Luis Lavena <luisl...@gmail.com> wrote:
> [...]

>
> Reporting this right now to Ruby Redmine with all these results, as
> this is a bug of Ruby.
>

Done:
http://redmine.ruby-lang.org/issues/show/2618

nishant kumar

unread,
Apr 26, 2012, 3:22:34 AM4/26/12
to rubyin...@googlegroups.com
Hello,

I was also facing the same issue when I tried to run an EXE asyncronously.It was giving the same error.The problem is the coinitialize function(http://msdn.microsoft.com/en-us/library/windows/desktop/ms678543(v=vs.85).aspx)  is not being called which initializes the COM librrary on the current thread.

The solution is to first call the initialize function on the WIN32OLE class.

This can be done by WIN32OLE.ole_initialize.Just place this code before WIN32OLE.new.

Hope This helps.

Thanks,
Nishant

nishant kumar

unread,
Apr 26, 2012, 3:23:18 AM4/26/12
to rubyin...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages