SafeArrayCreateEx Function
Creates and returns a safe array descriptor from the given VARTYPE, number of dimensions and bounds.
SAFEARRAY SafeArrayCreateEx( VARTYPE vt, unsigned int cDims, SAFEARRAYBOUND * rgsabound PVOID pvExtra );
If the VARTYPE is VT_RECORD then SafeArraySetRecordInfo is called. If the VARTYPE is VT_DISPATCH or VT_UNKNOWN then the elements of the array must contain interfaces of the same type. Part of the process of marshaling this array to other processes does include generating the proxy/stub code of the IID pointed to by pvExtra parameter. To actually pass heterogeneous interfaces one will need to specify either IID_IUnknown or IID_IDispatch in pvExtra and provide some other means for the caller to identify how to query for the actual interface.
The following example describes how a safe array of user-defined types is stored into a variant of type VT_RECORD:
SAFEARRAYBOUND * sab;
sab.cElements = 2;
sab.lLbound = 0;
hresult hr;
SAFEARRAY Sa;
Sa = SafeArrayCreateEx(VT_RECORD, 1, &sab, pRecInfo);
if (Sa == NULL)
return E_OUTOFMEMORY;
PVOID pvData;
hr = SafeArrayAccessData(Sa, &pvData);
if (FAILED(hr)) {
SafeArrayDestroy(Sa);
return hr;
}
TEST * pTest;
pTest = (TEST *)pvData;
pTest[0] = a;
pTest[1] = b;
hr = SafeArrayUnaccessData(Sa);
if (FAILED(hr)) {
SafeArrayDestroy(Sa);
return hr;
}
VariantInit(&variant);
V_VT(&variant) = VT_ARRAY|VT_RECORD;
V_ARRAY(&variant) = Sa;