Search code examples
windowsvisual-c++variantcoercion

Windows Variant Coercion Rules?


I have a Windows VARIANT holding a BSTR with a (supposedly) numeric value.

I want to use VariantChangeType to convert this BSTR value to a VT_I4 or VT_I8 or ...

To my surprise, passing in a string value of "5.6", . being my decimal separator, the function succeeds for integer conversion, and the resulting lValor llVal value is 6! I would have expected either a conversion error, or a value of 5, which is what sscanf gives me.

--> Is there any documentation for the conversion rules used by the VARIANT conversion functions? <--

I found that VariantChangeType is implemented through (e.g.) VarI4FromStr, but there is zero info wrt. the conversion rules used by this function.


Solution

  • There does not seem to be any documentation with regard to this.

    From the linked MSDN thread and a bit of debugging, I can conclude:

    • VariantChangeType does call the Data Type Conversion Functions (as noted)
    • VarI4FromStr seems to call VarR8FromStr internally - this explains how we have rounding
    • VarR*FromStr seems to call VarParseNumFromStr
    • VarParseNumFromStr does not seem to further delegate the numeric parsing itself (quick test of mine in VS2010); it doesn't document any parsing rules AFAIK.
    • Rounding seems to use Round Half to Even or some variation.