How to convert Platform::String to char*?

后端 未结 5 1308
长情又很酷
长情又很酷 2020-12-01 23:53

How do I convert the contents of a Platform::String to be used by functions that expect a char* based string? I\'m assuming WinRT provides helper functions for this but I ju

5条回答
  •  悲哀的现实
    2020-12-02 00:14

    Here is a very simple way to do this in code w/o having to worry about buffer lengths. Only use this solution if you are certain you are dealing with ASCII:

    Platform::String^ fooRT = "aoeu";
    std::wstring fooW(fooRT->Begin());
    std::string fooA(fooW.begin(), fooW.end());
    const char* charStr = fooA.c_str();
    

    Keep in mind that in this example, the char* is on the stack and will go away once it leaves scope

提交回复
热议问题