COleSafeArray::GetElement
擷取安全陣列中的單一項目。
void GetElement(
long* rgIndices,
void* pvData
);
參數
rgIndices
物件陣列的指標陣列每個維度的索引。pvData
會將陣列的元素之位置的指標。
備註
在擷取項目之前或之後,這個函式自動呼叫 Windows 函式 SafeArrayLock 和 SafeArrayUnlock 。 如果資料項目是字串、物件或變數中,函式會複製項目的正確方式。 參數 pvData 應該指向足夠大的緩衝區包含項目。
在發生錯誤時,擲回例外狀況 CMemoryException 或 COleException。
範例
//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