Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

CComSafeArray Class

This class is a wrapper for the SAFEARRAY structure.

template <
   typename T,
   VARTYPE _vartype = _ATL_AutomationType< T >::type
>
class CComSafeArray

Parameters

T
The type of data to be stored in the array.

Remarks

CComSafeArray provides a wrapper for the SAFEARRAY class, making it a simple matter to create and manage single- and multidimensional arrays of almost any of the VARIANT-supported types.

CComSafeArray simplifies passing arrays between processes, and in addition provides extra security by checking array index values against upper and lower bounds.

The lower bound of a CComSafeArray can start at any user-defined value; however, arrays that are accessed through C++ should use a lower bound of 0. Other languages such as Visual Basic may use other bounding values (for example, -10 to 10).

Use CComSafeArray::Create to create a CComSafeArray object, and CComSafeArray::Destroy to delete it.

A CComSafeArray can contain the following subset of VARIANT data types:

VARTYPE Description
VT_I1 char
VT_I2 short
VT_I4 int
VT_I4 long
VT_I8 longlong
VT_UI1 byte
VT_UI2 ushort
VT_UI4 uint
VT_UI4 ulong
VT_UI8 ulonglong
VT_R4 float
VT_R8 double
VT_DECIMAL decimal pointer
VT_VARIANT variant pointer
VT_CY Currency data type

Requirements

Header: atlsafe.h

Example

// Create a multidimensional array, 
// then write and read elements

#include "stdafx.h"
#include "atlsafe.h"

int main(int argc, char* argv[])
{
   // Define an array of character pointers
   CComSafeArray<char> *pSar;

   char cElement;
   char cTable[2][3] = {'A','B','C','D','E','F'};

   // Declare the variable used to store the
   // array indexes
   LONG aIndex[2];

   // Define the array bound structure
   CComSafeArrayBound bound[2];
   bound[0].SetCount(3);
   bound[0].SetLowerBound(0);
   bound[1].SetCount(3);
   bound[1].SetLowerBound(0);   
   
   // Create a new 2x3 dimensional array
   pSar = new CComSafeArray<char>(bound,2); 

   // Use SetAtMultiDimensional to store
   // characters in the array
   for (int x = 0; x < 2; x++)
   {
      for (int y = 0; y < 3; y++)
      {
         aIndex[0] = x;
         aIndex[1] = y;
         ATLASSERT(pSar->MultiDimSetAt(aIndex,cTable[x][y]) == S_OK);
      }
   }
   // Use GetAtMultiDimensional to retrieve
   // characters in the array
   for (int x = 0; x < 2; x++)
   {
      for (int y = 0; y < 3; y++)
      {
         aIndex[0]=x;
         aIndex[1]=y;
         ATLASSERT(pSar->MultiDimGetAt(aIndex,cElement) == S_OK);
         ATLASSERT(cElement == cTable[x][y]);
      }   
   }
   return 0;
}

See Also

Class Members | SAFEARRAY | CComSafeArray::Create | CComSafeArray::Destroy | ATL Class Overview

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.