我有兩個非託管C++函數,Compress
和Decompress
。參數和返回去如下:如何將兩個非託管C++函數包裝到兩個託管C#函數中?
無符號字符* Compress
(無符號字符*,INT)
無符號字符* Decompress
(無符號字符*,INT)
所有uchars是uchars的陣列。
有人可以幫我擺出一種方法來使用字節[]數組而不是unsigned char *將這些轉換爲託管的C#代碼嗎?非常感謝你!
我有兩個非託管C++函數,Compress
和Decompress
。參數和返回去如下:如何將兩個非託管C++函數包裝到兩個託管C#函數中?
無符號字符* Compress
(無符號字符*,INT)
無符號字符* Decompress
(無符號字符*,INT)
所有uchars是uchars的陣列。
有人可以幫我擺出一種方法來使用字節[]數組而不是unsigned char *將這些轉換爲託管的C#代碼嗎?非常感謝你!
您應該能夠以byte []形式傳遞unsigned char *參數,標準P/Invoke編組人員應該處理該參數。你將不得不自己封送輸出的unsigned char *,但這只是對Marshall.Copy()的調用。請參閱下面的例子,瞭解我的想法。
兩大問題:
樣品:
[DllImport("Name.dll")]
private static extern IntPtr Compress([MarshalAs(UnmanagedType.LPArray)]byte[] buffer, int size);
[DllImport("Name.dll")]
private static extern IntPtr Decompress([MarshalAs(UnmanagedType.LPArray)]byte[] buffer, int size);
public static byte[] Compress(byte[] buffer) {
IntPtr output = Compress(buffer, buffer.Length);
/* Does output need to be freed? */
byte[] outputBuffer = new byte[/*some size?*/];
Marshal.Copy(output, outputBuffer, 0, outputBuffer.Length);
return outputBuffer;
}
public static byte[] Decompress(byte[] buffer) {
IntPtr output = Decompress(buffer, buffer.Length);
/* Does output need to be freed? */
byte[] outputBuffer = new byte[/*some size?*/];
Marshal.Copy(output, outputBuffer, 0, outputBuffer.Length);
return outputBuffer;
}
謝謝!這應該現在正常工作,我有一些功能,可以獲得解壓縮輸出的大小以及壓縮輸出。我唯一的問題是我如何在C#中釋放輸出。 – Gbps 2010-05-06 02:23:24
不太清楚:你要一)重塑功能,B)寫在託管C爲他們包裝++或C)將它們編譯成非託管DLL和調用它們.NET使用P/Invoke? – 2010-05-06 01:56:17
託管和非託管在問題標題上是多餘的。 你有什麼是你需要包裝到C#中的兩個C++函數 – Panic 2010-05-06 02:12:05
我想使用P/Invoke從.NET調用它們,我很抱歉沒有足夠清楚。 – Gbps 2010-05-06 02:24:23