SafeArrayCreate
セーフアレイはorで作成されSafeArrayCreateVector
ますが、SAFEARRAY の反復処理について尋ねると、他の関数によって返された SAFEARRAY が既にあるとします。1 つの方法は、SafeArrayGetElement
多次元 SAFEARRAY がある場合に特に便利な API を使用することです。これにより、IMO が許可され、インデックスの指定が少し簡単になります。
ただし、ベクトル (一次元 SAFEARRAY) の場合は、データに直接アクセスして値を反復処理する方が高速です。次に例を示します。
long
s のSAFEARRAY だとしましょう。VT_I4
// get them from somewhere. (I will assume that this is done
// in a way that you are now responsible to free the memory)
SAFEARRAY* saValues = ...
LONG* pVals;
HRESULT hr = SafeArrayAccessData(saValues, (void**)&pVals); // direct access to SA memory
if (SUCCEEDED(hr))
{
long lowerBound, upperBound; // get array bounds
SafeArrayGetLBound(saValues, 1 , &lowerBound);
SafeArrayGetUBound(saValues, 1, &upperBound);
long cnt_elements = upperBound - lowerBound + 1;
for (int i = 0; i < cnt_elements; ++i) // iterate through returned values
{
LONG lVal = pVals[i];
std::cout << "element " << i << ": value = " << lVal << std::endl;
}
SafeArrayUnaccessData(saValues);
}
SafeArrayDestroy(saValues);