{
Mystuct m_struct[10]; //Mystuct 自定义结构
for(int i=0;i<10;i++)
{
m_struct[i].x=(float)i;
m_struct[i].y=(float)i;
m_struct[i].z=(float)i;
m_struct[i].w='x';
}
HRESULT hr;
//create an array bound
SAFEARRAYBOUND rgsabound[1];
rgsabound[0].lLbound = 0; //The first (and only) collumn of our array starts at 0.
rgsabound[0].cElements = 10; //and has 100 elements.
//create the array
SAFEARRAY FAR* pMySafeArray;
pMySafeArray = SafeArrayCreate(VT_VARIANT, 1, rgsabound);
Mystuct *pData;
hr = SafeArrayAccessData( pMySafeArray, reinterpret_cast<PVOID*>(&pData)); //Get a pointer to the data.
//copy the bytes of data from our old array to the new one.
memcpy(pData, m_struct, 10*sizeof(Mystuct));
for(int i = 0; i < 10; i++) ASSERT(pData[i].x == m_struct[i].x);
SafeArrayUnaccessData(pMySafeArray);
//To put the SafeArray in a Variant
VARIANT myVariant;
myVariant.parray = pMySafeArray;
myVariant.vt = VT_ARRAY;
SafeArrayUnaccessData(myVariant.parray); //然后把myVariant传出去就可以了