What is the best way to convert between char* and System::String in C++/CLI

前端 未结 5 607
情深已故
情深已故 2020-11-29 01:07

What is the approved way to convert from char* to System::string and back in C++/CLI? I found a few references to marshal_to<> templated functions on Google, but it appea

5条回答
  •  执笔经年
    2020-11-29 01:48

    I created a few helper methods. I needed to do this to move from an old Qt library to CLI String. If anyone can add onto this and tell me if it seems like I have a memory leak and what I can do to fix it, I would be most appreciative.

    void MarshalString (  String ^ s, wstring& os ) {
        using namespace Runtime::InteropServices;
        const wchar_t* char = (const wchar_t*)(Marshal::StringToHGlobalUni(s)).ToPointer();
        os = char;
    }
    QString SystemStringToQt( System::String^ str)
    {
        wstring t;
        MarshalString(str, t);
        QString r = QString::fromUcs2((const ushort*)t.c_str());
        return r;
    }
    

提交回复
热议问题