Convert a 'System::String ^' to 'const char *' in vc++

前端 未结 2 1117
生来不讨喜
生来不讨喜 2021-02-09 13:52

How can I convert a \'System::String ^\' to \'const char *\' in vc++?

My code:

String ^Result1= \"C:/Users/Dev/Desktop/imag.jpg\";

Ip

2条回答
  •  日久生厌
    2021-02-09 14:13

    This is work for me.

    void MarshalString ( String ^ s, string& os ) {
    
       using namespace Runtime::InteropServices;
    
       const char* chars = 
          (const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer();
    
       os = chars;
    
       Marshal::FreeHGlobal(IntPtr((void*)chars));
    }
    

提交回复
热议问题