views:

34

answers:

1

Hi all,

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)
+3  A: 

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.

Hans Passant
Thank you soooooo much - I just spent the last 4 hours trying to track down this problem, you're a legend!
Gravitas