2014-12-04 40 views
1

我想使用Ruby v1.93在COM對象中設置數組屬性。將數組從一個數組編組到一個Ruby數組中是行得通的,但不是在另一個方向上。我如何將一個Ruby數組放入COM中?使用Ruby和Win32OLE設置數組

屬性包含在.NET程序集:

namespace LibForRuby 
{ 
    public class MyClass 
    { 
     public MyClass() 
     { 
     MyInt = 1; 
     MyArray = new[] {2, 3}; 
     } 

     public int MyInt { get; set; } 
     public int[] MyArray { get; set; } 
    } 
} 

我的整個Ruby腳本是:

require 'win32ole' 

com_class = WIN32OLE.new('LibForRuby.MyClass') 

puts 'Before:' 
my_int = com_class.MyInt 
puts my_int 
my_array = com_class.MyArray 
print my_array 
puts 

puts 'After:' 
com_class.MyInt = 10 
my_int = com_class.MyInt 
puts my_int 
com_class.MyArray = [20,30] 
my_array = com_class.MyArray 
print my_array 

輸出是:

C:\Ruby193\bin>test 
Before: 
1 
[2, 3] 
After: 
10 
C:/Ruby193/bin/test.rb:13:in `method_missing': (in setting property `MyArray':) 
(WIN32OLERuntimeError) 
    OLE error code:0 in <Unknown> 
     <No Description> 
    HRESULT error code:0x80020005 
     Type mismatch. 
     from C:/Ruby193/bin/test.rb:13:in `<main>' 

回答

0

試試這個:

puts 'After:' 
com_class.MyInt = 10 
my_int = com_class.MyInt 
puts my_int 
com_class._setproperty(
com_class.ole_method_help('MyArray').dispid, 
[[20,30]], 
[WIN32OLE::VARIANT::VT_ARRAY] 
) 
my_array = com_class.MyArray 
print my_array 

請確保在ruby-doc.org上查找#_setproperty方法,以瞭解數組爲什麼包含在數組中以及爲什麼變量常量在數組內。

+0

嗯......你的新代碼是由代碼行我已經在嘗試之前他在帖子中顯示的錯誤。假設你打算省略那一行,我試圖在沒有它的情況下運行,但是對setproperty的調用失敗:在'ole_method'中:找不到MyArray。 – 2014-12-05 21:09:12

+0

實際上,print com_class.ole_get_methods給了我[]。 – 2014-12-05 21:32:40

+0

哎呀,意在刪除失敗的線路。可憐的複製/粘貼作業。你有沒有試過運行這個(編輯)的代碼呢?什麼是結果和/或錯誤? – 2014-12-06 03:01:59

0

有點晚了,但我有同樣的問題而努力,因爲我實現了一些加密算法爲紅寶石引擎沒有OpenSSL

這不起作用設定的System.Security.Cryptography.TripleDESCryptoServiceProvider關鍵:

crypto = WIN32OLE.new("System.Security.Cryptography.TripleDESCryptoServiceProvider") 
crypto.key = [62,116,108,70,56,97,100,107,61,51,53,75,123,100,115,97] 

這並不工作:com_class.MyArray = [20,30],它產生T:

arr = [1,2,3,4] 
crypto = WIN32OLE.new("System.Security.Cryptography.TripleDESCryptoServiceProvider") 
vArr = WIN32OLE_VARIANT.array([arr.length],WIN32OLE::VARIANT::VT_UI1) 
arr.each_with_index {|val,index| vArr[index]=val} 
crypto.key = vArr 
相關問題