原文: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类型的转换的更多相关文章

  1. VC中BSTR、Char和CString类型的转换

    1.char*转换成CString 若将char*转换成CString,除了直接赋值外,还可使用CString::format进行.例如: char chArray[] = "This is ...

  2. VC中句柄、指针、ID之间的转换

    win32直接操作的是句柄HANDLE,每个句柄就对应windows窗口,而vc对HANDLE进行类封装,间接操作的都是HANDLE,现在句柄只是类的一个成员变量. 从句柄到指针 CWnd* pWnd ...

  3. [转] java中int,char,string三种类型的相互转换

    原文地址:http://blog.csdn.net/lisa0220/article/details/6649707 如何将字串 String 转换成整数 int? int i = Integer.v ...

  4. java中int,char,string三种类型的相互转换

    如何将字串 String 转换成整数 int? int i = Integer.valueOf(my_str).intValue(); int i=Integer.parseInt(str); 如何将 ...

  5. Java之戳中痛点 - (6)避免类型自动转换,例如两个整数相除得浮点数遇坑

    先来看一个例子: package com.test; public class calculate { /** * 光速30万公里/秒 */ public static final int LIGHT ...

  6. 【转载】C/C++中的char,wchar,TCHAR

    点击这里查看原文章 总体简介:由于字符编码的不同,在C++中有三种对于字符类型:char, wchar_t , TCHAR.其实TCHAR不能算作一种类型,他紧紧是一个宏.我们都知道,宏在预编译的时候 ...

  7. VC++中的CString、char、int类型转换

    1.如何将CString类型的变量赋给char*类型的变量   方法一:GetBuffer函数  使用CString::GetBuffer函数.  char *p;  CString str=&quo ...

  8. VC中不同类型DLL及区别

    1. DLL的概念可以向程序提供一些函数.变量或类. 静态链接库与动态链接库的区别:(1)静态链接库与动态链接库都是共享代码的方式.静态链接库把最后的指令都包含在最终生成的EXE文件中了:动态链接库不 ...

  9. JAVA中的char类型

    1.JAVA中,char占2字节,16位.可在存放汉字 2.char赋值 char a='a';  //任意单个字符,加单引号. char a='中';//任意单个中文字,加单引号. char a=1 ...

随机推荐

  1. 使用范围for语句处理多维数组

    在C++11新标准中新增了范围for语句,所以遍历多维数组可以用如下形式: int num[rowCnt][colCnt]; for(auto &row : num){ for(auto &a ...

  2. 接上篇—用spring注入DBbean,并使用maven管理

    接着上篇的登陆功能,用spring的依赖注入和maven的管理,又实现了一遍.新增了注册功能. 有两个目标:使用spring和使用maven 目录结构 首先是目标1:使用spring的依赖注入,注入b ...

  3. django 迁移工程数据库无法创建的问题

    1.今天我遇到一个问题在此做笔记记下来 2.我晚上一般是在家练习的,白天会拷贝工程到公司用 3.因为我在家里创建过一次数据库了,通过命令创建,但是无论我怎么修改models都无法创建表,最后只能通过新 ...

  4. js从后台取值并绑定到元素上

    用ajax从后台取值不是什么有技术含量的活计,把后台取来的值绑定到Vue对象上也不算难,但每一次向后台拿数据的时候都得写上这么一段代码就十分痛苦了. 于是我写了这么一小段js代码,能够自己根据url去 ...

  5. Qt4.8.5移植

    这两天搞了Qt移植 因为不小心 耽误了挺多时间 但是也比较好的掌握了   现在记录一下 准备工具: tslib-1.16 qt-everywhere-opensource-src-4.8.5.tar ...

  6. es第二篇:Document APIs

    文档CRUD API分为单文档API和多文档API.这些API的索引名参数既可以是一个真正的索引的名称,也可以是某个索引的别名alias. 单文档API有:Index API.Get API.Dele ...

  7. Longest palindrome subsequence

    A palindrome is a nonempty string over some alphabet that reads the same forwardand backward. Exampl ...

  8. Git学习系列之 Git 、CVS、SVN的比较

    Git .CVS.SVN比较 项目源代码的版本管理工具中,比较常用的主要有:CVS.SVN.Git 和 Mercurial  (其中,关于SVN,请参见我的博客:SVN学习系列) 目前Google C ...

  9. 案例20-页面使用redis缓存显示类别菜单

    1 准备工作 1  需要导入所需要的jar包. 2 启动windows版本的redis服务端 3 准备JedisUtils工具类的配置文件redis.properties redis.maxIdle= ...

  10. springboot项目更改代码后实时刷新问题

    在spring boot使用的过程中, 发现我修改了静态文件, 前台刷新后, 没有任何变化, 必须重新启动, 才能看到, 这简直不能让人接受. 那有什么方法来解决这个问题呢. Baidu之后, 得到了 ...