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 Data Type 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
// 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 2 dimensional array
// each dimension size is 3
pSar = new CComSafeArray<char>(bound,2);
// Use MultiDimSetAt 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;
HRESULT hr = pSar->MultiDimSetAt(aIndex,cTable[x][y]);
ATLASSERT(hr == S_OK);
}
}
// Use MultiDimGetAt 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;
HRESULT hr = pSar->MultiDimGetAt(aIndex,cElement);
ATLASSERT(hr == S_OK);
ATLASSERT(cElement == cTable[x][y]);
}
}
Smart Device Developer Notes
Only the following VARTYPEs are fully supported: VT_BSTR, VT_VARIANT, VT_UNKNOWN, and VT_DISPATCH.