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

前端 未结 2 1155
生来不讨喜
生来不讨喜 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:00

    It's like this: How to convert from System::String* to Char* in Visual C++

    System::String ^ str = "Hello world\n";
    
    //method 1
    pin_ptr str1 = PtrToStringChars(str);
    wprintf(str1);  
    
    //method 2
    char* str2 = (char*)Marshal::StringToHGlobalAnsi(str).ToPointer();
    printf(str2);
    Marshal::FreeHGlobal((IntPtr)str2);
    
    //method 3
    CString str3(str); 
    wprintf(str3);
    
    //method 4
    #if _MSC_VER > 1499 // Visual C++ 2008 only
    marshal_context ^ context = gcnew marshal_context();
    const char* str4 = context->marshal_as(str);
    puts(str4);
    delete context;
    #endif
    

提交回复
热议问题