2012-04-26 84 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