试图加载格式不正确的程序 [英] An attempt was made to load a program with an incorrect format
问题描述
我使用C#加载C ++ DLL,并得到这个错误:
I am using C# loading C++ dll, and got this error:
MyApp.exe中发生类型为'System.BadImageFormatException'的未处理的异常
附加信息:尝试加载格式不正确的程序(来自HRESULT的异常:0x8057000B)
没有找出为什么。C ++ dll是使用vs2012向导,win32应用程序,dll与pre-head生成的。它是用x64选项构建的。这是代码:
I could not figure out why. The C++ dll was generated using vs2012 wizard, win32 application, dll with pre-head. It is built with x64 option. Here is the code:
// MyNativeDLL.cpp : Defines the exported functions for the DLL application.
//
#include "stdafx.h"
//char* pMemoryBuffer = NULL;
using namespace std;
__declspec(dllexport) long Test()
{
long a;
a = 1;
a++;
return a;
}
调用它是:
[DllImport("C:\\MyNativeDLL\\x64\\Debug\\MyNativeDLL.dll", EntryPoint = "Test")]
private extern static int Test();
void doJob()
{
long a = Test(); // exception thrown here
}
C#代码是使用任何CPU选项构建的,正在加载x64本机dll。
我想知道我在哪里做错了?我一直在努力很长,但真的卡在这里。谢谢!
C# code is built with Any CPU option, and it is loading the x64 native dll. I wondering where I did wrong? I have been trying long, but really get stuck here. Thanks!
UPDATE
当我使用win 32选项编译我的本地dll并设置正确的dll路径, 。但是当我编译我的本机dll与x64选项,加载正确的路径,加载失败。
UPDATE When I compile my native dll using win 32 option, and set up correct dll path, it loads successfully. But when I compile my native dll with x64 options, load with correct path, the loading fails.
推荐答案
如前所述:
C ++ dll是使用vs2012向导,win32应用程序,dll前头。它是用x64选项构建的
DLL和exe必须同时为32位或64位。
The DLL and exe have to be both 32 bit, or both 64 bit.
这篇关于试图加载格式不正确的程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!