COleSafeArray::AccessData

void AccessData( void** ppvData );

Parameters

ppvData

A pointer to a pointer to the array data.

Remarks

Retrieves a pointer to the array data. On error, the function throws a CMemoryException or COleException.

Example

//Sort is an automation method by the CCmdTarget-derived class 
// CSAProjDlgAutoProxy
void CSAProjDlgAutoProxy::Sort(VARIANT FAR* vArray) 
{
   COleSafeArray sa;
   BSTR *pbstr;
   TCHAR buf[1024];
   LONG cElements, lLBound, lUBound;
  
   //needed for OLE2T macro below, include afxpriv.h
   USES_CONVERSION;

   // Type check VARIANT parameter. It should contain a BSTR array
   // passed by reference. The array must be passed by reference it is
   // an in-out-parameter.
   if (V_VT(vArray) != (VT_ARRAY | VT_BSTR))
      AfxThrowOleDispatchException(1001, 
         "Type Mismatch in Parameter. Pass a string array by reference");

   // clears data in sa and copies the variant data into sa
   sa.Attach(*vArray);

   // Check that array is 1 dimensional
   if (sa.GetDim() != 1)
      AfxThrowOleDispatchException(1002, 
         "Type Mismatch in Parameter. Pass a one-dimensional array");

   try 
   {
      // Get array bounds.
      sa.GetLBound(1, &lLBound);
      sa.GetUBound(1, &lUBound);

      // Get a pointer to the elements of the array
      // and increments the lock count on the array
      sa.AccessData((LPVOID*)&pbstr);

      //get no. of elements in array
      cElements = lUBound-lLBound+1;
      for (int i = 0; i < cElements-1; i++)
      {
         //output the elements of the array
         wsprintf(buf, "%s", OLE2T(pbstr[i]));
         OutputDebugString(buf);
      }
      
      //decrement lock count
      sa.UnaccessData();
   }
   catch (COleException *pEx)
   {
      AfxThrowOleDispatchException(1003, 
         "Unexpected Failure in FastSort method");
      pEx->Delete();
   }
}

COleSafeArray OverviewClass MembersHierarchy Chart

See Also   COleSafeArray::UnaccessData, SafeArrayAccessData