2

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

JohanB
  • 21
  • 3
  • You may just have a Core 3.1 bug that lots of people recently started getting. If using 3.1.14 try this patch : https://devblogs.microsoft.com/dotnet/net-core-april-2021-updates/?force_isolation=true – jdweng Apr 29 '21 at 12:13
  • I just installed the SDK you shared with me (SDK 3.1.408 with .NET Runtime 3.1.14, ASP.NET Core Runtime 3.1.14 and .NET Desktop Runtime 3.1.14). Unfortunately I still have the error :/ – JohanB Apr 29 '21 at 12:46
  • What version of Net library are you using to build? You need to use Net 4.7.2 or later. There are issues with older version of Net targeting Core. – jdweng Apr 29 '21 at 13:12
  • On download page for 3.1.13 says " On Windows, we recommend installing the Hosting Bundle, which includes the .NET Runtime and IIS support." So it looks like as a Kludge Microsoft fixed an issue with 3.1.13 by suggesting to install two packages. Against 3.1.14 there is no note so I've been assuming 3.1.14 was fixing the issue with 3.1.13. Maybe you should go back to 3.1.13. – jdweng Apr 29 '21 at 13:17
  • @JonasH, Ok, but if I understand correctly, my dll, in C # .NET core 3.1, is managed, right? I have to work with .NET core 3.1 to use the bluetooth of the PC. Currently my C++ project is a CLR console application. What should I do ? – JohanB Apr 29 '21 at 13:19
  • c++ CLR is managed. But probably need to be built with Net 4.7.2 or newer to work with c#. – jdweng Apr 29 '21 at 13:21
  • See following : https://devblogs.microsoft.com/cppblog/porting-a-c-cli-project-to-net-core/?force_isolation=true – jdweng Apr 29 '21 at 13:31
  • @jdweng I just installed .Net framework 4.7.2 https://dotnet.microsoft.com/download/dotnet-framework/net472 I have difficulties to understand. I am using .NET core 3.1, necessary to use the bluetooth of the PC. I use this project to make my bluetoothLE DLL : https://github.com/microsoft/Windows-universal-samples/tree/master/Samples/BluetoothLE .NET core and .NET framework are two different things, right? https://www.developpez.net/forums/attachments/p288370d1/a/a/a OK, I'll try to follow what you just shared. Thank you for your help and your very quick responses – JohanB Apr 29 '21 at 13:47
  • Net Core is a compact version of Net that is intended to be used on Mobile devices and uses less memory. Microsoft started with their Net library and removed many features and rewrote code. See : https://learn.microsoft.com/en-us/dotnet/core/compatibility/3.1?force_isolation=true You are suppose to be able with Net 4.7.2 be able to build and target Core. – jdweng Apr 29 '21 at 13:51
  • So you think I should migrate my project from .NET core 3.1 to .NET framework 4.7.2? I'm not sure I understand the value of porting my C ++ / CLI project to .NET Core. In one hand I need to keep some code in C++ because all my software is developed in C++ and in other hand I need to have a DLL allowing me to communicate in bluetooth via the bluetooth of the PC thanks to the library of Microsoft in C #. I'm so sorry, I feel like I'm really bad ... – JohanB Apr 29 '21 at 14:01
  • If your c++ dlls is compiled with CLR support (i.e. c++/cli) it should work, at least in principle. I'm not familiar enough with c++/cli in .net Core to suggest an actual fix. .Net core (and .net 5) is the replacement of .Net framework, and is not (just) for mobile devices. But both the c++ project and c# project should use the same .Net version limit possible compatibility issues. If the API you want to use is part of the "windows universal" or WinRT APIs, you should be able to access them using c++/cx, i.e. a windows universal application. – JonasH Apr 29 '21 at 14:22
  • OK, I just saw something. In the properties of my C ++ / CLI project, in configuration property / Advanced settings, in "support of common language" I have "support of common language runtime (/ clr)", then in "version of . NET Framework target" I have "v4.6". However, I am using .NET Core 3.1 and not .NET framework 4.6. So, in "Support of common language" I can choose "Support for .NET Core runtime (/ clr: netcore)" and choose ".NET Core 3.1" in "target framework of .NET Core". But, when I compile I have this error : "C ++ / CLI projects targeting .NET Core must be dynamic libraries." – JohanB Apr 29 '21 at 14:56
  • With this last error, I may be starting to understand what @jdweng wanted to explain to me, but I still have a long way to go – JohanB Apr 29 '21 at 15:01
  • You have a Project type (class library, console, windows form) and the Target Net. The project type determines if the output file is an exe or a dll. The exe and dll are very similar. The exe has a main entry point while a dll doesn't. It use to be you could use an exe instead a dll when importing to another project. I haven't tried it in a while. – jdweng Apr 29 '21 at 15:31

0 Answers0