Which is better code for converting BSTR parameters to ANSI in C/C++?

后端 未结 4 827
谎友^
谎友^ 2021-01-14 18:28

So far I\'ve discovered I can convert incoming BSTRs to ANSI in two (of many?) ways, and I\'m curious to know whether one is \"better\" than the other with respect to speed

4条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-14 19:14

    From MSDN:

    [...]The recommended way of converting to and from BSTR strings is to use the CComBSTR class. To convert to a BSTR, pass the existing string to the constructor of CComBSTR. To convert from a BSTR, use COLE2[C]DestinationType[EX], such as COLE2T.

    From the CComBSTR page:

    [...]The CComBSTR class provides a number of members (constructors, assignment operators, and comparison operators) that take either ANSI or Unicode strings as arguments. The ANSI versions of these functions are less efficient than their Unicode counterparts because temporary Unicode strings are often created internally. For efficiency, use the Unicode versions where possible.

提交回复
热议问题