_bstr_t and BSTR

Source: Internet
Author: User

Problem:

BSTR a = _bstr_t ("a");

BSTR B = _bstr_t ("B");

CString C;

c = A;

MessageBox (c);

c = b;

MessageBox (c);

Why is the message box displayed in B?

If so:

_bstr_t bstr1 ("a");

BSTR a = BSTR1;

_bstr_t bstr2 ("B");

BSTR B = bstr2;

CString C;

c = A;

MessageBox (c);

c = b;

MessageBox (c);

The message box is displayed!

Answer:

The first thing to be clear, _bstr_t is the encapsulation of a BSTR.

Code 1:

BSTR a = _bstr_t ("a");

Here, _bstr_t ("a") is just a temporary object, and when it initializes (contains an internal BSTR) and assigns a value to BSTR A, it is revoked. And the value it assigns is the address of the BSTR.

BSTR B = _bstr_t ("B");

The second time, still produces a temporary object, it also initializes ... The process is the same as above. Also, because the previous _bstr_t object has been revoked, it produces the same address as the BSTR.

So, in fact, a, B points to the same address. The content of the address was written two times.

Code 2:

_bstr_t bstr1 ("a");

BSTR a = BSTR1;

_bstr_t bstr2 ("B");

BSTR B = bstr2;

The two _bstr_t bstr1,bstr2 here are local objects and exist at the same time. Therefore, the address of the BSTR contained within them is not the same. It's okay to assign values differently.

#if defined (WIN32) &&!defined (Ole2ansi)

typedef WCHAR OLECHAR;

#else

typedef char OLECHAR;

#endif

typedef olechar* BSTR;

The BSTR is actually the first address (double-byte or ANSI string) that points directly to the string.

The four bytes before the BSTR are the length of the string, and the code generated by the compiler automatically reads the length, rather than "s" to identify the end of the string.

The BSTR string has several important points to emphasize:

1. A BSTR string variable is actually a pointer variable. It takes 32bit or 4 bytes, just like any other pointer. Also, it points to a character array in Unicode format. However, we cannot equate strings with BSTR strings. We must use its own exact name-"BSTR".

2. A BSTR string variable that points to a string array that must start with a 4-byte reserved word (the number of bytes in the string array is saved instead of the number of characters), ending with 2 null characters.

3. Because null characters can appear anywhere in a Unicode-formatted string, it is not appropriate to declare the end of a string in Unicode format with a null character. Therefore, it is important to save the length of the string in 4 reserved words.

4. Let us emphasize again that the BSTR string pointer actually points to the first address of the character array in Unicode format, rather than the beginning of the 4 bytes. And then we'll see that it's so tedious to emphasize that the character of a BSTR string variable is to be compared with the string type in VC + + that is about to be explained.

5. The length of the first 4 bytes records the number of bytes in the character array (note, not the number of characters), including the empty byte at the end. Because the array is in Unicode format, the actual character is half the length.

It is emphasized here that a null character in a Unicode format actually occupies 2 bytes of space instead of 1 bytes. Be careful when testing null characters in arrays of Unicode format.

We generally say the BSTR string "Help" is "a string of BSTR type". It is generally recognized that a BSTR string variable points to a character array that includes at least two null characters.

For Visual Basic, it may not be useful to have two empty bytes of a BSTR string, but they are critical for Win32. The reason for this is that the Win32 version of the Unicode string (which is known as LPWSTR) is defined as a pointer to a string of Unicode-formatted strings that point to the end of a null character.

For this reason, it is reasonable to explain why the BSTR string ends with a null character. Let's discuss the string variables of the C + + type.

Two lines of code:

Dim Str as String

Str= "Help"

STR represents the name of a BSTR string variable, not a character array in Unicode format. In other words, STR is the name of a variable that holds the address xxxx.

The following is a small experiment that shows that a string variable in Visual Basic is a pointer to a character array rather than a character array. The following defines a struct whose member variable is of type string.

Private Tyep Uttest

Astring as String

Bstring as String

End Type

Dim Utest as Uttest

Dim S as String

s= "Testing"

utest.astring= "Testing"

utest.astring= "Testing"

Debug.Print Len (s)

Debug.Print Len (Utest)

The result of the execution of these lines of code is:

7

8

For string variables, the Len function returns the number of characters in an array of strings. So the 7-character string "testing" returns 7. For structural variable utest, the Len function returns the memory space occupied by the structure. So a return value of 8 clearly shows that each BSTR variable consumes 4 bytes in memory. Because the BSTR is a WIN32 pointer!

C-Type LPSTR and LPWSTR strings

Visual C + + uses LPSTR and LPWSTR strings.

The LPSTR type string is defined as a pointer to an ANSI-formatted string array that ends in an empty byte. However, since we are judging the termination of the LPSTR string in the null byte position, it is not allowed in the LPSTR to have a second empty byte in the string. Similarly, LPWSTR is a pointer to a null-byte-terminated Unicode-formatted string, and the middle of it does not allow empty bytes to exist. The W in LPWSTR refers to wide, which is another Microsoft statement about Unicode.

We may also encounter strings of type LPCSTR and LPCWSTR. where C represents constant (constant). This string cannot be modified by the API function. In addition, LPCSTR are the same as LPSTR. Similarly, LPCWSTR can not be modified, but others are the same as LPWSTR.

Besides, LPTSTR,LPTSTR is generally used in conditional compilation, just like TCHAR. Here is an example code:

#ifdef UNICODE

typedef LPWSTR LPTSTR; LPTSTR is the same as LPWSTR under Unicode

typedef LPCWSTR LPCTSTR; LPCTSTR is the same as LPCWSTR under Unicode

#else

typedef LPSTR LPTSTR; Under ANSI LPTStr and LPSTR are the same

typedef LPCSTR LPCTSTR; Under ANSI Lptcstr and LPCSTR are the same

#endif


_bstr_t and BSTR

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.