VC中BSTR、Char*、CString和CComBSTR类型的转换
原文:http://blog.csdn.net/wanghaihao_1/article/details/37498689
1、char*转换成CString
若将char*转换成CString,除了直接赋值外,还可使用CString::format进行。例如:
char* p = "This is a test"; 或
CString theString = p;
theString.format("%s", p);
theString = p;
2、CString转换成char*
若将CString类转换成char*(LPSTR)类型,常常使用下列三种方法:
方法一,使用强制转换。例如:
CString theString( "This is a test" );
LPTSTR lpsz =(LPTSTR)(LPCTSTR)theString; 方法二,使用strcpy。例如:
CString theString( "This is a test" );
LPTSTR lpsz = new TCHAR[theString.GetLength()+1];
_tcscpy(lpsz, theString); 需要说明的是,strcpy(或可移值Unicode/MBCS的_tcscpy)的第二个参数是 const wchar_t* (Unicode)或const char* (ANSI),系统编译器将会自动对其进行转换。
方法三,使用CString::GetBuffer。例如:
CString s(_T("This is a test "));
LPTSTR p = s.GetBuffer();
// 在这里添加使用p的代码
if(p != NULL) *p = _T('/0');
s.ReleaseBuffer(); // 使用完后及时释放,以便能使用其它的CString成员函数
3、BSTR转换成char*
方法一,使用ConvertBSTRToString。例如:
#include #pragma comment(lib, "comsupp.lib")
int _tmain(int argc, _TCHAR* argv[])
{
BSTR bstrText = ::SysAllocString(L"Test");
char* lpszText2 = _com_util::ConvertBSTRToString(bstrText);
SysFreeString(bstrText); // 用完释放
delete[] lpszText2;
return 0;
} 方法二,使用_bstr_t的赋值运算符重载,_bstr_t是对BSTR的封装。例如:
_bstr_t b = bstrText;
char* lpszText2 = b;
4、char*转换成BSTR
方法一,使用SysAllocString等API函数。例如:
BSTR bstrText = ::SysAllocString(L"Test");
BSTR bstrText = ::SysAllocStringLen(L"Test",4);
BSTR bstrText = ::SysAllocStringByteLen("Test",4); 方法二,使用COleVariant或_variant_t。例如:
//COleVariant strVar("This is a test");
_variant_t strVar("This is a test");
BSTR bstrText = strVar.bstrVal; 方法三,使用_bstr_t,这是一种最简单的方法。例如:
BSTR bstrText = _bstr_t("This is a test"); 方法四,使用CComBSTR。例如:
BSTR bstrText = CComBSTR("This is a test"); 或
CComBSTR bstr("This is a test");
BSTR bstrText = bstr.m_str; 方法五,使用ConvertStringToBSTR。例如:
char* lpszText = "Test";
BSTR bstrText = _com_util::ConvertStringToBSTR(lpszText);
5、CString转换成BSTR
通常是通过使用CStringT::AllocSysString来实现。例如:
CString str("This is a test");
BSTR bstrText = str.AllocSysString();
SysFreeString(bstrText); // 用完释放
6、BSTR转换成CString
一般可按下列方法进行:
BSTR bstrText = ::SysAllocString(L"Test");
CStringA str;
str.Empty();
str = bstrText; 或
CStringA str(bstrText);
SysAllocString
7. CString转化为CComBSTR
CString strName = "cert";
CComBSTR xxx_strName = strName.AllocSysString();
8. CComBSTR转化为BSTR
CComBSTR xxx_strName ;
BSTR bstr = xxx_strName ;
CComBSTR是对BSTR的封装,起内部实现了对BSTR的转化
operator BSTR() const throw()
{
return m_str;
}
所以之后不需要SysFreeString
9. BSTR转化为CComBSTR
BSTR bstrText = ::SysAllocString(L"Test");
CComBSTR cbstr = bstrText;
或者
cbstr = bstrText;
SysFreeString(bstrText); // 用完释放
VC中BSTR、Char*、CString和CComBSTR类型的转换的更多相关文章
- VC中BSTR、Char和CString类型的转换
1.char*转换成CString 若将char*转换成CString,除了直接赋值外,还可使用CString::format进行.例如: char chArray[] = "This is ...
- VC中句柄、指针、ID之间的转换
win32直接操作的是句柄HANDLE,每个句柄就对应windows窗口,而vc对HANDLE进行类封装,间接操作的都是HANDLE,现在句柄只是类的一个成员变量. 从句柄到指针 CWnd* pWnd ...
- [转] java中int,char,string三种类型的相互转换
原文地址:http://blog.csdn.net/lisa0220/article/details/6649707 如何将字串 String 转换成整数 int? int i = Integer.v ...
- java中int,char,string三种类型的相互转换
如何将字串 String 转换成整数 int? int i = Integer.valueOf(my_str).intValue(); int i=Integer.parseInt(str); 如何将 ...
- Java之戳中痛点 - (6)避免类型自动转换,例如两个整数相除得浮点数遇坑
先来看一个例子: package com.test; public class calculate { /** * 光速30万公里/秒 */ public static final int LIGHT ...
- 【转载】C/C++中的char,wchar,TCHAR
点击这里查看原文章 总体简介:由于字符编码的不同,在C++中有三种对于字符类型:char, wchar_t , TCHAR.其实TCHAR不能算作一种类型,他紧紧是一个宏.我们都知道,宏在预编译的时候 ...
- VC++中的CString、char、int类型转换
1.如何将CString类型的变量赋给char*类型的变量 方法一:GetBuffer函数 使用CString::GetBuffer函数. char *p; CString str=&quo ...
- VC中不同类型DLL及区别
1. DLL的概念可以向程序提供一些函数.变量或类. 静态链接库与动态链接库的区别:(1)静态链接库与动态链接库都是共享代码的方式.静态链接库把最后的指令都包含在最终生成的EXE文件中了:动态链接库不 ...
- JAVA中的char类型
1.JAVA中,char占2字节,16位.可在存放汉字 2.char赋值 char a='a'; //任意单个字符,加单引号. char a='中';//任意单个中文字,加单引号. char a=1 ...
随机推荐
- ORACLE的WITH语句的一个疑惑
使用WITH语句,更新表数据,不行: WITH VN AS ( SELECT T.ID, T.NODE_ID, N.NODE_TYPE, N.NODE_NAME, T.NODE_LEVEL, T.RN ...
- Access computer files on iPad without using iTunes.
going to my CSDN blog ---> https://blog.csdn.net/qq_37746801/article/details/89708103
- wiz笔记
分享本地的wiz笔记到网上 , 成为博客 博客园 http://www.cnblogs.com/您的博客名称/services/metablogapi.aspx // region 图片里面的字段:从 ...
- jmeter安装部署、maven路径配置
jmeter下载地址: https://jmeter.apache.org/download_jmeter.cgi 解压文件 配置jmeter环境变量 (1)设置jmeter解压目录的JMETER_H ...
- Comparing deep learning frameworks: Tensorflow, CNTK, MXNet, & Caffe
https://imaginghub.com/blog/10-a-comparison-of-four-deep-learning-frameworks-tensorflow-cntk-mxnet-a ...
- React 同构开发(一)
为什么要做同构 要回答这个问题,首先要问什么是同构.所谓同构,顾名思义就是同一套代码,既可以运行在客户端(浏览器),又可以运行在服务器端(node). 我们知道,在前端的开发过程中,我们一般都会有一个 ...
- 【javascript】Javascript闭包
在描述闭包的实现与用途前,需要了解以下一些知识点. 执行上下文(执行上下文环境) console.log(a); //Uncaught ReferenceError: a is not defined ...
- jmeter(2)——元件简介、作用域及执行顺序
上一篇主要介绍了一下 jmeter还有安装配置,这一篇来说一下它的目录组成和元件 1.目录组成 1>bin目录是可执行文件,双击jmeter.bat启动工具,也可以编辑这个批处理文件,设置jvm ...
- .netCore2.0 WebApi 传递form表单
随着it的技术发展,目前越来越多的项目采用前后端分离的开发模式,通过webapi提供接口数据来进行交互 最近项目用的是.netCore WebApi,在最近的项目使用中发现一些问题,进行记录.个人简介 ...
- js惊奇效果分享,和排序算法
分享地址:http://www.cnblogs.com/lhb25/p/8-amazing-codepen-demos.html 排序算法分享地址:http://www.w3cplus.com/js/ ...