Safe Array
-
Hi, I have the following prototype in ActiveX to send the Array from my client program. The variable VarArray contains array of values, Can you please provide me an example to get the values of each index by using SafeArray. void CSimpleAdditionAtxCtrl::Final(VARIANT* VarArray) { }
-
Hi, I have the following prototype in ActiveX to send the Array from my client program. The variable VarArray contains array of values, Can you please provide me an example to get the values of each index by using SafeArray. void CSimpleAdditionAtxCtrl::Final(VARIANT* VarArray) { }
-
Hi, I have the following prototype in ActiveX to send the Array from my client program. The variable VarArray contains array of values, Can you please provide me an example to get the values of each index by using SafeArray. void CSimpleAdditionAtxCtrl::Final(VARIANT* VarArray) { }
sivaprakashshanmugam wrote:
I have the following prototype in ActiveX to send the Array from my client program. The variable VarArray contains array of values, Can you please provide me an example to get the values of each index by using SafeArray.
Here m_vecIcecreamFlavors and m_vecIcecreamPrices are vecters of BSTR and float resp.
//Initialize the bounds for the array //Ours is a 2 dimensional array SAFEARRAYBOUND safeBound[2]; //Set up the bounds for the first index //That's the number of flavors that we have safeBound[0].cElements = m_vecIcecreamFlavors.size(); safeBound[0].lLbound = 0; //Set up the bounds for the second index safeBound[1].cElements = m_vecIcecreamPrices.size(); safeBound[1].lLbound = 0 ; ///Initialize the VARIANT VariantInit(VarArray); //The array type is VARIANT //Storage will accomodate a BSTR and a float pVariant->vt = VT_VARIANT | VT_ARRAY; pVariant->parray = SafeArrayCreate(VT_VARIANT,2,safeBound); //Initialize the vector iterators std::vector::iterator iterFlavor; std::vector::iterator iterPrices; //Used for indicating indexes in the Multidimensional array long lDimension[2]; int iFlavorIndex = 0; //Start iteration iterPrices = m_vecIcecreamPrices.begin(); iterFlavor = m_vecIcecreamFlavors.begin(); //Iterate thru the list of flavors and prices while(iterFlavor != m_vecIcecreamFlavors.end()) { //Put the Element at (0,0), (0,1) , (0,2) ,.............(0,n) lDimension[1] = iFlavorIndex; lDimension[0] = 0; CComVariant variantFlavor(SysAllocString((*iterFlavor).m_str)); SafeArrayPutElement(pVariant->parray,lDimension,&variantFlavor); //Put the Element at (1,0), (1,1) , (1,2) ,.............(1,n) lDimension[1] = iFlavorIndex; lDimension[0] = 1; CComVariant variantPrices(*iterPrices); SafeArrayPutElement(pVariant->parray,lDimension,&variantPrices); iFlavorIndex++; iterPrices++; iterFlavor++; }
Suppose Variant result is parameter passed for fillup then we can retrive the elements from the array as follows.long lUDimension,lLDimension; SafeArrayGetUBound(result->parray,2,&lUDimension); SafeArrayGetLBound(result->parray,2,&lLDimension); VARIANT val[2]; long Dimension[2]; for(LONG i=lLDimension;i<=lUDimension;i++) { m_Data.InsertItem(i," "); Dimension[0]=0; Dimension