Problem: How to convert CString into const char * in C++ MFC

后端 未结 5 669
囚心锁ツ
囚心锁ツ 2021-01-21 11:32

How do I convert CString into const char *? I have tried everything found on the internet but I still cant convert them.

Please help.

Thank you.

5条回答
  •  被撕碎了的回忆
    2021-01-21 11:42

    I know it's late, but I couldn't use the marked-as-answer solution. I search all over the internet and nothing worked for me. I muscled through it to get a solution:

    char * convertToCharArr(CString str) {
        int x = 0;
        string s = "";
        while (x < str.GetLength()) {
            char c = str.GetAt(x++);
            s += c;
        }
        char * output = (char *)calloc(str.GetLength() + 1, sizeof(char));
        memcpy(output, s.c_str(), str.GetLength() + 1);
        return output;
    }
    

提交回复
热议问题