I safearg sono creati con SafeArrayCreate
o SafeArrayCreateVector
, ma come chiedete di iterare su un SAFEARRAY, diciamo che avete già un SAFEARRAY restituito da qualche altra funzione. Un modo è utilizzare l'API SafeArrayGetElement
che è particolarmente comoda se si dispone di SAFEARRAY multidimensionali, poiché consente, IMO, di specificare un po 'più facilmente gli indici.
Tuttavia, per i vettori (SAFEARRAY unidimensionale) è più rapido accedere ai dati direttamente e iterare sui valori. Ecco un esempio:
Diciamo che è un SAFEARRAY di long
s, vale a dire. 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);
fonte
2012-09-18 20:12:23
Che cos'è 'cmd'? ! –