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?