描述:
如题. 简单问题.
我看了在ATL工程中如何让一个接口返回一个自定义的数据类型(比如结构).
现在我想在ActiveX工程中让一个接口返回一个结构类型.不知道如何来作.可能和ATL的类似.但我太笨了就是没有照搬过来.
请赐教.
解决方案1:
返回接口指针,使用LPDISPATCH
解决方案2: {
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传出去就可以了
因为系统提供的自动化汇集机制不支持自定义的结构,所以VB是没法用的,除非你自己提供汇集接口,这个比较麻烦的,可以看一下MSDN上的讲述
Taking Advantage of the Automation Marshaller