C++/CLI String Conversions

后端 未结 2 1725
迷失自我
迷失自我 2020-12-18 14:01

I found this really nice piece of code that converts a string to a System:String^ as in:

System::String^ rtn = gcnew String(move.c_str());  // \         


        
相关标签:
2条回答
  • 2020-12-18 14:34

    this could be useful:

    wchar_t *str = "Hi StackOverflow"; //native
    String^ mstr= Marshal::PtrToStringAnsi((IntPtr)str); // native to safe managed
    wchar_t* A=( wchar_t* )Marshal::StringToHGlobalAnsi(mstr).ToPointer(); // return back to native 
    

    don't forget using namespace System::Runtime::InteropServices;

    0 讨论(0)
  • 2020-12-18 14:35

    Use VC++'s marshaling library: Overview of Marshaling in C++

    #include <msclr/marshal_cppstd.h>
    
    // given System::String^ mstr
    std::string nstr = msclr::interop::marshal_as<std::string>(mstr);
    
    0 讨论(0)
提交回复
热议问题