1

當我在當時使用win32ole作爲獨立應用程序時,一切似乎都正常工作,只要我把它放到我的rails應用程序中,無限循環。使用WIN32OLE在紅寶石軌道上使用WIN32OLE渲染Word文檔

我試圖訪問「的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 

當我在那個時候單獨運行這段代碼放置一次拉網來爲微軟共享點憑據。然而在雜種的軌道上它進入了無限循環。

我需要處理這個彈出來通過Rails出現嗎?

+0

進展 - 當我去到Windows環境中的「服務」 - 在Mongrel服務屬性中,「登錄 - >允許與桌面交互」中有一個選項當我檢查它並嘗試運行我的代碼時,我得到了「交互式服務檢測 - 在這臺計算機上運行的程序試圖顯示一條消息[內容]該程序可能需要您或您的許可才能完成任務。爲什麼會發生? [V]顯示程序詳細信息[查看此消息] [稍後問我問題]「那麼如何在瀏覽器中獲得此服務提示並且不在後臺?我是否正確地進入了正確的方向? –

回答

0

您是否打算修補win32ole.rb文件?

基本上,這裏的修補程序的原因:

t turns out that win32ole.rb patches the thread to call the windows OleInitialize() & OleUninitialize() functions around the yield to the block. However, the MS documentation for CoInitialize (which OleInitialize calls internally) state that: "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." 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

+0

是的,我之前看到過這個,但是沒有理解當我進入服務的時候,我已經理解了它,所以你的意思是我必須使用這段代碼來管理線程,並且必須只對win32ole.rb使用這個修補程序並且使用它? –

+0

我正在使用redmine。創建了win32ole.rb並把這個代碼放在它裏面我把這個文件放在初始化文件夾中我重新啓動了Mongrel服務,但是當我嘗試訪問服務器localhost:3000時它沒有響應我需要刪除那些日誌從服務? –

+0

我已經在我的雜記日誌「/config/initializers/win32ole.rb:12:未定義的方法'ole_initialize'爲WIN32OLE:Class(NoMethodError)」調試它,當我進一步挖掘它時,我有發現紅寶石版本[ 1.8.7]我使用的源代碼本身沒有win32ole.rb文件。即http://rxr.whitequark.org/mri/source/ext/win32ole/lib/win32ole.rb?v=1.8.7 –