long i, j, min; BSTR bstrTemp; BSTR HUGEP *pbstr; HRESULT hr;// Get a pointer to the elements of the array. hr = SafeArrayAccessData(psa, (void HUGEP* FAR*)&pbstr); if (FAILED(hr)) goto error;// Bubble sort. cElements = lUBound-lLBound+1; for (i = 0; i < cElements-1; i++) { min = i; for (j = i+1; j < cElements; j++) { if (wcscmp(pbstr[j], pbstr[min]) < 0) min = j; } // Swap array[min] and array[i]. bstrTemp = pbstr[min]; pbstr[min] = pbstr[i]; pbstr[i] = bstrTemp;}SafeArrayUnaccessData(psa);
BSTR bstrTemp;
BSTR HUGEP *pbstr;
HRESULT hr;// Get a pointer to the elements of the array.
hr = SafeArrayAccessData(psa, (void HUGEP* FAR*)&pbstr);
if (FAILED(hr))
goto error;// Bubble sort.
cElements = lUBound-lLBound+1;
for (i = 0; i < cElements-1; i++)
{
min = i;
for (j = i+1; j < cElements; j++)
{
if (wcscmp(pbstr[j], pbstr[min]) < 0)
min = j;
} // Swap array[min] and array[i].
bstrTemp = pbstr[min];
pbstr[min] = pbstr[i];
pbstr[i] = bstrTemp;}SafeArrayUnaccessData(psa);
{
unsigned int i;
HRESULT hresult;
VARIANT var;
SAFEARRAY FAR* psa;
CEnumPoint FAR* penum;
POINTLINK FAR* ppointlink;
SAFEARRAYBOUND rgsabound[1];
rgsabound[0].lLbound = 0;
rgsabound[0].cElements = m_cPoints;
psa = SafeArrayCreate(VT_VARIANT, 1, rgsabound);
if(psa == NULL){hresult = ReportResult(0, E_OUTOFMEMORY, 0, 0);
goto LError0} // Code omitted here for brevity.LError0:;
return hresult;
}
ITypeLib *pTypeLib = NULL;
ITypeInfo *pTypeInfo = NULL;
IRecordInfo *pRecordInfo = NULL; SAFEARRAY *psa = NULL;
SAFEARRAYBOUND rgsabound;
struct tagXXX *pXXX = NULL;
LoadRegTypeLib(类型库的GUID, 1, 0, GetUserDefaultLCID(), &pTypeLib);
pTypeLib->GetTypeInfoOfGuid(结构体struct tagXXX的GUID, &pTypeInfo);
GetRecordInfoFromTypeInfo(pTypeInfo, &pRecordInfo);
pTypeInfo->Release();
pTypeLib->Release(); rgsabound.cElements = NUM;
rgsabound.lLbound = 0;
psa = SafeArrayCreateEx(VT_RECORD, 1, &rgsabound, pRecordInfo);
pRecordInfo->Release(); SafeArrayAccessData(psa, (void**)(&pXXX));
// ...
SafeArrayUnaccessData(psa);
safearray中除了有数组的数据(指针),还有数组的维数,每维的大小等信息。