首页 > 代码库 > (转)LPTSTR CString 相互转换
(转)LPTSTR CString 相互转换
LPTSTR不怎么常用,今天用到查之,转载文章。
引用
yinengsoft 的 LPTSTR CString 相互转换/* LPTSTR 转换成 CString */
(1)直接赋值
CString strText;
LPTSTR lpszText = _T("LPTSTR >> CString");
strText = lpszText;
::MessageBox( NULL, strText , _T("标题"), MB_ICONASTERISK|MB_TASKMODAL|MB_OK );
(2)CString::Format()格式化
CString strText;
LPTSTR lpszText = _T("LPTSTR >> CString");
strText.Format( _T("%s"), lpszText );
::MessageBox( NULL, strText , _T("标题"), MB_ICONASTERISK|MB_TASKMODAL|MB_OK );
/* CString 转换成 LPTSTR */
(1)强制转换
CString strText( _T("This is a test") );
LPTSTR lpszText =(LPTSTR)(LPCTSTR)strText;
::MessageBox( NULL, lpszText, _T("标题"), MB_ICONASTERISK|MB_TASKMODAL|MB_OK );
(2)使用lstrcpy()
CString strText( "This is a test" );
LPTSTR lpszText = new TCHAR[strText.GetLength()+1];
lstrcpy( lpszText, strText );
::MessageBox( NULL, lpszText, _T("标题"), MB_ICONASTERISK|MB_TASKMODAL|MB_OK );
(3)使用CString::GetBuffer()
CString strText(_T("This is a test "));
LPTSTR lpszText = strText.GetBuffer();
strText.ReleaseBuffer();
::MessageBox( NULL, lpszText, _T("标题"), MB_ICONASTERISK|MB_TASKMODAL|MB_OK );TRACE:http://hcorecore.blog.163.com/blog/static/79664102200931811428906/