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

后端 未结 4 826
谎友^
谎友^ 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:11

    Note:

    If you use the ATL macros, eg: COLE2[C]DestinationType[Ex] (which you probably should), be sure to use the 'C' versions as possible, not the non-const versions as you have written. They may be equivalent for explicit BSTR->ASCII conversions (ie: COLE2A), but for conversions where there is no actual conversion required (eg: COLE2T when compiling for UNICODE), the 'C' versions can expand to noops, whereas the non-'C' versions will still copy if the source string is const (because you are expressing that you need the resulting string to be non-const).

    Also of note:

    The new ATL7 macros don't always require USES_CONVERSION, however they allocate temporary r-value objects, whereas the old macros use _alloca. This may or may not be important, depending on your usage (for example, DO NOT use the old macros in a loop which runs a large number of times, you can blow out the stack doing so).

提交回复
热议问题