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 ...
随机推荐
- FreeRTOS-06任务运行时间信息统计
根据正点原子FreeRTOS视频整理 单片机:STM32F207VC FreeRTOS源码版本:v10.0.1 * 1. 要使用vTaskGetRunTimeStats()函数,需满足以下条件: * ...
- 超大图片显示,可任意缩放,移动,不用DiskLruCache
1.演示,代码 下载示例apk 下载项目 : https://gitee.com/xi/LImage.git 2.遇到的问题 想省内存,不太可能 只支持拖拽手势,不支持缩放相对简单,解码v ...
- JRebel idea 热部署
下载链接:https://pan.baidu.com/s/1CUvEarKVARJF46LJ2W90Lg 提取码:s46w 下载完以后是个ZIP(jr-ide-intellij-7.1.5_13-17 ...
- J15W-J45W铜质截止阀厂家,J15W-J45W铜质截止阀价格 - 专题栏目 - 无极资讯网
无极资讯网 首页 最新资讯 最新图集 最新标签 搜索 J15W-J45W铜质截止阀 无极资讯网精心为您挑选了(J15W-J45W铜质截止阀)信息,其中包含了(J15W-J45W铜质截止阀)厂家,( ...
- range() 和 np.arange()区别
range() 和 np.arange()区别 range(start,stop,step) 三个参数都必须是整数 np.arange()没有此类约束
- Zookeeper选举算法原理
Zookeeper选举算法原理 Leader选举 Leader选举是保证分布式数据一致性的关键所在.当Zookeeper集群中的一台服务器出现以下两种情况之一时,需要进入Leader选举. (1) 服 ...
- LSTM时间序列预测学习
一.文件准备工作 下载好的例程序 二.开始运行 1.在程序所在目录中(chapter_15)打开终端 输入下面的指令运行 python train_lstm.py 此时出现了报错提示没有安装mat ...
- Oracle 事务操作
在看本文之前,请确保你已经了解了Oracle事务和锁的概念即其作用,不过不了解,请参考数据库事务的一致性和原子性浅析和Oracle TM锁和TX锁 1.提交事务 当执行使用commit语句可以提交事务 ...
- mysql 导入 excel 数据
客户准备了一些数据存放在 excel 中, 让我们导入到 mysql 中.先上来我自己把数据拷贝到了 txt 文件中, 自己解析 txt 文件,用 JDBC 循环插入到数据库中. 后来发现有更简单 ...
- Netbeans8的中文乱码
Netbeans8的中文乱码 前提: 不是编码的问题,统一改成UTF-8,之后文字还是显示方块.使用以下方式解决: 菜单 -> 工具 -> 选项 -> 字体和颜色 -> 语法 ...