0
votes

I am developing a C++/CLI wrapper, for a dll which has been developed in QT 4.5 . I have only the QT dll and its header file. The QT dll is a 32 bit dll.

When I am trying to call my C++/CLI wrapper from my C# console application , I am seeing the BadImageException.

using GWrapper;

 class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("WELCOME");

            GWrapper wrapper = new GWrapper();

            wrapper.CallAMethod(someText);
        }

   }

If I keep only the Console.Writeline("WELCOME") there is no exception

I am on Windows 7 64 bit, and would like to keep x86 as the preferred option, as the application needs to run on 64 bit systems as well.

Project settings :

Wrapper in C++/CLI - Win32

C# console application - x86

Output from CORFLAGS

C++/CLI wrapper ( Output is a DLL, and references a 32 bit QT dll )

Version : v4.0.30319

CLR Header: 2.5

PE : PE32

CorFlags : 16

ILONLY : 0

32BIT : 0

Signed : 0

I set the 32 bit flag for the DLL to 1, but the exception was still there. When I rebuilt the dll, its was set to 0 again.

C# Console application ( Output is a EXE)

Version : v4.0.30319

CLR Header: 2.5

PE : PE32

CorFlags : 3

ILONLY : 1

32BIT : 1

Signed : 0

How do I make the console application work with the wrapper ?

1
Did you check out msdn.microsoft.com/en-us/library/… There are some possible reasons listed.Silicomancer
I tried,but but the exception is being thrown at the program startSujay Ghosh
Are you sure you are not compiling your console app as ANY CPU? Did you doublecheck the Configuration Manager if for sure both projects compile as c# --> x86 and c++/cli --> Win32t3chb0t
Finally I made it work. It was not because of architecture, it was because a QT dll was missing. Such exception can be so misleading at times. Thanks for all your comments.Sujay Ghosh

1 Answers

1
votes

This happens when the program can't find a dll or one of its dependencies.
Most often in a mixed native-managed project the missing dependencies are the native dlls that aren't getting copied in the build process.

Referenced .Net libraries are usually copied to the output compilation directory, while the native dependencies aren't copied by Visual Studio by default.