I am not sure that an Errata and Clarifications document is needed in this case. This is more of a a "How to" question, and it would seem that the forum is an appropriate location for that.
First off, in C++, enums are integer data types. The SAFEARRAY can be an integer array, and the VARIANT of type VT_ARRAY | VT_I4. I have created some C++ code in VS2008. I am including the main program and two functions: one returns a SAFEARRAY by reference and the other returns a VARIANT wrapping the SAFEARRAY. The code I have to create the SAFEARRAY, and wrap it waith a VARIANT looks like this...
- Code: Select all
//This code defines the CapePhaseStatus enumeration.
typedef enum CapePhaseStatus{
CAPE_UNKNOWNPHASESTATUS = 0,
CAPE_ATEQUILIBRIUM = 1,
CAPE_ESTIMATES = 2
} PhaseStatus;
- Code: Select all
// This class creates the SAFEARRAY and VARIANT
class TestSafeArray
{
public:
TestSafeArray(void){};
~TestSafeArray(void);
HRESULT CreateSafeArray(SAFEARRAY** phaseStatus)
{
// Create an array of Phases Statuses
CapePhaseStatus statuses[6];
statuses[0] = CAPE_ATEQUILIBRIUM;
statuses[1] = CAPE_ESTIMATES;
statuses[2] = CAPE_UNKNOWNPHASESTATUS;
statuses[3] = CAPE_ATEQUILIBRIUM;
statuses[4] = CAPE_ESTIMATES;
statuses[5] = CAPE_UNKNOWNPHASESTATUS;
// Create D array of long integers.
SAFEARRAYBOUND sabdBounds[1] = { sizeof(statuses)/sizeof(CapePhaseStatus), 0};
SAFEARRAY* retVal = SafeArrayCreate(VT_I4, 1, sabdBounds);
retVal->cLocks++;
// Fill array with values
retVal->pvData = &statuses;
*phaseStatus = retVal;
return S_OK;
}
HRESULT CreateVariantArray(VARIANT** phaseStatus)
{
VARIANT *retVal = new VARIANT();
VariantInit(retVal);
retVal->vt = VT_ARRAY | VT_I4;
this->CreateSafeArray(&retVal->parray);
*phaseStatus = retVal;
return S_OK;
}
};
The calling code looks like this...
- Code: Select all
int _tmain(int argc, _TCHAR* argv[])
{
// Create an instance of the class that makes the enum arrays...
TestSafeArray* tester = new TestSafeArray();
// Try as SafeArray...
SAFEARRAY *pSafeArray;
tester->CreateSafeArray(&pSafeArray);
// Get an array of ints from the SafeArray
SAFEARRAYBOUND arrayBounds = pSafeArray->rgsabound[0];
int lowerBound = (int)arrayBounds.lLbound;
int elements = (int)arrayBounds.cElements;
int *intValues = (int*)pSafeArray->pvData;
// convert the ints to CapePhaseStatuses
CapePhaseStatus retrievedStatuses[10];
for (int i = lowerBound; i < lowerBound+elements; i++)
{
int temp = intValues[i];
retrievedStatuses[i] = (CapePhaseStatus)temp;
}
pSafeArray->cLocks--;
// Now do as a VARIANT...
VARIANT *pVariant;
tester->CreateVariantArray(&pVariant);
// Get an array of ints from the SafeArray
arrayBounds = pVariant->parray->rgsabound[0];
lowerBound = (int)arrayBounds.lLbound;
elements = (int)arrayBounds.cElements;
intValues = (int*)pVariant->parray->pvData;
CapePhaseStatus retrievedStatuses2[10];
for (int i = lowerBound; i < lowerBound+elements; i++)
{
int temp = intValues[i];
retrievedStatuses2[i] = (CapePhaseStatus)temp;
}
pVariant->parray->cLocks--;
return 0;
}
When I run this, both retrievedStatuses and retrievedStatuses2 contain an array of CapePhaseStatus enum data.