BSTR details 1

Source: Internet
Author: User
(Conversion) BSTR explanation 1-BSTR inclusion class 1 Why need BSTRCom is a cross-programming language platform that provides language-independent data types. Most programming languages have their own string representation.
  • The C ++ string is an array of ASCII or Unicode characters ending with 0.
  • A Visual Basic string is an ASCII character array with a prefix indicating the length.
  • The Java string is an array of Unicode characters ending with 0.
A common string type needs to be defined and can be easily matched to different programming languages. In C ++ Is BSTR. 2 What is BSTR 2.1BSTRIntroduction " Basic stringThe standard string data type defined by Microsoft in COM/Ole. For C ++, the Windows header file wtypes. H is defined as follows: typedef wchar_t wchar; typedef wchar olechar; typedef olechar _ rpc_far * BSTR ;; 2.2BSTR Implementation In COM, the characters are represented by 16-bit olechar, so that com can support various code pages, including Unicode. For Windows systems, it can be simply understood that olechar uses Unicode. The olechar string is similar to a single-byte string and is a buffer ending with null. The only difference is that each character occupies two bytes, instead of a 0 1 2 3 4 5 6 7 8 9 0 1 | H | E | L | o | \ 0 | ^ olchar figure 1. format of an olechar string. it is inconvenient to use a simple string ending with null to pass between COM component. Therefore, Standard BSTR Is a long prefix and Null End Olechar Array.The first four bytes of BSTR are a prefix that represents the string length. The value of the BSTR length field is the number of bytes of the string, excluding the 0 Terminator. Because it is a unicode string, the number of characters is half the number of bytes. This method allows programmers to embed null characters in the middle of a BSTR string. However, the first four bytes of BSTR indicate the length, while the first four bytes of the olechar array indicate the first two characters. In this case, how do I implement the exchange between BSTR and olechar FOR THE C ++ program? The answer is that com provides two APIs for BSTR allocation: sysallocstring/sysreallocstring. The pointer returned by the function points to the first character of BSTR, instead of the first byte of BSTR in memory.

0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 0a000000 | H | E | L | o | \ 0 | ^ BSTR

Figure 2. Format of a bstr. below is the pseudo code of sysallocstring and sysfreestring. BSTR simplesysallocstring (const olechar * sz) {If (SZ = NULL) return NULL; byte * Buf = new byte [sizeof (int32) + (wcslen (sz) + 1) * sizeof (olechar)]; If (BUF = NULL) {return NULL;} else {int32 Len = wcslen (sz) * sizeof (olechar); * (int32 *) buf) = Len; wcscpy (wchar *) (BUF + sizeof (int32), SZ); Return (BSTR) (BUF + sizeof (int32 ));}} void simplesysfreestring (BSTR) {If (BSTR! = NULL) {byte * Start = (byte *) BSTR-sizeof (int32); Delete [] Start ;}}

 

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.