SafeArrays
USES_CONVERSION; // enables use of ATL conversion macro A2W
char buffer[20]; // used to store ANSI string
HRESULT hr= S_OK;
// Create SafeArray of VARIANT BSTRs
SAFEARRAY *pSA;
SAFEARRAYBOUND aDim[1]; // a one dimensional array
aDim[0].lLbound= 0; // Visual Basic arrays start with index 0
aDim[0].cElements= 10;
pSA= SafeArrayCreate(VT_VARIANT,1,aDim); // create a 1D SafeArray of VARIANTS
if (pSA != NULL) {
long aLong[1];
// iterate over array adding VARIANTs of type VT_BSTR
for (long l= aDim[0].lLbound; l< (aDim[0].cElements + aDim[0].lLbound); l++) {
VARIANT vOut;
VariantInit(&vOut);
vOut.vt= VT_BSTR; // set type
ltoa(l,buffer,10); // convert long to ANSI string value
vOut.bstrVal= ::SysAllocString(A2W(buffer)); // system wide 'new'
aLong[0]= l; // set index value
if (hr= SafeArrayPutElement(pSA, aLong, &vOut)) { // 'correctly' copies VARIANT
VariantClear(&vOut); // release BSTR from memory on error
SafeArrayDestroy(pSA); // does a deep destroy on error
return hr;
}
没有评论:
发表评论