0

I need to perform a safe conversion between a _variant_t and a type such as long\bool_bstr_t etc.

There are many options to do this, but I'm getting lost in the (poor) documentation. For example for conversion to long: Option 1: Check if the variant type is long (==VT_I4), and if so perform a static cast. Option 2: Use an extractor, which I don't fully understand. The MSDN says that if the VARIANT is not already the proper type, VariantChangeType is used to attempt a conversion. However VariantChangeType returns an error code if it fails. Where do I get this error code? The return type should be of the type we're extracting to shouldn't it?

I'd appreciate the most safe solution and an explanation of the various options.

Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
user181218
  • 1,655
  • 5
  • 28
  • 42
  • any language of choice? – Simon Mourier Apr 03 '13 at 13:28
  • Fixed the description. Thanks. :) – user181218 Apr 03 '13 at 13:47
  • VariantChangeType is really the way to go. What do you mean by "where do I get this error code". Official doc is here: http://msdn.microsoft.com/en-us/library/windows/desktop/ms221258.aspx – Simon Mourier Apr 03 '13 at 13:57
  • If you look at the MSDN page on extractors, you'll see that under remarks they say:"Extracts raw data from an encapsulated VARIANT. If the VARIANT is not already the proper type, VariantChangeType is used to attempt a conversion, and an error is generated upon failure". What I don't understand is where do I see this error. The return type of the extractors is their own name. http://msdn.microsoft.com/en-us/library/ew0bcz27(v=vs.80).aspx – user181218 Apr 03 '13 at 14:37
  • Ok, these are C++ oriented wrappers around the VariantChangeType Windows API. If an error occurs using these (ie: if the VariantChangeType call returns a failure HRESULT), it will throw a _com_error object that contains this HRESULT value. – Simon Mourier Apr 03 '13 at 15:42
  • This is the answer I was looking for. Thank you. Where is what you've just said documented? – user181218 Apr 04 '13 at 12:02
  • It's the general behavior of Compiler COM Support Classes http://msdn.microsoft.com/en-us/library/5yb2sfxk.aspx you can check directly in the source that's provided, in the `C:\Program Files (x86)\Microsoft Visual Studio XX.0\VC\include\comutil.h` header file. – Simon Mourier Apr 04 '13 at 12:27

0 Answers0