Reputation: 1357
I am using C# loading C++ dll, and got this error:
"An unhandled exception of type 'System.BadImageFormatException' occurred in MyApp.exe" "Additional information: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8057000B)
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;
}
The C# code calling it is:
[DllImport("C:\\MyNativeDLL\\x64\\Debug\\MyNativeDLL.dll", EntryPoint = "Test")]
private extern static int Test();
void doJob()
{
long a = Test(); // exception thrown here
}
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 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.
Upvotes: 0
Views: 8669
Reputation: 6050
As you mentioned: The C++ dll was generated using vs2012 wizard, win32 application, dll with pre-head. It is built with x64 option
The DLL and exe have to be both 32 bit, or both 64 bit.
Upvotes: 1