C string full guide (2) - Various string classes - CRT class_BSTR_T
String packaging class
I have explained various types of strings and now discuss packaging classes. For each packaging class, I will explain its object constructor and how to convert to C-Type C string pointers. Application interface call, or construct another different type of string class, most of which use C-type pointers. This article does not involve other operations such as sorting and comparison.
Also stressed, do not use the forced type conversion before fully understand the result of the conversion.
CRT class_BSTR_T
_BSTR_T is the full packaging class of BSTR. In fact, it implies BSTR. It provides a variety of constructors that can handle implicit C-Types strings. But it itself does not provide the process mechanism for BSTR, so it cannot be used as an output parameter [OUT] of the COM method. If you want to use BSTR * type data, it is more convenient to use ATL's CCOMBSTR class.
_BSTR_T data can be passed to functions that require BSTR data, but must meet the following three conditions:
First, _bstr_t has functions that can be converted to WCHAR_T * type data.
Second, according to the BSTR definition, WCHAR_T * and BSTR are the same for compilers.
Third, _bstr_t reserved inside the pointer to the memory data block Wchar_t * To follow the BSTR format.
These conditions are met, even if there is no corresponding BSTR conversion document, _bstr_t can work normally. Examples are as follows:
// Structure _bstr_t bs1 = "char string"; // From LPCSTR_T BS2 = L "Wide Char String"; // From LPCWSTR Structure_BSTR_T BS3 = BS1; // Copy another _BSTR_T_VARIANT_T V = "bob "; _BSTR_T BS4 = V; // From an _variant_t constructor // data extraction LPCSTR PSZ1 = BS1; / / automatically converted to MBCS string LPCSTR PSZ2 = (LPCSTR) BS1; // Cast OK, the same LPCWSTR PWSZ1 = BS1; // Return the internal Unicode string LPCWSTR PWSZ2 = (lpcwstr) BS1; // Cast OK, the same BSTR BSTR = BS1.copy (); // Copy BS1, return BSTR / / ... SYSFREESTRING (BSTR );
Note that _BSTR_T can also be converted to Char * and Wchar_T *. This is a design problem. Although Char * and WCHAR_T * are not a constant pointer, it cannot be used to modify strings because the internal BSTR structure may be broken.
[_VARIANT_T]
_variant_t_variant_t is a full packaging class of Variant. It provides a variety of constructor and data conversion functions. This article only discusses the operation related to the string.
// Structure _variant_t v1 = "char string"; // From LPCSTR Structure _variant_t v2 = L "Wide Char String"; // From LPCWSTR_T BS1 = "BOB"; _ variant_t v3 = BS1; // Copy one _BSTR_T object // Data extraction _BSTR_T BS2 = V1; // Extract BSTR_BSTR_T BS3 = (_BSTR_T) v1; // Cast OK, pay attention to the _variant_t method, so ready to use Catch capture _com_error exception.
Also pay attention to _variant_t can't be converted directly into an MBCS string. To create a transition-only _bstr_t variable, use other class functions that convert Unicode to MBCs, or ATL conversion macros to convert.
Unlike _bstr_t, _variant_t data can be directly transmitted to the COM method as parameters. _variant_t inherits the Variant type, so use the use of Variant's place to use _variant_t is allowed by C language rules.
In the following chapter, I will also introduce string classes such as STL, ATL.