Functions for Setting and Retrieving Values from Variant Safe Arrays

Here are two very easy-to-use functions for setting (FillVariant) and retrieving (GetVariant) values from a variant safe array. All of the instructions that you'll need to use these functions can be found in the comments below. Simply copy and paste the code from this article into your files and follow these instructions.


//////////////////////////////////////////////////////////////////////////
// Kishore:-
// include this pice of code in .h file.
// function for putting values to VARIANT->SAFEARRAY
// function for getting values from VARIANT->SAFEARRAY
// 
// @1 Type of Array 
// @2 Actual type contained in the variant
// @3 Variant to read or write 
// @4 Actual array to read or write
// @5 VT_BSTR or VT_DATE ...  (FillVariant)
// Usage:- 
// BSTRArray someThing; someThing.Add(bstr);
// FillVariant<BSTRArray,BSTR>(&toVariant,someThing,VT_BSTR);
//
// _BSTR_TArray someThing;
// GetVariant<_BSTR_TArray,BSTR>(fromVariant,someThing);
// _bstr_t _bstr=someThing.GetSize(),GetAt(),[n]
//
// GetVariant<DATEArray,DATE>(fromVariant,dt);
// GetVariant<OLEDATETIMEArray,DATE>(fromVariant,dt);
// OleDateTime x=dt.GetAt(n) ...GetSize(),[],.....
////////////////////////////////////////////////////////////////////////////

#include <afxtempl.h>

typedef CArray<BSTR,BSTR> BSTRArray;
typedef CArray<DATE,DATE> DATEArray;
typedef CArray<_bstr_t,_bstr_t> _BSTR_TArray;
typedef CArray<COleDateTime,COleDateTime> OLEDATETIMEArray;
typedef CArray<DWORD,DWORD> DWORDArray;

template <class T,class T1> 
void FillVariant(VARIANT* pVariant, T& arrySrc,int iType)
{
 ASSERT(NULL!=pVariant);
 VariantInit(pVariant);
 int iMax = arrySrc.GetSize();

 SAFEARRAY * pSafeArray; 
 SAFEARRAYBOUND aDim[1]; 
 aDim[0].lLbound = 0; 
 aDim[0].cElements = iMax;

 pVariant->vt = VT_ARRAY | iType;

 pSafeArray = SafeArrayCreate(iType, 1, aDim);

 T1* dwArray = NULL;
 SafeArrayAccessData(pSafeArray, (void**)&dwArray);

 for(int nCount = 0; nCount < iMax ; nCount++)
 {
  dwArray[nCount] = (T1)arrySrc[nCount];
 }
 SafeArrayUnaccessData(pSafeArray);
 pVariant->parray = pSafeArray;
} 

template <class T,class T1> 
void GetVariant(VARIANT variant, T& arrySrc)
{
 long lStartBound = 0;
 long lEndBound = 0;
 
 SAFEARRAY* pSafeArray  = variant.parray;
 ASSERT(NULL!=pSafeArray);
 SafeArrayGetLBound(pSafeArray, 1, &lStartBound);
 SafeArrayGetUBound(pSafeArray, 1, &lEndBound);

 T1* arrayAccess = NULL;
 SafeArrayAccessData(pSafeArray, (void**)&arrayAccess);
 
 for(int iIndex = lStartBound; iIndex <= lEndBound; iIndex ++)
 {
  arrySrc.Add((T1)arrayAccess[iIndex]);
 }
 SafeArrayDestroy(pSafeArray);
 SafeArrayUnaccessData(pSafeArray);
}


Comments

  • How to return CArray object from Function

    Posted by renugopal on 06/20/2005 01:48am

    How to pass & return CArray object with Function Thanks -Renugopal

    Reply
  • OMG, your a life saver

    Posted by Legacy on 02/09/2003 12:00am

    Originally posted by: Jeryl Cook

    EXCELLENT!!!

    Reply
  • anybody created 2 or 3 dimensional arrays with this code ?

    Posted by Legacy on 10/17/2002 12:00am

    Originally posted by: SAM

    Pls send or post the code with 2 or 3 dimensional arrays....

    Thank you in Advance

    Reply
  • why ?

    Posted by Legacy on 07/03/2002 12:00am

    Originally posted by: skizmo

    why did M$ build this ??? it is (!Excellent)

    Reply
  • Functions for Setting and Retrieving Values from Variant Safe Arrays

    Posted by Legacy on 01/16/2002 12:00am

    Originally posted by: binoo

    very good coding

    Reply
  • h file..

    Posted by Legacy on 01/04/2002 12:00am

    Originally posted by: mb

    what exactly do I need to have in my H file? I don't understand how to create it.


    Reply
  • dfkfdk

    Posted by Legacy on 09/06/2001 12:00am

    Originally posted by: ashish sambare

    bekkar project

    Reply
  • Mast !

    Posted by Legacy on 05/30/2000 12:00am

    Originally posted by: Vinod Mahajan

    Really helpful code...If One know SafeArray very well then he/she knows COM...VB programmer may find it difficult initially anyway thats the beauty of C++...Thank you...


    Reply
  • Great Piece of Work...Keep it up...

    Posted by Legacy on 05/25/2000 12:00am

    Originally posted by: Kapil

    Great Piece of Work...Keep it up...

    kapil

    Reply
  • Excellent

    Posted by Legacy on 05/01/2000 12:00am

    Originally posted by: Anil Kumar

    It is excellent and ready to use. It was very helpful to us

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: May 6, 2015 @ 1:00 p.m. ET / 10:00 a.m. PT Where are you in your plans to adopt Disaster Recovery-as-a-Service? Are you just getting started? Fighting an uphill battle with management? At Cisco, Zerto and iland, we've seen it all – from the early adopters who excitedly rushed to implement DRaaS with us nine years ago to the IT folks dragging their business leaders into the future. With our years of experience, we've learned there are six types of DRaaS leaders – but which type …

  • Employees must exchange sensitive emails with customers and partners. These emails might contain protected health information, protected financial information, or corporate information that should not be made public. Globalscape® Mail Express® allows you to encrypt the emails that it manages so that no one but the sender and recipient--not even the administrator--can view the contents of the email. "Secure the Transfer of Sensitive Emails" is the property of GlobalSCAPE

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date