(转)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/

(转)LPTSTR CString 相互转换

时间: 2024-08-24 04:33:27

(转)LPTSTR CString 相互转换的相关文章

char与CString相互转换

Char -> CStringchar ch[] = "Hello";CString str;str.Format("%s",ch);CString ->Charchar ch[6] ;CString str = "Hello";memcpy(ch,str.GetBuffer(str.GetLength()),6);

CString的GetBuffer用法,GetBuffer本质,GetBuffer常见问题解决方法

一.函数原型 CString::GetBuffer LPTSTR GetBuffer( int nMinBufLength ); throw( CMemoryException ); Return Value An LPTSTR pointer to the object’s (null-terminated) character buffer. Parameters nMinBufLength The minimum size of the character buffer in charac

CreateThread函数&&CString::GetBuffer函数

对这个两个常见的windows下的函数学习了一下: //最简单的创建多线程实例 #include <stdio.h> #include <windows.h> //子线程函数 DWORD WINAPI ThreadFun(LPVOID pM) { printf("子线程的线程ID号为:%d\n子线程输出Hello World\n", GetCurrentThreadId()); return 0; } //主函数,所谓主函数其实就是主线程执行的函数. int m

vc中通过ADO操作数据库

准备工作 (1).引入ADO类 #import "c:\program files\common files\system\ado\msado15.dll" \ no_namespace \ rename ("EOF", "adoEOF") (2).初始化COM 在MFC中可以用AfxOleInit();非MFC环境中用: CoInitialize(NULL); CoUnInitialize(); (3)#import 包含后就可以用3个智能指针

C++ 中 int,char*,string,CString之间相互转换-整理

#include <string> //使用C++标准库的string类时 using namespace std; //同上 #include <sstream> #include <iostream> #include <stdlib.h> //要将string类和int类型直接转换最好有这些包含, //因为自己写一个转换函数比较方便,函数定义参考如下 string getstring ( const int n ) { std::stringstrea

Unicode字符集下CString与char *相互转换

经常遇到CString转换char*时只返回第一个字符.原因是因为在Unicode字符集下CString会以Unicode的形式来保存数据,强制类型转换只会返回第一个字符.所以直接转换在基于MBCS的工程可以,但在基于Unicode字符集的工程中直接转换是不可行的.下面就具体看一下,在Unicode字符集下如下进行CString与char*的互相转换. 在Visual C++.NET2005中,默认的字符集形式是Unicode,但在VC6.0等工程中,默认的字符集形式是多字节字符集(MBCS:M

安全使用CString [转]

1. 安全使用CString 今天我花了差不多一下午的功夫,解决了一个很隐蔽的bug,包括修改和排除相关的可能存在隐患代码. 就是一个关于CString的使用问题,重点体现在Format上. 目前我们的代码里,对于Format的应用可以分为下面的几种方式: ① 格式字符串(format)和可变参数(args)都为非目标字符串对象(str) CString str; str.Format( format, args ); ② 将目标字符串对象(str)初始化为格式字符串(format),并作为格式

CBitmap、HBITMAP、BITMAP相互转换

一:理解 BITMAP是C++中定义的位图结构体 HBITMAP是Windows中使用的位图句柄 CBitmap是MFC封装的位图类 二:相互转换 1.HBITMAP->CBitmap HBITMAP hBitmap=(HBITMAP)::LoadImage(NULL, str, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE); CBitmap bitmap; bitmap.Attach(hBitmap); 实验源码,在(OnPaint函数中添加) CString st

CString常用操作

①.CString 类对象的初始化: CString str; CString str1(_T("abc")); CString str2 = _T("defg"); TCHAR szBuf[] = _T("kkk"); CString str3(szBuf); CString str4 = szBuf; TCHAR *p = _T("1k2"); //TCHAR * 转换为 CString CString str5(p);