【发布时间】:2017-03-28 16:36:47
【问题描述】:
我正在尝试从 C# 调用用 C 编写的外部 DLL 函数,这是来自 C 的 DLLEXPORT 代码:
DLLEXPORT int DLLCALL Compress(int compressLevel, const unsigned char *srcBuf, unsigned char **outBuf, unsigned long *Size);
这是我在 C# 中调用该函数的代码:
[DllImport("test.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int Compress(int compressLevel, ref byte[] srcBuf, ref byte[] outBuf, Uint64 size);
...
byte[] buffer = new byte[1000];
byte[] _compressedByteArray = null;
Uint64 OutSize = 0;
Compress(10, buffer , compressedByteArray, OutSize);
但是我的调用代码出现错误:“尝试读取或写入受保护的内存。这通常表明其他内存已损坏。”
我的声明是否有任何错误?任何解决此问题的想法将不胜感激。
【问题讨论】:
-
您的代码有缺陷。尝试调试它。我们不能。我们甚至看不到它。
-
@Ðаn 我已经用 C# 代码更新了我的问题。这是一个自定义 dll,它没有在 pinvoke.net 上声明信息。