2014-12-04 1 views
1

Ruby v1.93을 사용하여 COM 객체에서 배열 속성을 설정하려고합니다. 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; } 
    } 
} 

내 전체 루비 스크립트입니다 :

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가 없습니다. –

+0

사실, print com_class.ole_get_methods는 나에게 []을줍니다. –

+0

죄송합니다. 실패한 행을 삭제하려고했습니다. 불쌍한 복사/붙여 넣기 작업. 이 (편집 된) 코드를 아직 실행 해 보셨습니까? 결과 및/또는 오류가 무엇입니까? –

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 
관련 문제