(轉)LPTSTR CString 相互轉換

來源:互聯網
上載者:User

標籤:blog   http   tar   ext   com   get   

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/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.