Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > C/C++ > BSTR详解五 - BSTR与其它字符串类型转换
【标  题】:BSTR详解五 - BSTR与其它字符串类型转换
【关键字】:BSTR,BSTR
【来  源】:http://blog.csdn.net/pkrobbie/archive/2007/01/18/1486376.aspx

BSTR详解五 - BSTR与其它字符串类型转换

Your Ad Here
1         类型转换
常用字符串件的类型转换。
 
From
To
Sample
字符串常量
BSTR
Right:
BSTR bs = ::SysAllocString(_T("Test string"));
::SysFreeString();
Wrong:
BSTR bs = _T("Test string"); //ERROR
LPWSTR /
LPCWSTR /
WCHAR* /
wchar_t
BSTR
Right:
LPCTSTR sz1 = _T("Test String");
BSTR bs = ::SysAllocString(sz1);
::SysFreeString();
 
Wrong:
LPTSTR sz1 = _T("Test String");
BSTR bs = sz1; //ERROR
BSTR
LPCWSTR /
const WCHAR * /
const wchar_t *
Right:
BSTR bs = ...; //
...
LPCTSTR sz = static_cast<LPCTSTR>bs;
...
::SysFreeString(bs);
//Never use sz after this line
 
Wrong:
BSTR bs = ...; //
...
 
LPCTSTR sz = bs;
...
::SysFreeString(bs);
//Never use sz after this line
_tcslen(sz); //ERROR
 
BSTR
LPWSTR /
WCHAR* /
wchar_t*
Right:
BSTR bs = ...; //
//...
UINT len = ::SysStringLen(bs);
 
// Do not modify the BSTR content by
// C/C++ string functions
LPTSTR sz = new TCHAR[len+1];
_tcsncpy(sz, bs, len);
::SysFreeString(bs);
 
delete []sz;
Wrong:
BSTR bs = ...; //
//...
 
// Do not modify the BSTR content by
// C/C++ string functions
LPTSTR sz = bs; //Error
 
CString
BSTR
Right:
 
CString str1 = ...;
 
BSTR bs = str1.AllocSysString();
SomeMethod(bs);
// void SomeMethod([in]BSTR)
::SysFreeString(bs);
 
CComBSTR bs1(static_cast<LPCTSTR>(str1));
SomeMethod(static_cast<BSTR> (bs1) );
 
// void SomeMethod([in] BSTR )
_bstr_t bs2( static_cast<LPCTSTR>(str1));
SomeMethod(static_cast<BSTR> (bs2) );
 
Wrong:
CString str1 = ...;
 
SomeMethod(str1.AllocSysString());
 
// No one will releasee the return BSTR of
// str1.AllocSysString()
 
BSTR
CString
Right:
 
BSTR bs = SysAllocString(_T(“Test”));
CString str1(bs);
CString str2;
Str2 = bs;
SysFreeString(bs); // Never forget this line
char* / LPSTR / LPCSTR
BSTR
Right:
Solution 1
char str[MAX_STR_LEN] = "ANSI string";
WCHAR wstr[MAX_WSTR_LEN];
// Convert ANSI to Unicode
 
MultiByteToWideChar( CP_ACP, 0, str,
        strlen(str)+1, wstr,  
     sizeof(wstr)/sizeof(wstr[0]) );
 
BSTR bs1 = ::SysAllocString(wstr);
 
CString cs = str;
BSTR bs2 = cs.AllocSysString()
 
Solution 2
char str[MAX_STR_LEN] = "ANSI string";
_bstr_t bs1(str);
CComBSTR bs2(str);
 
Wrong:
char *str = "ANSI string";
BSTR bstr1 = SysAllocString(
            (const OLECHAR*) str);
BSTR
char* / LPSTR / LPCSTR
Right:
Solution 1
char str[MAX_STR_LEN];
BSTR bs = ::SysAllocString(L"Test");
// Convert ANSI to Unicode
WideCharToMultiByte( CP_ACP, 0,
   (LPCWSTR)bs, -1,
   str, MAX_STR_LEN, NULL, NULL );
::SysFreeString(bs);
 
Solution 2
BSTR bs = ::SysAllocString(L"Test");
_bstr_t bs1(bs, false);
const char* str = static_cast <const char*> bs1;
 
Wrong:
BSTR bstr1 = SysAllocString(L”ANSI string");
char *str = (char*) bstr1;    
 
IMPORTANT: 上面所有的例子都是按照UNICODE应用程序设计的。并且不考虑BSTR中包含多个字串的情况,也就是BSTR只在结束的位置有一个0结束符。对于MBCS/ANSI程序,可以参考上面的例子。主要区别是对于现在的COM版本OLECHARwchar_t,但是TCHAR 对于UNICODE程序才是wchar_t
 
NULL对象指针:【上一篇】
BSTR详解四 - BSTR包容类:【下一篇】
【相关文章】
  • abstract class表示的是
  • TabStrip和MultiPage的安装和使用
  • dhTabStrip 1.0发布
  • tabstrip和multipage使用心得
  • 设计模式杂谈:创建型模式之抽象工厂模式(Abstract Factory)
  • ZOJ Problem 1374 Substrings
  • 字符串处理函数substr()使用方法
  • 关于abstract class和interface的理解
  • c#中的interface abstract与virtual
  • abstract class和interface
  • 【随机文章】
  • Flash制作函数曲线课件
  • Bresenham高效画线算法
  • 《 中国黑客致中国黑客和红客的公开信 》
  • ABAP学习笔记之一
  • 拨号适配器诱发IP地址冲突故障解析
  • 关于emacs21设置问题
  • 爆破经验谈 算法分析
  • 发表文章的时候怎么总是出现text错误?
  • llinux编程中的makefile
  • IIS中死循环的解决方法 dllhost占用大量内存,cpu占用100%
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.