How to call C ++ dll from a Windows C # application project

I created a dll in C ++ using the class library project in Visual Studio. I need to call a method in a dll from a C # application.

I found out that there are 2 endorsements. One of them is to add a link to the dll project to the C # project or use the DllExport method for export. However, when I tried in both cases, it always gives the following error when the dll method is called at runtime.

An unhandled exception of type "System.BadImageFormatException" occurred in TestClient.exe Additional information: An attempt was made to load a program with the wrong format. (Exception from HRESULT: 0x8007000B)

May I know how to avoid this problem?

Thanks in advance!

+3
source share
1 answer

This error means that you are trying to load a 32-bit DLL into a 64-bit process or a 64-bit DLL into a 32-bit process. On Windows, DLL bits must match the process bit for proper loading.

Is your native dll 32 or 64 bit? In the C # project build settings, what platform are you targeting? If you go to the properties of the C # project, you can go to the "Assembly" tab and change the "Platform Target" to something specific, such as x86 or x64, to match the platform for which your native DLL was created.

DLL #. # AnyCPU, 32- 32- Windows 64- 64- Windows. - 32- 64- DLL.

# - (x86, x64), DLL .

+5

Source: https://habr.com/ru/post/1747516/


All Articles