CreateSafeStringArray and CreateSafeDoubleArray |
Top Previous Next |
This is useful in various parts of COM programming:
// Call SafeArrayDestroy after you've finished with the array SAFEARRAY* CreateSafeStringArray(long nElements, TCHAR *elements[]) { SAFEARRAYBOUND saBound[1];
saBound[0].cElements = nElements; saBound[0].lLbound = 0;
SAFEARRAY *pSA = SafeArrayCreate(VT_BSTR, 1, saBound);
if (pSA == NULL) { return NULL; }
for (int ix = 0; ix < nElements; ix++) { BSTR pData = SysAllocString(elements[ix]);
long rgIndicies[1];
rgIndicies[0] = saBound[0].lLbound + ix;
HRESULT hr = SafeArrayPutElement(pSA, rgIndicies, pData); }
return pSA; }
For example in adding tables into sheets.
For doubles use:
// After use call SafeArrayDestroy SAFEARRAY* CreateSafeDoubleArray(long nElements, double* pElements) { SAFEARRAYBOUND rgsabound[1]; rgsabound[0].lLbound = 0; // index starts at 0 rgsabound[0].cElements = nElements; // number of elements
SAFEARRAY FAR* psa = SafeArrayCreate(VT_R8, 1, rgsabound); if (psa == NULL) { return 0; }
HRESULT hr = SafeArrayLock(psa);
for (int i = 0 ; i < nElements ; i++) { LONG rgIndex = i ; double d = pElements[i]; hr = SafeArrayPutElement(psa, &rgIndex, &d); }
hr = SafeArrayUnlock(psa);
return psa; }
|
Text, images and diagrams © 2021 Owen F. Ransen. All rights reserved. (But copy the source code as much as you want!)