views:

414

answers:

1

I created a C++/CLI assembly that creates a wrapper around native C++ code. The resource compiles and the assembly loads fine into my C# project when I add it as a resource. I can access my objects and intellisense from within my application, but when attempting to build, it crashes with the exception:

BadImageFormat

Could not load file or assembly 'MyCLI, Version=1.0.3680.28432, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format.

I load it into my form load event:

MyCLI.myCLI z;

... and when I compile, it crashes on this line in my main constructor in C#

Application.Run(new Form1());

Does anyone have an idea of what could be causing this exception?

Thanks

+4  A: 

You are trying to run this code on a 64-bit operating system. Your C# code will get nicely compiled to 64-bit machine code. But you'll hit the wall when it tries to load a 32-bit C++/CLI assembly.

In the C# project, use Project + Properties, Application tab, Platform Target = x86. Creating a 64-bit version of your C++/CLI assembly is possible too, use Build + Configuration Manager. Using Platform Target is the better solution.

Hans Passant
I'm absolutely sure that's it now that you pointed it out -- however, I cannot get my C++/CLI project to show anything else than Win32 and my C# project has the platform target dropdown disabled. Also note I am on a 64 bit operating system
George
Disabled? That's *very* strange. You can use Corflags.exe to do the same. Re-run VS setup to get the 64-bit C/C++ compiler, it is not installed by default.
Hans Passant
If you would like to compile a C++ project to x64, you need to read through some articles, such as this one, http://msdn.microsoft.com/en-us/library/h2k70f3s(VS.80).aspx
Lex Li
Worked, thanks!
George