简体   繁体   English

如何使用 Il2CppInspector 将 C++ 中的 std::string 转换为 System.String?

[英]How do I convert a std::string to System.String in C++ with Il2CppInspector?

I am using Il2CppInspector to generate scaffolding for a Unity game.我正在使用Il2CppInspector为 Unity 游戏生成脚手架。 I am able to convert System.String ( app::String in Il2CppInspector) to std::string using the functions provided below.我可以使用下面提供的函数将System.String (Il2CppInspector 中的app::String )转换为std::string

How would I reverse this process;我将如何扭转这个过程; how do I convert a std::string to System.String ?如何将std::string转换为System.String

helpers.cpp

    // Helper function to convert Il2CppString to std::string
    std::string il2cppi_to_string(Il2CppString* str) {
        std::u16string u16(reinterpret_cast<const char16_t*>(str->chars));
        return std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>{}.to_bytes(u16);
    }
    // Helper function to convert System.String to std::string
    std::string il2cppi_to_string(app::String* str) {
        return il2cppi_to_string(reinterpret_cast<Il2CppString*>(str));
    }

In short, I am looking for a function that takes in a std::string and returns an app::String简而言之,我正在寻找一个接收std::string并返回app::String的 function

    // Helper function to convert std::string to System.String
    app::String string_to_il2cppi(std::string str) {
        // Conversion code here
    }

Export Il2CppInspector with all namespaces, which will give you access to Marshal_PtrToStringAnsi .使用所有命名空间导出 Il2CppInspector,这将使您能够访问Marshal_PtrToStringAnsi

app::String* string_to_il2cppi(std::string str) {
    return app::Marshal_PtrToStringAnsi((void*)&str, NULL);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM