It is possible if you use COM. You should make your C# class exposed to COM and then you can use it in C++. Here is an example inspired by a question here:
namespace ManagedDLL
{
[Guid("32529FAE-6137-4c62-9945-DE4198FA9D1B")]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface ICalculator
{
[DispId(1)]
int Add(int Number1, int Number2);
};
}
namespace ManagedDLL
{
[Guid("9F2F180D-94A9-47e6-91CC-6BCFABD1DDEB")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("ManagedDLL.ManagedClass")]
public class ManagedClass : ICalculator
{
public int Add(int Number1, int Number2)
{
return Number1 + Number2;
}
}
}
Then you can use this in C++:
#import "..\bin\ManagedDLL.tlb" raw_interfaces_only
using namespace ManagedDLL;
int _tmain(int argc, _TCHAR* argv[])
{
HRESULT hr = CoInitialize(NULL);
ICalculatorPtr pICalc(__uuidof(ManagedClass));
long lResult = 0;
pICalc->Add(5, 10, &lResult);
wprintf(L"The result is %d\n", lResult);
CoUninitialize();
return 0;
}