CString 转 const char *

网上搜了很多,看得头疼,总结了以下几种方法

环境vs2017 win10

1、方法3的简化版
CString s("123");
string(CW2A(s)).c_str();

2、sprintf()
CString s("123"); char buf[16]; sprintf(buf,"%S",s.GetBuffer()); //注意%S是大写 s.ReleaseBuffer();

3、转string再转const char*
需要#include <atlconv.h>
CString s("123");
USES_CONVERSION; //需要这一步才能使用W2A
const char* buf=string(W2A(s)).c_str();

 

posted @ 2018-12-19 15:45  onlyandonly  阅读(1518)  评论(0编辑  收藏  举报