2012-04-26 88 views
0

我仍然努力從c靜態庫中調用一些函數。 我寫了一個包裝在這個toms wrapper後面的視覺工作室周圍的靜態庫,現在可以訪問c#端的功能,但我在處理它們時遇到一些錯誤。編組c#數組指向靜態c庫的指針

因此,這裏是什麼功能看起來原文:

ZEXTERN Int32 ZEXPORT compressZIP OF((unsigned char *dest, Uint32 destLen, 
          unsigned char *source, Uint32 sourceLen, 
          Uint32 *_crc32)); 

導致在C#方

public int compressZIP(byte* dest, uint destLen, byte* source, uint sourceLen, 
          uint* _crc32); 

所以,現在我被困在指針上的問題,我想的東西出像

byte[] data = new byte[size] 
(fixed ptr = data) 

並將ptrs傳入函數。但我總是最終導致一些內存違規或其他內存異常。

當我打電話uncompressZIP這看起來有點相同,我得到2個字符正確無壓縮,但只有柵欄的東西。

有人可以幫我填寫正確的功能參數嗎?

回答

1

不要將它聲明爲C#端的指針,而是將其聲明爲一個字節數組。並且一定要包含OutAttribute

工作示例:

[DllImport("PixelFlowIE.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "PixelFlow")] 
private static extern void PixelFlowDLL([In, Out] Node[] gi, int width, int height, SourceInfo[] sources, int sourceCount, int iterations, int iterPerPeriod, ProgressCallback prg); 
+0

不容因爲it's管理的C/CLI和沒有dllimport的。 – Gobliins 2012-04-27 12:45:17

+0

但無論如何,這個問題似乎是在c方面 – Gobliins 2012-04-27 12:45:44