MFC中 將std::string轉換為LPCTSTR的方法


 

 

在MFC中將std::string轉換為LPCTSTR的方法,在網上找了好久,終於被我發現了。

http://blog.sina.com.cn/s/blog_6f7e64f801014sjo.html

需要自己寫一個轉換函數:

std::wstring StoWs(const std::string& s)
{
 int len;
 int slength = (int)s.length() + 1;
 len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0); 
 wchar_t* buf = new wchar_t[len];
 MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
 std::wstring r(buf);
 delete[] buf;
 return r;
}

std::string s;

如果是 UNICODE 字符的話:
std::wstring stemp = StoWs(s);

LPCWSTR result = stemp.c_str();


如果不是:
LPCWSTR result = s.c_str();

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM