I am doing a POC where I need to invoke a c# method from a c++ application. So I did the following:
- Create a c# library.
- Create a c++/cli library wrapping the dll from step 1.
- Creating a c++ library that would consume cli wrapper.
- Creating an unmanaged console application that would use c++ dll output from step 3.
Guys, I have done a lot of searches in last days but did not get any useful help.
- Created the c# project with output: DotNetLogger.dll
- Created the c++/cli project with output: CliLogger.dll Also referenced DotNetLogger.dll Also exported a method.
- Create C++ project with output CppLogger.dll and so on.
Also, C# project setting: v4.6.2 C++/CLI project settings: /CLR enabled with v4.6.2 C++ project setting: no /clr
namespace DotNetLogger
{
public class ManagedLogger
{
public void Ping()
{
//Do something
}
}
}
using namespace System;
using namespace DotNetLogger;
namespace CliLogger {
public ref class LoggerBridge
{
public:
ManagedLogger^ obj;
LoggerBridge() {
obj = gcnew ManagedLogger();
}
void Result() {
return obj->Ping();
}
};
}
__declspec(dllexport) void AreYouThere() {
CliLogger::LoggerBridge obj;
return obj.Result();
}
#pragma once
#include "D:\Logger_POC_NoWorks\InteropLogger\CliLogger\CliLogger.h"
__declspec(dllimport) void AreYouThere();
class UnmanagedLogger {
public:
void InvokeResult() {
return AreYouThere();
}
};
#include "pch.h"
#include <iostream>
#include "D:\Logger_POC_NoWorks\InteropLogger\CppLogger\CppLogger.h"
int main()
{
UnmanagedLogger uLogger;
uLogger.InvokeResult();
}
Expected Result: The console application shall build successfully in VS 2017.
But I am getting compiler errors;
Warning C4273 'AreYouThere': inconsistent dll linkage ServiceCode d:\logger_poc_noworks\interoplogger\cpplogger\cpplogger.h 5
Error C2871 'DotNetLogger': a namespace with this name does not exist ServiceCode d:\logger_poc_noworks\interoplogger\clilogger\clilogger.h 4
Error C2039 'LoggerBridge': is not a member of 'CliLogger' ServiceCode d:\logger_poc_noworks\interoplogger\clilogger\clilogger.h 21
And many cascading errors.