原文: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. 【算法笔记】B1045 快速排序

    1045 快速排序 (25 分)   著名的快速排序算法里有一个经典的划分过程:我们通常采用某种方法取一个元素作为主元,通过交换,把比主元小的元素放到它的左边,比主元大的元素放到它的右边. 给定划分后 ...

  2. codeforces1137B kmp(fail的妙用)

    题目传送门 题意:给出$s$和$t$两个串,让你构造出一个答案串,使得答案串中的01数量和s一样,并且使$t$在答案串中作为子串出现次数最多. 思路: 要想出现的次数尽可能多,那么就要重复的利用,哪一 ...

  3. Codeforces - tag::graphs 大合集 [占坑]

    520B 给定初始n和目标m,存在两种操作\(-1\)和\(×2\),要求最少操作次数 无脑解法是BFS,不能解决稍大的规模 当n>m时,输出n-m 否则逆向处理,转换为m到n的最小操作次数,存 ...

  4. Python——制作模块

    步骤一:创建包 步骤二:编辑示例模块代码 __init__调用: 步骤三:创建setup.py from distutils.core import setup setup(name="pa ...

  5. SystemVerilog中枚举类型注意事项

    enum logic {a = 'bx, d = 1'bz}; 在SystemVerilog枚举类型中当使用logic进行声明时,注意logic为四态,所以当使用时如果声明时需要x.z态需要显式声明. ...

  6. linux下执行Python项目,crontab不能定时执行任务

    问题描述: 在项目下的文件执行脚本,可以手动执行 在其他文件目录下,手动执行脚本报错误,提示不存在该模块. 解决方法: 文件头加上: import sysimport ossys.path += [ ...

  7. 入门系列之在Ubuntu上使用Netdata设置实时性能监控

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由小翼 发表于云+社区专栏 介绍 Netdata通过可扩展的Web仪表板提供准确的性能监控,可以显示Linux系统上的流程和服务.它监控 ...

  8. Parsing Failure in config.xml: java.lang.IllegalArgumentException: In production mode, it's not allowed to set a clear text value to the property

    Step1). in your "setDomainEnv.sh" script set the "PRODUCTION_MODE=false" or use ...

  9. 用jQuery来绑定事件的3种方法和区别

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. javascript typeof 和 instanceof 的区别和联系

      这篇文章是我看完<JavaScript高级程序设计(第2版)>书籍的随笔文章,目的只有一个,以备自己和网友们方便参考和记忆! typeof是什么?       typeof 是一个操作 ...