COleSafeArray::GetElement

检索安全数组的一个元素。

void GetElement(
   long* rgIndices,
   void* pvData 
);

参数

  • rgIndices
    对数组的指针数组每一维的索引。

  • pvData
    用于将数组元素的位置的指针。

备注

在检索元素之前,此功能可自动调用windows函数 SafeArrayLockSafeArrayUnlock。如果数据元素是字符串、对象或变量,该功能复制元素将正确方式。该参数 pvData 应指向足够大缓冲区包含元素。

在错误,该函数引发 CMemoryExceptionCOleException

示例

//sa is of type COleSafeArray with 2 dimensions

//Determine upper bounds for both dimensions
long lNumRows;
long lNumCols;
sa.GetUBound(1, &lNumRows);
sa.GetUBound(2, &lNumCols);

//Display the elements in the SAFEARRAY.
long index[2];
VARIANT val;

//Determine lower bounds for both dimensions
long lowRow, lowCol;
sa.GetLBound(1, &lowRow);
sa.GetLBound(2, &lowCol);

for(long r = lowRow; r <= lNumRows; r++ )
{
   for(long c = lowCol; c <= lNumCols; c++ )
   {
      index[0] = r;
      index[1] = c;

      //retrieve each element of the safearray
      sa.GetElement(index, &val);

      switch(val.vt)
      {
      case VT_R8:
         TRACE(_T("%1.2f\n"), val.dblVal);
         break;

      case VT_BSTR:
         TRACE(_T("%s\n"),(CString)val.bstrVal);
         break;

      // other cases ommitted

      case VT_EMPTY:
         TRACE(_T("<empty>\n"));
         break;
      }
   }
}

要求

Header: afxdisp.h

请参见

参考

COleSafeArray选件类

层次结构图

COleSafeArray::PutElement