c++ 调用C# 的DLL
1. 修改cpp属性

常规-》公共语言运行时支持-》选择/clr

常规-》调试信息格式-》选择无
代码生成-》启用c++异常-》选择否

代码生成-》基本运行时检查-》选择默认值

命令行-》其它选项-》输入/Zc:twoPhase-

2. c#代码
| 1 | using System; | 
3. c++ 代码
CSharpLibTest.cpp1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26// CSharpLibTest.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
using namespace System;
using namespace System::Reflection;
using namespace System::Runtime::CompilerServices;
using namespace System::Runtime::InteropServices;
using namespace System::Security::Permissions;
using namespace CSharpTest;
int main(int argc, char* argv[])
{
    char str[]{ "xxxxxx" };
    String^ name = System::Runtime::InteropServices::Marshal::PtrToStringAnsi((IntPtr)str);
    CSharpTest::TestApi^ api = gcnew CSharpTest::TestApi();
    String^ out = api->Print(name);
    char* outStr = (char*)Marshal::StringToHGlobalAnsi(out).ToPointer();
    std::cout << outStr << std::endl;
    Marshal::FreeHGlobal(IntPtr(outStr));
    return 0;
}
4 输出






