C#调用VC dll 出现“System.BadImageFormatException”

来源:互联网 发布:卖家如何申请淘宝介入 编辑:程序博客网 时间:2024/05/29 04:17
namespace Hello_seesharp
{
    class Program
    {
        [DllImport("Read-WriteString.dll", EntryPoint = "Add",
         ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
        public static extern int Add(int a, int b);

        static void Main(string[] args)
        {
            int c = Add(3, 2);
            Console.WriteLine("hello,world");
            Console.Write(c);
            Console.Read();
            
        }
    }

}


在调试程序时,出现如下错误,(前提Read-WriteString.dll已放到bin目录下面)。

“System.BadImageFormatException”类型的未经处理的异常出现在 Hello-seesharp.exe 中。

其他信息: 试图加载格式不正确的程序。 (异常来自 HRESULT:0x8007000B)


经检查发现,原来编写vc dll时用的是x86平台,即32位

而编译C#时用的x64, 即64位

我们可以将C#切换到x86平台编译,编译通过。


原创粉丝点击