c# – BadImageFormatException

c# – BadImageFormatException,第1张

概述我正在使用C#调用C#并遇到问题 C功能: int _declspec(dllexport) CompressPacket(unsigned char *buff, int offset, int len); C#功能: [DllImport("HuffCompress.dll")] private static extern unsafe int HuffCompress 我正在使用C#调用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 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,packet.Length-12);

运行,我得到一个BadImageFormatException

由于C#编辑器是VSC#Express,它不能编译64位程序,所以我不确定这个问题
任何想法都会很棒

解决方法 Express版中缺少的Platform Target设置几乎肯定是您的问题.您必须手动编辑项目的.csproj文件.运行notepad.exe并打开.csproj文件.找到如下所示的属性组:

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'DeBUG|Anycpu' ">

并添加此行:

<PlatformTarget>x86</PlatformTarget>

重复下面的Release配置组.

你的下一个问题是函数的名称,如果你用C编译它就会被装饰.声明如下:

extern "C" __declspec(dllexport)  int  __stdcall HuffCompress(unsigned char *buff,int len);

并且您的C#声明是错误的,将ref关键字放在第一个参数上.

总结

以上是内存溢出为你收集整理的c# – BadImageFormatException全部内容,希望文章能够帮你解决c# – BadImageFormatException所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1221830.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存