// BSTR_Convert.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <comutil.h> // _com_util::ConvertBSTRToString
#include <atlbase.h> //CComBSTR
#include <atlstr.h> #pragma comment(lib, "comsuppw.lib") using namespace _com_util; int _tmain(int argc, _TCHAR* argv[])
{
/****** BSTR->char* *****/
//方法一使用 ConvertBSTRToString
//BSTR bstrText = ::SysAllocString(L"Test");
//char* lpszText = _com_util::ConvertBSTRToString(bstrText);
//SysFreeString(bstrText);//用完释放
//delete[] lpszText;
//方法二 使用_bstr_t的赋值运算符重载
//_bstr_t b = bstrText;
//char* lpstrText1 = b; /****** char*->BSTR *****/
//方式一 使用SysAllocString等API函数
//BSTR bstrText = ::SysAllocString(L"Test");
//BSTR bstrText1 = ::SysAllocStringLen(L"Test1",3);
//BSTR bstrText2 = ::SysAllocStringByteLen("Test2",4);//乱码 //方式二 使用COleVariant或_variant 编译出错 属于MFC?无法再WIN32下使用?
//COleVariant strVar("this is a test");
//_variant_t strVar1("this is a test");
//BSTR bstrText = strVar.bstrVal;
//BSTR bstrText1 = strVar1.bstrVal; //方式三 方法三,使用_bstr_t,这是一种最简单的方法。
//BSTR bstrText = _bstr_t("This is a test"); //方法四,使用CComBSTR。例如:
//BSTR bstrText = CComBSTR("This is a test");
//CComBSTR bstr1("This is a test");
//BSTR bstrText1 = bstr1.m_str; //方法五,使用ConvertStringToBSTR。
//char* lpszText = "Test";
//BSTR bstrText2 = _com_util::ConvertStringToBSTR(lpszText); /******* CString->BSTR *******/
//通 常是通过使用CStringT::AllocSysString来实现
CString cstr("this is a test");
BSTR bstrText = cstr.AllocSysString();
SysFreeString(bstrText); /******* BSTR -> CString *******/
//BSTR bstrText = ::SysAllocString(L"Test");
//CString cstr;
//cstr.Empty();
//cstr = bstrText;
// 或 CStringA str(bstrText); system("pause");
return ;
}

BSTR与char*、cstring、CComBSTR的转换的更多相关文章

  1. 转:char*, char[] ,CString, string的转换

    转:char*, char[] ,CString, string的转换 (一) 概述 string和CString均是字符串模板类,string为标准模板类(STL)定义的字符串类,已经纳入C++标准 ...

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

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

  3. MFC中char*,string和CString之间的转换

    MFC中char*,string和CString之间的转换 一.    将CString类转换成char*(LPSTR)类型 方法一,使用强制转换.例如:  CString theString( &q ...

  4. CString string char* char 之间的字符转换(多种方法)

    在写程序的时候,我们经常遇到各种各样的类型转换,比如 char* CString string 之间的互相转换.首先解释下三者的含义. CString 是一种很有用的数据类型.它们很大程度上简化了MF ...

  5. Char* ,CString ,WCHAR*之间的转换

    关于Char* ,CString ,WCHAR*之间的转换问题 GDI+所有类的接口函数如果要传递字符串作为参数的话,似乎都用UNICODE串,即WCHAR*.我开始也被整得晕头转向,因为窗口编程所用 ...

  6. CString与string、char*的区别和转换

    转自:http://blog.csdn.net/luoweifu/article/details/20232379 我们在C++的开发中经常会碰到string.char*以及CString,这三种都表 ...

  7. 【转】CString与string、char*的区别和转换

    我们在C++的开发中经常会碰到string.char*以及CString,这三种都表示字符串类型,有很多相似又不同的地方,常常让人混淆.下面详细介绍这三者的区别.联系和转换: 各自的区别 char*: ...

  8. MFC string char cstring 类型转换

    在Unicode环境下用以下转换: CString z_strCurtTime = _T(""); // 获取当前时间 CTime z_tCurTime = CTime::GetC ...

  9. NSString / NSData / char* 类型之间的转换

    转自网络: NSString / NSData / char* 类型之间的转换 1. NSString转化为UNICODE String: (NSString*)fname = @“Test”; ch ...

  10. char类型的数值转换

    在视频教程中,你已经认识到了数字类型之间.字符串和其他类型之间的转换.而某些时候,我们还需要将char类型转换为int类型,或者把int类型转换为char类型. 这篇文章,将介绍在代码中虽然不太常用, ...

随机推荐

  1. TextBox控件设置ReadOnly=true后台取不到值三种解决方法(转)

    当TextBox设置了ReadOnly=true后要是在前台为控件添加了值,后台是取不到的,值为空,多么郁闷的一个问题经过尝试,发现可以通过如下的方式解决这个问题.感兴趣的朋友可以了解下当TextBo ...

  2. Java web 项目读取src或者tomcat下class文件夹下的xml文件或者properties文件

    //生成一个文件对象: File file = new File(getClass().getClassLoader().getResource("test.xml").getPa ...

  3. 在WMware新建一个虚拟机

  4. sql server 查找主键、外键、索引、约束

    主键约束 SELECT   tab.name AS [表名],   idx.name AS [主键名称],   col.name AS [主键列名] FROM   sys.indexes idx    ...

  5. Linux while 获取键盘输入退出

    c 语言实现如下: #include <stdio.h> #include <stdlib.h> #include <string.h> #include < ...

  6. 应用层timer_libc_posix timer

    应用层除了通过setitimer/getitimer设置获取timer外,还可通过timer_create()等一系列函数实现应用层timer功能. 应用流程 The timers created b ...

  7. iOS彩票项目--第五天,新特性引导页的封装、返回按钮的自定义、导航控制器的滑动返回以及自定义滑动返回功能

    一.上次实现了在AppDelegate中通过判断app版本决定是否进入新特性页面,今天将AppDelegate中的一坨进行了封装.将self.window的根控制器到底应该为新特性界面,还是主页面,封 ...

  8. RRD.so文件 rrdruby

    ubuntu 12.04绑定rrdruby gem install librrd 用来装rrdruby,这样才能找到RRD.so文件然后在rb文件中加入这两句话: $: << '/path ...

  9. warning LNK4099: PDB 原因及解决方案

    0x00 现象及原因举例: warning LNK4099: PDB 'wxbase30ud.pdb' was not found with 'wxbase30ud.lib(any.obj)'使用VC ...

  10. 数据处理包plyr和dplyr包的整理

    以下内容主要参照 Introducing dplyr 和 dplyr 包自带的简介 (Introduction to dplyr), 复制了原文对应代码, 并夹杂了个人理解和观点 (多附于括号内). ...