std::string to LPCTSTR

后端 未结 7 1304
甜味超标
甜味超标 2021-02-19 10:30

New version of the typical question of how to convert from std::string to LPCTSTR.

Reading from different SO posts I learnt that I should do th

7条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-19 11:29

    You are compiling for Unicode which means that CreateDirectory is an alias for CreateDirectoryW, the wide character version. However, the text in your program is encoded using ANSI. This means that your program cannot handle internationalization properly.

    The compiler is telling you that there is a mismatch between the text encoding that CreateDirectoryW expects, and the text encoding that you are supplying. It's true that you could call CreateDirectoryA to resolve that mis-match, but that is only going to perpetuate the root problem, the fact that you are using ANSI text in your program.

    So, the best solution is to start encoding all your text as Unicode. Stop using string and start using wstring. Once you change path to wstring then

    CreateDirectory(path.c_str(),NULL);
    

    is correct.

提交回复
热议问题