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 开发者_如何学Cthat 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.
精彩评论