我在C#中调用C++ Dlls时遇到了一个问题
C++函数:
int _declspec(dllexport) CompressPacket(unsigned char *buff, int offset, int len);C#函数:
[DllImport("HuffCompress.dll")]
private static extern unsafe int HuffCompress(ref byte[] buff, int offset, int len);
...
private unsafe byte[] CompressPacket(byte[] packet)
{
int len = HuffCompress(ref packet, 12, packet.Length-12);
byte[] compressed = new byte[len];
for (int i = 0; i < len; i++)
compressed[i] = packet[i];
return compressed;
}什么时候
int len = HuffCompress(ref packet, 12, packet.Length-12);
运行时,我会得到一个BadImageFormatException
因为C#编辑器是VSC# Express,它不编译64位程序,所以我不确定这个问题,任何想法都是很好的
发布于 2011-04-21 07:11:27
Express版中缺少的平台目标设置几乎肯定是您的问题所在。您必须手动编辑项目的.csproj文件。运行notepad.exe并打开.csproj文件。找到如下所示的属性组:
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">然后添加这一行:
<PlatformTarget>x86</PlatformTarget>对其下方的Release configuration组重复上述步骤。
你的下一个问题是函数的名称,如果你在C++中编译它,它就会被修饰。像这样声明它:
extern "C" __declspec(dllexport)
int __stdcall HuffCompress(unsigned char *buff, int offset, int len);并且您的C#声明是错误的,请在第一个参数上删除ref关键字。
发布于 2011-04-21 06:06:24
DLL已损坏或它是错误的bitness。32位和64位模块不能混合使用。
https://stackoverflow.com/questions/5737134
复制相似问题