How to interact between C # and C ++

I try to interact very thoroughly between the two languages. I basically have a high-performance code that I want to process in C ++ and then return the result to my application.

Everything will be compiled in Visual Studio.

I chose int as the input and output type, since sorting can be a bit winning and not quite what I'm dealing with.

C ++ I have:

#include "stdafx.h" // default from vs2013, no idea what it is _declspec(dllexport) int Diu(int p) { return p * 2; } 

C # I have:

 using System; namespace Interop { public class Program{ [System.Runtime.InteropServices.DllImport("Hardworker.dll")] public static extern int Diu(int p); private static void Main(string[] args) { Console.WriteLine(Diu(2)); } } } 

So this is a pretty simple example. But I get an exception:

An unhandled exception of type "System.BadImageFormatException" occurred in Interop.exe

Additional Information: An attempt was made to download the program using the wrong format. (Exception from HRESULT: 0x8007000B)

The C ++ project is created as a console application> Dll in the create dialog. I checked the C ++ dll in the disassembler and I see Diu as an exported character.

E. What did I miss about setting up interaction?

+5
source share
1 answer

When you get this error: HRESULT: 0x8007000B caused by paltform incompatibility.
Make sure your compiler profile is configured on the same platform ( x86 , x64 or AnyCPU ).

+4
source

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


All Articles