让我们分享一切
2012-05-31 11:30:16| 分类: vc++ | 标签:vc++开发开始 |举报 |字号 订阅
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;
|
CComBSTR Method
|
Description
|
CComBSTR
|
多个版本的构造函数用来创建新的
BSTR
。可以使用的参数包括
LPCOLESTR, LPCSTR, CComBSTR
。
|
~CComBSTR, Empty
|
释放内部封装的
BSTR.
|
Attach, Detach, Copy
|
Attach
把一个已经存在
BSTR
加入类中。
Detach
把劣种的
BSTR
剥离,以便在超出作用域的时候,析构函数不会释放
BSTR
。
Detach
用于把
CComBSTR
赋给
[out]
参数。
Copy
用于产生一个
BSTR
的副本。一般用于用于把
CComBSTR
内容赋给
[out]
参数。
|
operator BSTR, operator&
|
允许直接操作内部的
BSTR
。
operator BSTR
用于把
CComBSTR
传给
BSTR
输入
[in]
参数。
operator&
用于把
CComBSTR
传给
BSTR*
类型输出
[out]
参数。
|
operator=, operator+=, operator<, operator==, operator>
|
重载运算符,用于赋值、字符串连接、简单比较。
|
Append, AppendBSTR
|
字符串连接
|
Length
|
计算字符串长度
|
LoadString
|
利用字符串资源初始化
BSTR
。
|
ToLower, ToUpper
|
字符串大小写转换。
|
WriteToStream,ReadFromStream
|
从
IStream
中读
/
写
BSTR
。
|
Features Not Included in CComBSTR
|
Explanation
|
LPCSTR extraction
|
CComBSTR
可以把一个单字节字符串转换成
BSTR
,但是没有提供反向转换的功能。
_bstr_t
提供了
LPCTSTR operator
。
|
String manipulation (including Replace, Insert, Delete, Remove, Find, Mid, Left, Right, and so on)
|
CComBSTR
没有提供这些方法。如果需要,可以使用
STL
中的
wstring
。
|
Language-sensitive collation
|
CComBSTR
提供的字符串比较
(<, >, ==)
按照是
byte-by-byte
方式进行的。没有提供语言相关的比较
(language-specific collation)
。如果需要可以使用
wstring.
|
Construction
|
Version
|
|
_bstr_t
|
|
Constructs a _bstr_t object.
|
Operations
|
|
|
Assign
|
|
Copies a BSTR into the BSTR wrapped by a _bstr_t.
|
Attach
|
VC 7
|
Links a _bstr_t wrapper to a BSTR.
|
copy
|
|
Constructs a copy of the encapsulated BSTR.
|
Detach
|
VC 7
|
Returns the BSTR wrapped by a _bstr_t and detaches the BSTR from the _bstr_t.
|
GetAddress
|
VC 7
|
Points to the BSTR wrapped by a _bstr_t.
|
GetBSTR
|
VC 7
|
Points to the beginning of the BSTR wrapped by the _bstr_t.
|
length
|
|
Returns the number of characters in the _bstr_t.
|
Operators
|
|
|
operator =
|
|
Assigns a new value to an existing _bstr_t object.
|
operator +=
|
|
Appends characters to the end of the _bstr_t object.
|
operator +
|
|
Concatenates two strings.
|
operator !
|
|
Checks if the encapsulated BSTR is a NULL string.
|
operator ==, !=, <, >, <=, >=
|
|
Compares two _bstr_t objects.
|
operator wchar_t* | char*
|
|
Extract the pointers to the encapsulated Unicode or multibyte BSTR object.
|
|
|
|
String Manipulation Functions
|
Descriptions
|
SysAllocString
|
Creates and initializes a string.
|
SysAllocStringByteLen
|
Creates a zero-terminated string of a specified length.
|
SysAllocStringLen
|
Creates a string of a specified length.
|
SysFreeString
|
Frees a previously created string.
|
SysReAllocString
|
Changes the size and value of a string.
|
SysReAllocStringLen
|
Changes the size of an existing string.
|
SysStringByteLen
|
Returns the length of a string in bytes.
|
SysStringLen
|
Returns the length of a string.
|
优先级
|
类型
|
说明
|
最高
|
stl::string/wstring
|
·
功能最完善,可移植性最好。
|
|
CString
|
·
如果编码规范限制使用STL的时候,推荐CString。
·
VC 6
的版本很不完善。
.Net
有明显改进,需要进一步研究。
|
|
C/C++ basic type
(
TCHAR* / char* / LPTSTR / LPCTSTR / TCHAR[]
)
|
·
在结构体中,优先使用指定最大长度的字符数组。
·
效率最好
|
|
CComBSTR/ _bstr_t
|
·
在必须使用
BSTR
时的优先选择。
·
在
ATL
(
COM component
)工程或者工程中必须使用
ATL
中,优先选择
CComBSTR
。一般
Exe/dll
如果
_bstr_t
能满足要求,优先使用
_bstr_t
。
·
对于
VC6
,使用
_bstr_t
一定要慎重,最好只用作简单临时变量保存调被调用函数的传入参数。因为
_bstrt_t
不能支持一些关键性操作,比如
Detach
。
·
对于
VC++ .Net
推荐使用
_bstr_t
,它是
C++
扩展,不需要额外包含
ATL
的文件。
|
最低
|
BSTR
|
·
COM
接口
|