Using .NET Core 3.1 C# dll in C++ project

1.5k Views Asked by At

I'm a software developer in a generalist company. We are a small team and I inherit some 20 year old software (like in a lot of companies I guess)

This software is coded in C / C ++, with Visual Studio 2010. Its purpose is to communicate with devices in USB, Ethernet and bluetooth. Until then we used a Bluetooth dongle to communicate with Bluetooth equipment. Today I have to integrate communication via bluetooth integrated into the PC. For that, I used the example proposed by Microsoft, coded in C # :

https://github.com/microsoft/Windows-universal-samples/tree/master/Samples/BluetoothLE

In order to get my hands on, I made a small tool that allows me to communicate with our equipment. Now I would like to integrate the communication part into our software as a DLL.

So I'm trying to get software coded in C / C ++ (with Visual Studio) to communicate with a DLL in C#, with .NET Core 3.1 as framework.

First, I wanted to start from scratch and create a simple console application in C ++ using a function built into a C # .NET Core 3.1 DLL. This allows me to validate C ++ / C # communication. However, I've been on the subject for a week and I'm starting to lose hope ...

Here are some sources that I found :

https://en.wikipedia.org/wiki/C%2B%2B/CLI https://www.codeproject.com/Questions/476583/callplusc-23plusdllplusorplusclasspluswithinplusc Using C# dll in C++ code https://learn.microsoft.com/fr-fr/dotnet/core/native-interop/expose-components-to-com

But I am not able to put all of this into practice.

The last test I did is the following:

MyDll in C# .NET Core 3.1:

namespace MyDLL
{
    public class Class1
    {
        public static double add(double a, double b)
        {
            return a + b;
        }
    }
}

My C++ project :

#include "pch.h"
#include "stdafx.h"
using namespace System;
#using "MyDLL.dll"

int main(array<System::String ^> ^args)
{
    double x = MyDLL::Class1::add(40.1, 1.9);
    return 0;
}

My application and my DLL compile. The application finds the DLL well and apparently has no problem finding the add function of my DLL. However when I launch my application I have this errorin mcrtexe.cpp, line 247 :

System.IO.FileNotFoundException: 'Could not load file or assembly' System.Runtime, Version = 4.2.2.0, Culture = neutral, PublicKeyToken = b03f5f7f11d50a3a 'or one of its dependencies. The specified file can not be found.'

I am ready to test another way. All I want is to be able to use my DLL in C # using .NET Core 3.1 with my software in C ++. If you have any suggestions, I am listening.

Best regards

0

There are 0 best solutions below