Packing a array inside a VARIANT (VC++ 6.0) (Series 1 of N)

VARIANTs are versatile data types, they can hold any type of value, apart from that they can hold array of different types. In this post I am going to explain how can you pack a array of bytes, inside a variant variable.

Packing  a array of simple types inside a Variant

Packing  a array of bytes

int GetVariantWithByteBuffer (int nBufferSize, VARIANT FAR* pVar)
{

    /* here size of buffer desired (nBufferSize) has been passed to the function, which can be determined before hand. */

/* Creating a temporary buffer to hold the values one need to return and filling that buffer. fill buffer, this buffer can be filled some other ways too, reading from a file or reading from some hardware or whatever  here it has been filled from values 1 – 255*/

     BYTE *pBuffer = (BYTE *) calloc ( nBufferSize, sizeof (BYTE))
     for ( int i = 0 ;  i < nBufferSize; i++ ) pBuffer [i] = ( i + 1 ) % 256 ; 

     /* Creating a variant to return, which can hold the safe array of bytes .*/

     SAFEARRAY FAR* psa ;
     psa = SafeArrayCreateVector (VT_UI1, 0, nBufferSize); 
      if(psa == NULL)    return –1 ;  
      
     /* Copying the temporary buffer to the variant to return */

     VariantInit ( pVar ) ;

     pVar->parray = psa;
     pVar->vt = VT_ARRAY | VT_UI1 ; 
     memcpy ( pVar->parray->pvData , pBuffer, nBufferSize * sizeof (BYTE) ) ;  

    /* free the temporary buffer */

    free ( pBuffer ) ;

    /* return the number of elements  this variant hold. */
    return nBufferSize;

}

as it can be seen from the above code, it involves following steps.

  1. Allocating the desired temporary buffer space to hold the array of bytes, and filling that array with values one need to return.
  2. creating a safe array of the desired (VT_UI1) type.
  3. copying the temporary buffer to the safe array of the variant.
  4. free the memory allocated for temporary buffer. (avoid memory leaks).

Fetching a array of bytes from a variant.

BYTE * GetByteBufferFromVariant (int nBufferSize, VARIANT var )
{

    /* here size of buffer desired (nBufferSize) has been passed to the function, which can help us determine the size of the buffer required. */

    BYTE *pBuffer = (BYTE *) calloc ( nBufferSize, sizeof (BYTE)) ;  
    memcpy ( pBuffer, var.parray->pvData, nBufferSize * sizeof (BYTE) ) ;  
    return pBuffer ;

}

In  a similar fashion array of some other simple types can be  packed in a variant. the following table describes the data types for different simple types.

S.N. Data Type Value of vt
1 BYTE VT_ARRAY | VT_UI1
2 SHORT (2 byte signed integer ) VT_ARRAY | VT_I2
3 USHORT (2 byte unsigned integer ) VT_ARRAY | VT_UI2
4 INT (4 byte signed integer ) VT_ARRAY | VT_I4
5 UINT (4 byte unsigned integer ) VT_ARRAY | VT_UI4
6 FLOAT ( 4 byte float value) VT_ARRAY|VT_R4
7 DOUBLE (8 byte double value) VT_ARRAY|VT_R8

 

using the table above one can pack array of above defined types, to variant, these conversion is useful in middleware scenario. in the next article I will explain how to pack array of strings and array of structure inside a variant.

Leave a comment

Your email address will not be published. Required fields are marked *