Are there any easy-to-use, high-level classes or libraries that let you interact with VARIANT
s in Visual C++?
More specifically, I'd like to convert between POD types (e.g. double
, long
), strings (e.g. CString
), and containers (e.g. std::vector
) and VARIANT
s. For example:
long val = 42;
VARIANT var;
if (ToVariant(val, var)) ... // tries to convert long -> VARIANT
comObjPtr->someFunc(var);
std::vector<double> vec;
VARIANT var = comObjPtr->otherFunc();
if (FromVariant(var, vec)) ... // tries VARIANT -> std::vector<double>
I (naively?) assumed that people working with COM do this all the time, so there would most likely be a single convenient library that handles all sorts of conversions. But all that I've been able to find is a motley assortment of wrapper classes that each convert a few types:
- _variant_t or CComVariant for POD types
- _bstr_t, CComBSTR, or BSTR for strings
- CComSafeArray or SAFEARRAY for arrays
Is there any simple way -- short of switching to Visual Basic -- to avoid this nightmare of awkward memory management and bitwise VT_ARRAY | VT_I4
code?
Related questions: