Something like this merged with your code? You have to work with the limits of the safearray
LONG lLBound, lUBound;
long element; HRESULT hr;
hr = SafeArrayGetLBound(*pEventCode, 1, &lLBound);
if (FAILED(hr))
{
}
hr = SafeArrayGetUBound(*pEventCode, 1, &lUBound);
if (FAILED(hr))
{
}
for( element = lLBound; element <= lUBound; element++ )
{
hr = SafeArrayPutElement(*pEventCode, &element, &dEventCode);
if(FAILED(hr))
{
}
}