将C++字符串传递给C#函数

本文关键字:函数 C++ 字符串 | 更新日期: 2023-09-27 18:20:25

我正在用c#包装Microsoft的assert类,并希望在c++中使用该代码。我遇到的问题是处理信息字符串,我不太确定如何将c++字符串传递给c#方法。两者都有管理。

//c# Assert wrapper
static public bool AreEqual<T>(T expected, T actual, string info){
    //...
    WriteLine(info);
    //...
}

从c++调用

//c++
void someCppFunc(){
    long expected = 2;
    long actual = 2;
    Assert::AreEqual<long>(expected, actual, L"Some info message");
}

但我得到的错误表明它没有匹配功能。我该怎么做?

将C++字符串传递给C#函数

只需传递一个托管字符串,而不是本地C++字符串:

//c++/clr
void someCppFunc(){
    long expected = 2;
    long actual = 2;
    Assert::AreEqual<long>(expected, actual, gcnew System::String(L"Some info message"));
}