简体   繁体   中英

How can I use WIN32OLE safely from a rails app?

I'm trying to use the win32ole gem in a Rails 3 app and running into some problems that I think are thread-related. Basically, I have code like this that I use to get user-specific data from our Active Directory store:

root = WIN32OLE.connect("LDAP://RootDSE")

But this gives a sporadic error when run as part of a Rails request (I think the first request after the Rails app has been restarted always works). It works fine every time when I run this code from a standalone Ruby app. I understand from a bit of Googling that this is down to the fact the OLE/COM object can only be used in the thread it was first initialised in (something to do with it running in an STA?). From that I'm guessing that this is something to do with Rails 3 running different requests on separate threads? (My Rails app is running on a Windows box.)

The error thrown by the WIN32OLE.connect call is 'connect': failed to parse display name of moniker .

Any ideas how I can fix this?

I finally resolved this problem thanks to the information & code here . I modified my win32ole.rb file as suggested there to barf if CoInitialize wasn't first called on the main thread. I achieved that in my Rails 3 app by adding a Ruby file to the config/initializers directory containing just

require 'win32ole'

That seems to have fixed it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM