1
我有dllimport的调用C zlib的解压缩从C#
[DllImport("../../zlib-1.2.5/zlib1.dll", CallingConvention = CallingConvention.Cdecl)]
static extern int uncompress(byte[] destBuffer, ref uint destLen, byte[] sourceBuffer, uint sourceLen);
,并调用它像
uint _dLen = (uint) 8192;
byte[] data = compressed_data;
byte[] _d = new byte[_dLen];
if (uncompress(_d, ref _dLen, data, (uint)data.Length) != 0)
return null;
中的zlib的解压缩功能看起来像
unsigned int ZEXPORT uncompress (dest, destLen, source, sourceLen)
unsigned char *dest;
Uint32 destLen;
unsigned char *source;
Uint32 sourceLen;
{
z_stream stream;
int err;
stream.next_in = source;
stream.avail_in = sourceLen;
/* Check for source > 64K on 16-bit machine: */
if ((Uint32)stream.avail_in != sourceLen) return Z_BUF_ERROR;
stream.next_out = dest;
stream.avail_out = destLen;
if ((Uint32)stream.avail_out != destLen) return Z_BUF_ERROR;
stream.zalloc = (alloc_func)0;
stream.zfree = (free_func)0;
err = inflateInit(&stream);
if (err != Z_OK) return err;
err = inflate(&stream, Z_FINISH);
if (err != Z_STREAM_END) {
inflateEnd(&stream);
if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0))
return Z_DATA_ERROR;
return err;
}
err = inflateEnd(&stream);
return stream.total_out;
}
但我总是落得在c#端接收空值。
编辑:错误是z_data_error。
edit2:Z_DATA_ERROR如果输入数据已损坏。
我是否需要将字节[]数组编组为非托管指针? 或者有什么问题?我的输入数组无效吗?
问候
为什么不直接使用['zlib.net'](http://www.componentace.com/zlib_.NET.htm)而不是在非托管库上编写托管包装? – 2012-04-23 14:36:01
嗨,压缩和解压缩函数从原始的zlib函数被修改,所以我必须构建dll并使用它 – Gobliins 2012-04-23 14:38:59
看起来一见钟情。如果你有DLL的源代码在调试中编译它,并启动一个调试“混合模式”会话,以找出发生了什么问题。 – Drake 2012-04-24 09:54:57