masterz(MS MVP):
非常感谢!
有几个疑问:
1.
在第一个例程中
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;
}
VariantClear(&vOut); // does a deep destroy of source VARIANT
} // end iteration
COleSafeArray saRet;
DWORD numElements[2];
numElements[0]= m_iNumRows; //Number of rows in the range.
numElements[1]= m_iNumCols; //Number of columns in the range.
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;
}
VariantClear(&vOut); // does a deep destroy of source VARIANT
} // end iteration
}
// clean up here only if you do not return SafeArray as an [out, retval]
SafeArrayDestroy(pSA); // again does a deep destroy
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;
}
VariantClear(&vOut); // does a deep destroy of source VARIANT
} // end iteration
}
// clean up here only if you do not return SafeArray as an [out, retval]
SafeArrayDestroy(pSA); // again does a deep destroy