博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
BSTR与char*、cstring、CComBSTR的转换
阅读量:7157 次
发布时间:2019-06-29

本文共 2041 字,大约阅读时间需要 6 分钟。

1 // BSTR_Convert.cpp : 定义控制台应用程序的入口点。 2 // 3  4 #include "stdafx.h" 5 #include 
// _com_util::ConvertBSTRToString 6 #include
//CComBSTR 7 #include
8 9 #pragma comment(lib, "comsuppw.lib")10 11 using namespace _com_util;12 13 int _tmain(int argc, _TCHAR* argv[])14 {15 /****** BSTR->char* *****/16 //方法一使用 ConvertBSTRToString17 //BSTR bstrText = ::SysAllocString(L"Test");18 //char* lpszText = _com_util::ConvertBSTRToString(bstrText);19 //SysFreeString(bstrText);//用完释放20 //delete[] lpszText; 21 //方法二 使用_bstr_t的赋值运算符重载22 //_bstr_t b = bstrText;23 //char* lpstrText1 = b;24 25 /****** char*->BSTR *****/26 //方式一 使用SysAllocString等API函数 27 //BSTR bstrText = ::SysAllocString(L"Test");28 //BSTR bstrText1 = ::SysAllocStringLen(L"Test1",3); 29 //BSTR bstrText2 = ::SysAllocStringByteLen("Test2",4);//乱码30 31 //方式二 使用COleVariant或_variant 编译出错 属于MFC?无法再WIN32下使用?32 //COleVariant strVar("this is a test");33 //_variant_t strVar1("this is a test");34 //BSTR bstrText = strVar.bstrVal;35 //BSTR bstrText1 = strVar1.bstrVal;36 37 //方式三 方法三,使用_bstr_t,这是一种最简单的方法。38 //BSTR bstrText = _bstr_t("This is a test");39 40 //方法四,使用CComBSTR。例如: 41 //BSTR bstrText = CComBSTR("This is a test");42 //CComBSTR bstr1("This is a test"); 43 //BSTR bstrText1 = bstr1.m_str; 44 45 //方法五,使用ConvertStringToBSTR。46 //char* lpszText = "Test"; 47 //BSTR bstrText2 = _com_util::ConvertStringToBSTR(lpszText);48 49 /******* CString->BSTR *******/50 //通 常是通过使用CStringT::AllocSysString来实现51 CString cstr("this is a test");52 BSTR bstrText = cstr.AllocSysString();53 SysFreeString(bstrText);54 55 /******* BSTR -> CString *******/56 //BSTR bstrText = ::SysAllocString(L"Test");57 //CString cstr;58 //cstr.Empty();59 //cstr = bstrText; 60 // 或 CStringA str(bstrText);61 62 system("pause");63 return 0;64 }
View Code

 

转载地址:http://phhgl.baihongyu.com/

你可能感兴趣的文章
MySQL执行SHOW STATUS查询服务器状态状态之 Handler_read_* 详解
查看>>
思科路由与交换系列--------NA系列
查看>>
Linux基于LDAP进行用户认证
查看>>
ECMASCript6 之 Parmameters and Arguments
查看>>
用php调用graphviz轻松画拓扑图
查看>>
【Linux实验】安装Red Hat Linux操作系统的详细步骤
查看>>
listView 点击后变色 抬起后保持颜色
查看>>
joomla第一单元第十节K2内容模板和K2模块样式
查看>>
linux 常用命令
查看>>
Java进程CPU使用率高排查
查看>>
wamp2.4+composer+rabbitmq环境部署-176
查看>>
vbs读取文件内的信息将对应数据拷贝到其他路径
查看>>
Linux运维常见面试题之精华收录
查看>>
8.3 有序表查找
查看>>
Python类型——数字类型
查看>>
Oracle sequence 简单的使用
查看>>
如何做到独特的营销价值
查看>>
APMserv5.2.6 apache启动失败
查看>>
UICollectionView简单使用
查看>>
Eclipse for Mac 常用快捷键整理
查看>>