且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从C ++ / CLI调用C#函数-将返回C#字符串转换为C字符串

更新时间:2021-12-02 22:36:19

使用C ++ / CLI,您可以随时使用所需的一切。

With C++/CLI you have everything you need at your disposal.

您可以这样操作:

#include <string>
#include <msclr\marshal_cppstd.h>

extern "C" __declspec(dllexport)
void __stdcall Example(char* name, char* greet) {
    // name will be passed to C# Test(...) function
    // and greet will contains the returned value

    // Create new System::String^ from char*
    System::String^ clrString = msclr::interop::marshal_as<System::String^>(name);

    // Call C# function
    System::String^ result = Test(clrString);

    // Create new std::string from System::String^
    std::string cppString = msclr::interop::marshal_as<std::string>(result);

    // Copy C++-string to the destination
    strcpy(greet, cppString.c_str());
}

此解决方案使用 std :: string 。您还可以使用 marshal_context 直接在 System :: String char []之间进行转换。 ,但我更喜欢使用 std :: string ,因为它可以节省一些键入时间,而且出错的地方更少。

This solution uses std::string. You can also use a marshal_context to directly convert between System::String and char[], but I prefer to use std::string since it saves you some typing and there is less to go wrong.

当然,可以将其缩短为:

Of course, one can shorten this down to:

strcpy(greet, marshal_as<string>(Test(marshal_as<String^>(name))).c_str());

甚至更远,因为 System :: String 有一个接受 char * 的构造函数:

Or even further, since System::String has a constructor accepting char*:

strcpy(greet, marshal_as<string>(Test(name)).c_str());

在这里查看有关编组的更多信息: http://msdn.microsoft.com/en-us/library/bb384865.aspx

Have a look here for more info about marshalling: http://msdn.microsoft.com/en-us/library/bb384865.aspx

重要提示:
C#使用动态字符串,而C#代码通常会很高兴地生成很长的字符串,如果 greet 的大小不足以容纳字符串。
一种常见的处理方法是将 Example 的签名更改为以下内容:

Important: C# uses dynamic strings and C# code often happily generates very long strings, resulting in a crash or worse if the memory pointed to by greet is not large enough to contain the string. A common way to deal with this is to change the signature of Example to something like this:

void __stdcall Example(char* name, char* greet, size_t destBufferSize)

然后检查destBufferSize是否足够大以包含结果字符串或使用 strncpy 或类似方法截断值。

And check if the destBufferSize is large enough to contain the resulting string or truncate the value using strncpy or similar methods.