1)Pass each structure member as an individual method parameter.
2)Serialize the structure to an opaque array of bytes.
3)Serialize the structure to an array of VARIANTs, one array element per structure member.
4)Implement a dispatch/dual wrapper object that contains the structure and pass an interface to the wrapper object as a parameter.
VARIANT CMyIeDlg::SetStruct()
{
HRESULT hr;
SAFEARRAY* pSa = NULL;
SAFEARRAYBOUND rgbounds;
pSa = SafeArrayCreate(VT_VARIANT, 1, &rgbounds);
void CMyIeDlg::GetStruct()
{
HRESULT hr;
SAFEARRAY* psaStudent = NULL;
studentsInfo *pStudentStruct = NULL;
studentsInfo students;
psaStudent = SetStruct();
students = *pStudentStruct;
hr = SafeArrayUnaccessData(psaStudent);
2)Serialize the structure to an opaque array of bytes.
3)Serialize the structure to an array of VARIANTs, one array element per structure member.
4)Implement a dispatch/dual wrapper object that contains the structure and pass an interface to the wrapper object as a parameter.
1 就是依次返回每个结构体成员
2 序列化
3 序列化到VARIANT类型里面
4 多写一个对象,包装这个结构体.
2 序列化
3 序列化到VARIANT类型里面
4 多写一个对象,包装这个结构体.
variant与结构体之间的相互转化方式
用SafeArray传。下面是代码:
struct
studentsInfo
{
short grade;
CString name;
int type;
};
VARIANT CMyIeDlg::SetStruct()
{
HRESULT hr;
SAFEARRAY* pSa = NULL;
SAFEARRAYBOUND rgbounds;
VARIANT
vaResult;
VariantInit(&vaResult);
rgbounds.1Lbound = 0;//下标从0开始
rgbounds.cElements = 1;//元素个数为1个结构体
studentsInfo StudentStruct;
studentsInfo StudentStruct;
StudentStruct = stuctInfo;//结构体复制
pSa = SafeArrayCreate(VT_VARIANT, 1, &rgbounds);
pSa->fFeatures = FADF_AUTO | FADF_FIXEDSIZE;//指定在栈上分配数据,大小不可以改变
pSa->cbElements = sizeof(StudentStruct);
pSa->pvData = &StudentStruct;
vaResult.vt = VT_ARRAY | VT_UI1;
vaResult.parray = pSa;
return vaResult;
}
void CMyIeDlg::GetStruct()
{
HRESULT hr;
SAFEARRAY* psaStudent = NULL;
studentsInfo *pStudentStruct = NULL;
studentsInfo students;
psaStudent = SetStruct();
hr = SafeArrayAccessData(psaStudent, reinterpret_cast<PVOID*>(&pStudentStruct));
students = *pStudentStruct;
hr = SafeArrayUnaccessData(psaStudent);
SafeArrayDestroy(psaStudent);
}
本文介绍了几种将结构体数据传递给COM对象的方法,包括直接传递结构体成员、序列化为字节数组或VARIANT数组,以及使用派生/双重包装对象。此外,还详细展示了如何通过SafeArray实现结构体与VARIANT之间的相互转换。
600

被折叠的 条评论
为什么被折叠?



