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

Passing C++ string to a c# function

本文关键字:函数 C++ 字符串      更新时间:2023-10-16

我正在用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++/clr
void someCppFunc(){
    long expected = 2;
    long actual = 2;
    Assert::AreEqual<long>(expected, actual, gcnew System::String(L"Some info message"));
}