Using Variants and SafeArrays

 
 
 

In C++ all variables are strictly typed. For example x may be declared as an int and y as a double. However, scripting languages that support ActiveX (Automation) may use loosely typed variables like Variants and SafeArrays, so a variable named x may contain a string or an integer or even a pointer to an object. The exact type of x is only determined when it is assigned a value, and the type of x can be changed at runtime by re-assigning its value.

The CValue and CValueArray classes provide the convenience of a loosely typed variable or multi-dimensional array inside a strictly typed language like C++. CValue is very similar to the VARIANT structure used in Win32, as CValueArray is to the SAFEARRAY structure. Normally conversion is automatic, for example when using CComAPIHandler or when a custom C++ command is called from scripting; however, it is also possible to convert between the two types using the functions XSIVariantFromCValue and XSIVariantToCValue (see the reference documentation for the sicppsdk__win32_8.h header file).

As a result it is possible to use these classes without any knowledge of difficult ActiveX data types like BSTR and SAFEARRAY. Because CValue provides operator overloads and various constructors it is much easier to use than a VARIANT, and code using CValue will look very similar to the equivalent script code.