1

当我当时使用 win32ole 作为独立应用程序时,一切似乎都运行良好,一旦我放入运行在 mongrel 服务器上的 rails 应用程序,它就会进入无限循环。

我正在尝试访问“https://microsoft/sharepoint/document.doc”

def generatertm(issue) 
begin 
  word = WIN32OLE.new('word.application') 
  logger.debug("Word Initialized...") 
  word.visible = true 
  myDocLink = "https://microsoft/sharepoint/url.doc" 
  myFile = word.documents.open(myDocLink) 
  logger.debug("File Opened...") 
  puts "Started Reading bookmarks..." 
  myBookMarks = myFile.Bookmarks puts "bookmarks fetched working background task..."

  print ("Bookmakr Count : " + myBookMarks.Count.to_s + "\n")

  myBookMarks.each do |i|
    logger.warn ("Bookmark Name : " + i.Name + "\n")
  end
rescue WIN32OLERuntimeError => e
  puts e.message
  puts e.backtrace.inspect
  else
ensure

word.activedocument.close( true )  # presents save dialog box
#word.activedocument.close(false) # no save dialog, just close it
word.quit
end
end

当我当时单独运行此代码时,会弹出一个弹出窗口以获取 Microsoft 共享点凭据。但是在 mongrel rails 中它会进入无限循环。

我是否需要处理此弹出窗口才能通过 Rails 出现?

4

1 回答 1

0

您是否考虑过修补 win32ole.rb 文件?

基本上,这是补丁的原因:

事实证明,win32ole.rb 修补线程以在块的 yield 周围调用 windows OleInitialize() 和 OleUninitialize() 函数。但是,CoInitialize(OleInitialize 内部调用)的 MS 文档指出:“应用程序中调用 CoInitialize 的第一个线程为 0(或 CoInitializeEx 与 COINIT_APARTMENTTHREADED)必须是最后一个调用 CoUninitialize 的线程。否则,对 CoInitialize 的后续调用STA 将失败,应用程序将无法运行。” http://msdn.microsoft.com/en-us/library/ms678543(v=VS.85).aspx

这是修改后的 win32ole.rb 文件以修复线程问题:

require 'win32ole.so'

# Fail if not required by main thread.
# Call OleInitialize and OleUninitialize for main thread to satisfy the following:
#
# The first thread in the application that calls CoInitialize with 0 (or CoInitializeEx with COINIT_APARTMENTTHREADED)
# must be the last thread to call CoUninitialize. Otherwise, subsequent calls to CoInitialize on the STA will fail and the
# application will not work.
#
# See http://msdn.microsoft.com/en-us/library/ms678543(v=VS.85).aspx
if Thread.main != Thread.current
  raise "Require win32ole.rb from the main application thread to satisfy CoInitialize requirements."
else
  WIN32OLE.ole_initialize
  at_exit { WIN32OLE.ole_uninitialize }
end


# re-define Thread#initialize
# bug #2618(ruby-core:27634)

class Thread
  alias :org_initialize :initialize
  def initialize(*arg, &block)
    if block
      org_initialize(*arg) {
        WIN32OLE.ole_initialize
        begin
          block.call(*arg)
        ensure
          WIN32OLE.ole_uninitialize
        end
      }
    else
      org_initialize(*arg)
    end
  end
end

http://cowlibob.co.uk/ruby-threads-win32ole-coinitialize-and-counin

于 2012-05-02T12:17:41.310 回答