Contango
Contango

Reputation: 80372

Calling a C++ .dll from C# throws a runtime error

The following line is generating a runtime error in a C# GUI:

int x = myclass.number_from_dll();

I'm using Microsoft Visual Studio 2008.

The code in C# is:

class myclass
{
  [DllImport("strat_gr_dll.dll", EntryPoint = "number_from_dll")]
  public static extern int number_from_dll();
}

The code in the C++ .dll is:

// This is an example of an exported function.

DLL int number_from_dll(void)
{
  return 42;
}

The runtime error from .NET is:

An attempt was made to load a program with an incorrect format.
(Exception from HRESULT: 0x8007000B)

Upvotes: 1

Views: 597

Answers (1)

Hans Passant
Hans Passant

Reputation: 942297

Project + Properties, Build tab, Platform Target = x86.

Your C/C++ DLL was compiled in 32-bit mode. But your C# program is running on a 64-bit version of Windows and will run in 64-bit mode. That mix don't match. Creating a 64-bit version of you DLL is another solution. Build + Configuration Manager, Platform combo, New, x64.

Upvotes: 4

Related Questions