The VARIANT structure is used for communication between COM objects. Essentially, the VARIANT structure is a container for a large union that carries many types of data. For more information, see VARIANT and VARIANTARG.
The value in the first member of the structure, vt, describes which of the union members is valid. When you receive information in a VARIANT structure, check the vt member to find out which member contains valid data. Similarly, when you send information using a VARIANT structure, always set vt to reflect the union member that contains the information.
Before using the structure, initialize it by calling the VariantInit COM function. When finished with the structure, clear it before the memory that contains the VARIANT is freed by calling VariantClear.
The SAFEARRAY structure is provided as a way to safely work with arrays in Automation. The VARIANT's parray field is a pointer to a SAFEARRAY. Use functions such as SafeArrayCreateVector, SafeArrayAccessData, and SafeArrayUnaccessData to create and fill a SAFEARRAY in a VARIANT.
The following C++ example demonstrates how to create an IInkStrokeDisp object, pInkStrokeDisp, in an IInkDisp object, pInk, from an array of point data.
VARIANT var, varPK;
LONG* plongArray=NULL;
POINT ptArray[2]={0};
long lSize=0;
IInkStrokeDisp* pInkStrokeDisp;
IInkDisp* pInk; // the object should be created correctly
// elsewhere and assigned here.
HRESULT hr=E_FAIL;
ptArray[0].x = 20;
ptArray[0].y = 100;
ptArray[1].x = 30;
ptArray[1].y = 110;
lSize = 2; // two points
VariantInit( &var );
VariantInit( &varPK );
SAFEARRAY* psa = SafeArrayCreateVector( VT_I4, 0, lSize*2 );
if( psa )
{
if( SUCCEEDED( hr = SafeArrayAccessData( psa, (void**)&plongArray) ))
{
for( long i = 0; i < lSize; i++ )
{
plongArray[2*i] = ptArray[i].x;
plongArray[2*i+1] = ptArray[i].y;
}
hr = SafeArrayUnaccessData( psa );
if ( SUCCEEDED( hr ) )
{
var.vt = VT_ARRAY | VT_I4;
var.parray = psa;
// varPK (packet description) is currently reserved, so it is
// just an empty variant for now.
pInk->CreateStroke( var, varPK, &pInkStrokeDisp );
}
}
}
VariantClear( &var );
VariantClear( &varPK );