步骤:
- 使用#using引用C#的DLL
- using namespace C#命名空间名称
- 使用C++/clr语法,采用正确的访问托管对象,即:使用’^’,而不是星号’*’
实例:
#include <iostream>
#include <msclr/marshal.h>
#using "../Debug/TestCPlus.dll"
using namespace TestCPlus;
using System::IntPtr;
using namespace System::Runtime::InteropServices;
int main()
{
TestCPlus::TestImp ^testPtr = gcnew TestCPlus::TestImp();
System::String ^str = testPtr->Add("测试测试");
const char* chars =
(const char*)(Marshal::StringToHGlobalAnsi(str)).ToPointer();
Marshal::FreeHGlobal(IntPtr((void*)chars));
printf("Add resut is %s\n", chars); // Add resut is 测试测试
getchar();
return 0;
}