一。定义
1. odl文件:
importlib(STDOLE_TLB);
importlib(STDTYPE_TLB);
typedef[uuid(6EDD5246-DEB1-433f-8AFE-535C851A9C1E),
helpstring("abc struct")
]
struct sabc
{
long la;
long lb;
long lc;
};
[ uuid(2F8CA417-FFBD-446E-9B6A-44B82D9558E0),
helpstring("Dispatch interface for Cabc Control"), hidden ]
dispinterface _DCabc
{
properties:
2. .h文件:
struct sabc
{
long la;
long lb;
long lc;
};
afx_msg VARIANT Getabc();
afx_msg void Setabc(const VARIANT & newValue);
enum {
//{{AFX_DISP_ID(CabcCtrl)
dispidabc = 1L,
//}}AFX_DISP_ID
};
sabc m_sabcB[2];
3. .cpp文件:
BEGIN_DISPATCH_MAP(CabcCtrl, COleControl)
//{{AFX_DISPATCH_MAP(CabcCtrl)
DISP_PROPERTY_EX(CabcCtrl, "abc", Getabc, Setabc, VT_VARIANT)
//}}AFX_DISPATCH_MAP
END_DISPATCH_MAP()
VARIANT CabcCtrl::Getabc()
{
VARIANT vaResult;
VariantInit(&vaResult);
// TODO: Add your property handler here
ITypeLib* pTypeLib = NULL;
ITypeInfo* pTypeInfo = NULL;
IRecordInfo* pRecordInfo = NULL;
HRESULT hr;
hr = LoadRegTypeLib(LIBID_TESTLib, 1, 0, LANG_NEUTRAL, &pTypeLib);
pTypeLib->GetTypeInfoOfGuid(GUID_MyStruct, &pTypeInfo);
hr = GetRecordInfoFromTypeInfo(pTypeInfo, &pRecordInfo);
sPUB *tt;
SAFEARRAY *psa;
SAFEARRAYBOUND rgsabound[1];
rgsabound[0].cElements = 2;
rgsabound[0].lLbound = 0;
psa = ::SafeArrayCreateEx(VT_RECORD,1,rgsabound,(void*)pRecordInfo);
hr = ::SafeArrayAccessData(psa,(void**)&tt);
tt[0].la = 777;
tt[0].lb = 888;
tt[0].lc = 999;
tt[1].la = 000;
tt[1].lb = 123;
tt[1].lc = 789;
vaResult.vt = VT_ARRAY | VT_RECORD;
vaResult.parray = psa;
::SafeArrayUnaccessData(psa);
pTypeLib->Release();
pTypeInfo->Release();
pRecordInfo->Release();
return vaResult;
}
void CabcCtrl::Setabc(const VARIANT & newValue)
{
// TODO: Add your property handler here
COleVariant ar(newValue);
if(ar.vt == (VT_ARRAY | VT_RECORD))
{
for(long index = 0;index < 2;index++)
{
::SafeArrayGetElement(ar.parray,&index,&(m_sabc[index]));
}
}
SetModifiedFlag();
}
二。使用
1. 生成.h文件:
VARIANT Getabc();
void Setabc(const VARIANT&);
2. 生成.cpp文件:
VARIANT Cabc::Getabc()
{
VARIANT result;
GetProperty(0x1, VT_VARIANT, (void*)&result);
return result;
}
void Cabc::Setabc(const VARIANT& propVal)
{
SetProperty(0x1, VT_VARIANT, &propVal);
}
3. 客户使用:
SET:
ITypeLib* pTypeLib = NULL;
ITypeInfo* pTypeInfo = NULL;
IRecordInfo* pRecordInfo = NULL;
HRESULT hr;
hr = LoadRegTypeLib(LIBID_TESTLib, 1, 0, LANG_NEUTRAL, &pTypeLib);
pTypeLib->GetTypeInfoOfGuid(GUID_MyStruct, &pTypeInfo);
hr = GetRecordInfoFromTypeInfo(pTypeInfo, &pRecordInfo);
sabc *tt;
VARIANT varChunk;
SAFEARRAY *psa;
SAFEARRAYBOUND rgsabound[1];
rgsabound[0].cElements = 2;
rgsabound[0].lLbound = 0;
VariantInit(&varChunk);
psa = ::SafeArrayCreateEx(VT_RECORD,1,rgsabound,(void*)pRecordInfo);
if (psa == NULL)
{
return FALSE;
}
hr = ::SafeArrayAccessData(psa,(void**)&tt);
tt[0].lPatID = 111;
tt[0].lBed = 222;
tt[0].lUnitID = 333;
tt[1].lPatID = 444;
tt[1].lBed = 555;
tt[1].lUnitID = 666;
varChunk.vt = VT_ARRAY | VT_RECORD;
varChunk.parray = psa;
m_ptrCabc->Setabc(varChunk);
::SafeArrayUnaccessData(psa);
pTypeLib->Release();
pTypeInfo->Release();
pRecordInfo->Release();
VariantClear(&varChunk);
GET:
VARIANT newValue;
sabc aa[2];
VariantInit(&newValue);
newValue = m_ptrCabc->Getabc();
COleVariant ar(newValue);
if(ar.vt == (VT_ARRAY | VT_RECORD))
{
for(long index = 0;index < 2;index++)
{
hr = ::SafeArrayGetElement(ar.parray,&index,&(aa[index]));
}
}
VariantClear(&newValue);
PS:
其中LIBID_TESTLib和GUID_MyStruct自定义(程序相关):
const GUID GUID_MyStruct = {0x6EDD5246,0xDEB1,0x433f,{0x8A,0xFE,0x53,0x5C,0x85,0x1A,0x9C,0x1E}};
const GUID LIBID_TESTLib = {0x79CF0CFD, 0xF523, 0x4645, { 0x91, 0xa1, 0x76, 0xca, 0xfa, 0xb1, 0x04, 0x67 }};