Locale functions don't effect _variant_t::ChangeType

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
My application contains code that attempts to convert a _variant_t with BSTR content to a currency value, using _variant_t::ChangeType( VT_CY ).  The string is always in the format "1.0" - the decimal separator is always a full stop, irrespective of the system locale.

The problem comes when running on a locale where the decimal separator is not a full stop - a comma for example.  The ChangeType function fails with a "Type Mismatch" exception - which is entirely reasonable given that it expects the string input to be in the format "1,0".

From examining the source code, I can see that the implementation of _variant_t::ChangeType uses the API function VariantChangeType.  My understanding is that by calling SetThreadLocale, passing the LCID of a locale that uses the full-stop decimal separator, before making the ChangeType call should change the behaviour of VariantChangeType to successfully convert the string to a currency value irrespective of the system locale.

Unfortunately, SetThreadLocale doesnt have any effect.  I have confirmed that the locale is changed to English(British) from the system default Polish, by calling GetThreadLocale before and after SetThreadLocale.  Is this behaviour expected?  If so, please can someone tell me how to change the behaviour of _variant_t::ChangeType to acheive what I need.

Thanks,
Ian

View the full article
 
Back
Top