heavy rocker dude
heavy rocker dude

Reputation: 2301

Can a C# DLL call C++/CLI managed wrapper which calls native C++ static library?

I have a DLL exception I am encountering:

 response threw exception: Could not load file or assembly  or one of its dependencies. An attempt was made to load a program with an incorrect format.

I have all source code for the following DLL/library combinations. I am trying to figure if I have a strange combination or what I need to check to ensure this runs. I have a native C++ code in a static library which is called by a managed C++ CLR supported DLL. Everything seems to work fine until the following point.

I created a test C# EXE console application to call the managed C++ CLR supported DLL. This works as expected. The difference where the exception is thrown when there is a C# Class library calling this same managed C++ CLR supported DLL. Is this supported at all or is this the reason why the exception gets thrown? If this combination is incorrect, is there any workaround to get a C# Class DLL to call the managed C++ CLR supported DLL.

Everything is created with Windows 7 64 bit with Visual Studio 2012. All projects are in Release mode with x64 bit platform selected.

Upvotes: 0

Views: 719

Answers (2)

David Hope
David Hope

Reputation: 2256

In every case I've seen, this is where a 32-bit C# app is trying to load a 64-bit C++ or C++/CLI DLL or a 64-bit C# app is trying to load a 32-bit C++ or C++/CLI DLL.

The next step I usually take is to use Dependency Walker to load the C++/CLI dll. Dependency walker will show you what DLLs your DLL is trying to load. In some cases I've had a 32-bit DLL with the same name as a 64-bit DLL appear in the Path first, hence it tries to load the wrong DLL.

Note that Dependency Walker will show a 64 on the icon next to 64-bit DLLs and will show a message along the lines of: "Error: Modules with different CPU types were found." in the info box at the bottom

Upvotes: 1

Matthew Sanford
Matthew Sanford

Reputation: 1099

my best bet is that the c++ dll is compiled as win32 your c# dll is set to Any CPU and you are running on a 64 bit system...

Upvotes: 0

Related Questions