2013-01-17 15 views
0

我在定義無符號字符*緩衝區屬性的文件IPlan.idl中定義了一個COM接口。編組來自COM接口的無符號字符*

[ 
    object, 
    uuid(...), 
    dual, 
    helpstring("..."), 
    pointer_default(unique) 
] 
interface IPlan : IDispatch 
{ 
    ... 
    [id(28), helpstring("method SetByte") 
    HRESULT SetByte([in] long index, [in] unsigned char buffer); 

    [id(29), helpstring("method GetByte") 
    HRESULT GetByte([in] long index, [out, retval] unsigned char * pBuffer); 

    [id(30), propget, helpstring("property Buffer")] 
    HRESULT Buffer([out, retval] unsigned char** pBuffer); 
    [id(30), propput, helpstring("property Buffer")] 
    HRESULT Buffer([in] unsigned char* newBuffer); 
    ... 
} 

我的實現類MyPlan是在C#中定義的。這個類講述了一個C++ COM類,CProcessor。 CProcessor將一些數據計算爲無符號字符*緩衝區。一旦CProcessor中的所有數據都存儲在其緩衝區中,該數據就會被加載到MyPlan對象中。

爲IPLAN元數據定義了下面的函數簽名:

public interface IPlan 
{ 
    ... 
    byte GetByte(int index); 
    void SetByte(int index, byte buffer); 
    IntPtr get_Buffer(); 
    void set_Buffer(ref byte pBuffer); 
    ... 
} 

我能夠使用了getByte和SetByte從CProcessor訪問和MyPlan修改一個byte []變量。我也可以通過編組byte []變量來使用get_Buffer()。

我的問題是我怎麼能利用

set_Buffer(ref byte pBuffer) 

功能?當我在CProcessor中調用它時,pBuffer只包含緩衝區的第一個字節(如對SetByte(0,data)的調用)。我認爲我需要像二傳手一樣編組二傳手,但是我的搜索已經空了。我已經嘗試過

set_Buffer([MarshalAs(UnmanagedType.LPStr)] ref byte pBuffer); 

但這似乎沒有做任何事情傳遞給整個數組。

MarshalAs(UnmanagedType.LPStr) - how does this convert utf-8 strings to char*

Problem using dll in c#

回答

1

我能夠使用

[id(1), propget, helpstring("Buffer")] 
HRESULT Buffer([out, retval] VARIANT* pBuffer); 
[id(1), propput, helpstring("Buffer")] 
HRESULT Buffer([in] VARIANT pBuffer); 

從C++通過無符號的字符陣列,以C#。 C#只需要強制轉換爲字節[]

this.buffer = (byte[])myPlan.Buffer; 
相關問題