BSTR and Variant

Source: Internet
Author: User
First, BSTR OverviewIt is described as a type compatible with automation, because the operating system provides the appropriate API functions (such as sysallocstring) to manage it and some default dispatch code. So BSTR is actually a COM string, but it is widely used in a variety of contexts other than automation technology. Why need BSTRCOM is a Cross-platform language platform that needs to provide language-independent data types.   Most programming languages have their own string representations.   The C + + string is an array of ASCII or Unicode characters ending with 0.   The Visual basic string is an array of ASCII characters plus a prefix representing the length.   The Java string is an array of Unicode characters ending with 0. You need to define a generic string type that can be easily matched to different programming languages. in C + +, is the BSTRWhat is BSTRBSTR is the abbreviation for "Basic string", a standard string data type defined by Microsoft in Com/ole.   The following is defined for the C++,windows header file wtypes.h: typedef wchar_t WCHAR;   typedef WCHAR OLECHAR;   typedef OLECHAR __RPC_FAR *BSTR;; Using a null-terminated simple string is not convenient for passing between COM component. So The Standard BSTR is a Olechar array with a length prefix and a null terminator. The first 4 bytes of BSTR are a prefix representing the length of the string.   The value of the BSTR length field is the number of bytes in the string and does not include a 0 terminator. Because it is a Unicode string, the number of characters is half the number of bytes. The advantage of this approach is that it allows programmers to embed null characters in the middle of a BSTR string. However, the first four bytes of the BSTR represent the length, whereas the first four bytes of the Olechar array represent the first two characters. In this case, for C + + programs, how to achieve BSTR and Olechar exchange. The answer is that COM provides two BSTR allocations for api:sysallocstring/sysreallocstring. The function returns a pointer to the first character of the BSTR, not the first byte of the BSTR in memory. when to use BSTR The actual use of BSTR is not the first choice, only when you have to use the time to consider it.    The use of BSTR in general has the following kinds of situations:COM interface interface definitions, and you do not want to provide an additional custom marshaling library (Mdil build or custom made by the developer), you must use BSTR to pass strings. Strings that are passed in a COM DLL using a C + + type string are available on the surface, but violate COM's basic rules and leave a hidden danger for later extensions. For example, a in-process COM object (Simple com DLL) is changed to Out-of-process object (COM EXE). Theoretically, the client's code should not make any changes.   However, if you use a C + + string, and you want to use only the system's Automation Mashaller (Oleaut32.dll), there will be an error.   If you can provide custom marshaling, you also recommend using BSTR.   The customer requests that the interface must use BSTR, and after discussion with the customer, cannot be modified. Use the interface of the external library using BSTR Non-use cases: It is not recommended to define BSTR members in the IDL structure body, which can cause problems in the duplication and release of the structure. It is best to use a TCHAR array that is limited to the maximum length directly.   If you do need to pass a variable-length string, BSTR should be defined as a separate parameter or use a separate Get/set interface. Scope of the BSTR and related types as narrow as possible。 The member variables and function parameters of the class do not use BSTR. Local variables should be released as soon as possible within the class without using BSTR. The code processing logic only uses BSTR in the directly related parts of the interface. As soon as a BSTR is received, it is processed as quickly as possible into a C + + string copy. The BSTR is generated before the BSTR parameter needs to be passed, and immediate release is used.

Second, VARIANT
VARIANTThe structure can refer to the header file Vc98\include\oaidl. h in relation to structural body tagVARIANTThe definition.

struct   tagvariant {     Union            {         struct   __tagvariant              {      &NBSP ;     VARTYPE VT;             WORD wReserved1;             WORD wReserved2;             WORD wReserved3;              Union                    {                LONG lval               &N Bsp BYTE Bval;                 short ival;                 FLOAT Fltval;                 DOUBLE dblval;                 VARIANT_BOOL BoolvAl;                 _variant_bool  BOOL;                 SCODE SCODE;                 CY Cyval;                 Date date;                 BSTR bstrval;                  IUnknown  __RPC_FAR  * punkval;                  IDispatch  __RPC_FAR  * pdispval;                 SAFEARRAY __rpc_far  * Parray;                 BYTE __rpc_far  * pbval;                 Short __rpc_far  * PIVAL;                 LONG __rpc_far  * plval;                 FLOAT __rpc_far  * PFLTVAL;                 DOUBLE __rpc_far  * pdblval;                 VARIANT_BOOL __rpc_far  * pboolval;                 _variant_bool __rpc_far  * pbool;                 SCODE __rpc_far  * PSCODE;                 CY __rpc_far  * pcyval;                 DATE __rpc_far  * pdate;                 BSTR __rpc_far  * pbstrval;                  IUnknown  __RPC_FAR  * __rpc_far  * ppunkval ;                  IDispatch  __RPC_FAR  * __rpc_far  * PPDISPVA L                 SAFEARRAY __rpc_far  * __RPC_FAr  * Pparray;                 VARIANT __rpc_far  * pvarval;                 PVOID ByRef;                 CHAR Cval;                 USHORT Uival;                 ULONG Ulval;                 INT intval;                 UINT Uintval;                 DECIMAL __rpc_far  * pdecval;                 CHAR __rpc_far  * pcval;                 USHORT __rpc_far  * puival;                 ULONG __rpc_far  * pulval;                 INT __rpc_far  * pintval;       &NBsp         UINT __rpc_far  * puintval;                  struct   __tagbrecord                      {                    PV OID Pvrecord;                      irecordinfo  __RPC_FAR  * precinfo;                       __variant_name_4;                   __variant_name_3;               __variant_name_2;         DECIMAL Decval;         }  __variant_name_1; };


Assignment for Variant variables: First assign values to VT members, indicate data types, and assign values to variables of the same data type in the federated structure, for example:

VARIANT va;

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.