步骤:
- 使用#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;
}
本文介绍了一个具体的示例,展示了如何通过C++/CLI混合模式编程来调用C#的DLL文件。该示例涵盖了必要的引用声明、命名空间使用及托管对象的正确访问方法。
1722

被折叠的 条评论
为什么被折叠?



