看流星社区

 找回密码
 注册账号
查看: 1762|回复: 0

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

[复制链接]

该用户从未签到

发表于 2013-5-16 14:47:21 | 显示全部楼层 |阅读模式


1类型转换

常用字符串件的类型转换。

FromToSample
字符串常量BSTRRight:
BSTR bs = ::SysAllocString(_T("Test string"));

::SysFreeString();
Wrong:
BSTR bs = _T("Test string"); //ERROR
LPWSTR /
LPCWSTR /
WCHAR* /
wchar_t
BSTRRight:
LPCTSTR sz1 = _T("Test String");
BSTR bs = ::SysAllocString(sz1);

::SysFreeString();

Wrong:
LPTSTR sz1 = _T("Test String");
BSTR bs = sz1; //ERROR
BSTRLPCWSTR /
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

BSTRLPWSTR /
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

CStringBSTRRight:

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()

BSTRCStringRight:

BSTR bs = SysAllocString(_T(“Test”));
CString str1(bs);
CString str2;
Str2 = bs;
SysFreeString(bs); // Never forget this line
char* / LPSTR / LPCSTRBSTRRight:
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);
BSTRchar* / LPSTR / LPCSTRRight:
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




点击按钮快速添加回复内容: 支持 高兴 激动 给力 加油 苦寻 生气 回帖 路过 感恩
您需要登录后才可以回帖 登录 | 注册账号

本版积分规则

小黑屋|手机版|Archiver|看流星社区 |网站地图

GMT+8, 2024-4-29 10:33

Powered by Kanliuxing X3.4

© 2010-2019 kanliuxing.com

快速回复 返回顶部 返回列表