加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – BadImageFormatException

发布时间:2020-12-15 23:58:35 所属栏目:百科 来源:网络整理
导读:我正在使用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); ... priv
我正在使用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关键字放在第一个参数上.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读