-2

this question has been asked before and the accepted solution (after VS2017) was "use COM interop", however is there a way to do it via the old pre-VS2017 way (C# ->C++/CLI->native C++)? I have been trying to follow this: How to call a C# library from Native C++ (using C++\CLI and IJW)

My C# Class (TestCpp.cs):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestCpp
{
    public class Calculate
    {
        public static int GetResult(int arg1, int arg2)
        {
            return arg1 + arg2;
        }

        public static float GetResult(float arg1, float arg2)
        {
            return arg1 + arg2;
        }

    }
}

Then I create the C++ project, set the compiler key to /clr and output type to .dll ManagedDLL.cpp

#pragma once
#include <string>
using namespace System;


namespace ManagedCpp {

    public ref class CallCSharp
    {
    public:int GetResult(int arg1, int arg2)
    {
        return TestCpp::Calculate::GetResult(arg1, arg2);

    }

    public:float GetResult(float arg1, float arg2)
    {

        return TestCpp::Calculate::GetResult(arg1, arg2);

    }


    };

}

__declspec(dllexport) int GetResult(int arg1, int arg2)
{
    ManagedCpp::CallCSharp callCsharp;
    return callCsharp.GetResult(arg1, arg2);
}


__declspec(dllexport) float GetResult(float arg1, float arg2)
{
    ManagedCpp::CallCSharp callCsharp;
    return callCsharp.GetResult(arg1, arg2);
}

ManagedDLL.h:

int GetResult(int arg1, int arg2);
float GetResult(float arg1, float arg2);

Then I have created native C++ project (TestCpp.cpp)

#include <iostream>
#include <Windows.h>
#include <ManagedDLL.h>

int main()
{

    std::cout << "enter arguments \n";

    std::cin >> arg1;
    std::cin >> arg2;

    int result = GetResult(arg1, arg2);
    std::cout << "Result is \n";
    std::cout << result;
}

The issues I am having are :

The VS2022 tells me that the ManagedCpp project "builds successfully" despite multiple errors "the managed nullptr type cannot be used here", but there is no .dll, which makes me think it isn't actully building

When I try to build TestCpp it throws LNK2019 unresolved external symbol "int __cdecl GetResult(int,int)" (?GetResult@@YAHHH@Z) referenced in function main UnmanagedCpp C:\Users\z003e9wr\source\repos\TestCpp\UnmanagedCpp\TestCpp.obj 1 which I am guessing is due to the fact that it cannot actually find the .dll

So what am I doing wrong?

  • It is still possible to create C++/clr projects in visual studio 2022. I assume your C# code compiles to a dll. You executable also needs to be a /clr executable "Console App". Then use ManagedCpp::CallCSharp^ call = gcnew ManagedCpp::CallCSharp() and call methods on the (or maybe make the methods static). I never used the free functions in managed C++/clr. These are just hints from the top of my head, but I would not go the COM route (I got tons of experience with COM but I don't know where the information comes from that COM would be recommended) – Pepijn Kramer Aug 07 '23 at 18:04
  • The issue is that the final caller of the C# methods cannot be compiled with /clr. I am trying to use C# functions in simulink engine (s-function) and AFAIK s-function interface is not compatible with /clr, hence C#->Cli->native C++ route – Pavel Beliaev Aug 07 '23 at 18:15
  • Ok I understand a bit better what you are tyring to do. The dllexport functions should have __stdcall signature too. This SO question looks similar : https://stackoverflow.com/questions/20501622/load-managed-c-dll-from-unmanaged-c-dll – Pepijn Kramer Aug 07 '23 at 18:31

0 Answers0