I have a simple C++ program which calls a C# DLL. It sends and receives a string.
#include "stdafx.h"
#include <string>
#include <msclr\marshal_cppstd.h>
char* callCsDLL(char* arg) {
// arg will be passed to C# DLL method
// and result will contain a string reply
// Copy a new System::String^ from char*
System::String^ clrString = msclr::interop::marshal_as<System::String^>(arg);
// Call C# function
System::String^ t = Namespace::Class::run(clrString);
// Create new std::string from System::String^
std::string cppString = msclr::interop::marshal_as<std::string>(t);
// Copy C++ string to the result
char* result = new char[cppString.length() + 1];
strcpy(result, cppString.c_str());
return result;
}
Inside Visual Studio 2017, this is quite easy to do, use C++ CLR, just add the C# DLL to the references in the C++ project, and build. It's almost magic.
How would I link these together outside of Visual Studio, that is, using the command line? Could I avoid MSBUILD altogether? The resulting executable should simply use the DLL.