我正在将 C++ Dll 调用到 C# 中并遇到了问题
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;
}
when
int len = HuffCompress(ref packet, 12, packet.Length-12);
运行时,我收到 BadImageFormatException
由于C#编辑器是VSC# Express,它不编译64位程序,所以我不确定这个问题
任何想法都会很棒
Express 版本中缺少的平台目标设置几乎肯定是您的问题。您必须手动编辑项目的 .csproj 文件。运行 notepad.exe 并打开 .csproj 文件。找到如下所示的属性组:
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
并添加这一行:
<PlatformTarget>x86</PlatformTarget>
对位于其下方的发布配置组重复此操作。
你的下一个问题是函数的名称,如果你用 C++ 编译它,它就会被修饰。像这样声明它:
extern "C" __declspec(dllexport)
int __stdcall HuffCompress(unsigned char *buff, int offset, int len);
并且您的 C# 声明是错误的,请删除ref第一个参数上的关键字。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)