A Review of the COM SAFEARRAY Data Type
为了在COM函数间传递Array参数,IDL定义了SAFEARRAY类型:
interface IMyInterface : IUnknown { HRESULT GetArray([out,retval] SAFEARRAY(VARIANT_BOOL)* myArray); };
typedef struct tagSAFEARRAY { USHORT cDims; USHORT fFeatures; ULONG cbElements; ULONG cLocks; PVOID pvData; SAFEARRAYBOUND rgsabound[ 1 ]; } SAFEARRAY;
其中
cDims数组的维数
cbElements每一个元素的size
pvData指向真实数据
SAFEARRAYBOUND用于规定上下限:rgsabound[0]用于规定最左维,rgsabound[cDims-1]规定最右维
typedef struct tagSAFEARRAYBOUND { ULONG cElements; LONG lLbound; } SAFEARRAYBOUND;
创建Array例子:
SAFEARRAYBOUND rgsabound[2]; rgsabound[0].cElements = 3; rgsabound[0].lLbound = 0; rgsabound[1].cElements = 4; rgsabound[1].lLbound = 0;
SAFEARRAY* psa = ::SafeArrayCreate(VT_R8, 2, rgsabound);
读取Array中数据例子:
long rgIndices[] = { 2, 1 }; double lElem; ::SafeArrayGetElement(psa, rgIndices, (void*)&lElem);
注意上面例子取得[1][2]的数据,
销毁Array应该使用:
::SafeArrayDestroy(psa);