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;
|
BSTR是一种字符串指针,如果你在VC找寻其定义,你会发现它其实是unsigned short*,然而它不能像普通的字符串指针char*一样可以直接赋值,而必须使用SysAllocString来分配,用SysFreeString来释放。
另外,又有两个BSTR的包容类_bstr_t和CComBSTR,它们是为了编程者使用BSTR更加方便,因为在他们的构造函数中都调用了SysAllocString,析构函数调用了SysFreeString,然而使用这两个类时仍然需要特别注意,否则也会导致不可预知的错误。
举例如下:
1.标准用法:
BSTR str = SysAllocString(L”aaa”); …(可以使用此BSTR变量的范围) SysFreeString(str); str = NULL; |
需要注意的是当SysFreeString被调用后,最好将此BSTR变量赋为NULL,以防止多次释放,导致释放非法内存空间。
2._bstr_t类型有一个函数叫copy,使用时需要当心,因为它其实调用了SysAllocStringByteLen,所以需要调用者去释放返回的BSTR字符串。
用法如下:
_bstr_t m_state = L"cc"; BSTR str = m_state.copy(); …(可以使用此BSTR变量的范围) SysFreeString(str); str = NULL; |
3._bstr_t类型有一个特殊构造函数,其第二个参数是一个bool值,表示是否进行SysAllocString,如果是false,表示直接将此字符指针赋给_bstr_t内部所包容的BSTR,这虽然给使用者带来了更大的灵活性,但却需要使用时更加注意,用法如下:
BSTR str1 = SysAllocString(L”aaa”); _bstr_t str2(str1, false);
|
注意,此时不需要调用SysFreeString,因为_bstr_t的构造函数虽然没有调用SysAllocString,但其析构函数仍然会调用SysFreeString。所以不需要使用者自己去释放了。
4.函数接口传递BSTR或BSTR*的基本规则
· 如果调用一个使用BSTR参数的函数,调用者负责在调用前分配BSTR,在调用后释放。
连接:http://blog.youkuaiyun.com/wangqiulin123456/article/details/8114638