OCX通过IDispatch传递结构体的4种方法

简介: 1)Pass each structure member as an individual method parameter.2)Serialize the structure to an opaque array of bytes.
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.

1 就是依次返回每个结构体成员
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 ;
Variant Init(&vaResult);

rgbounds.1Lbound = 0;//下标从0开始
rgbounds.cElements = 1;//元素个数为1个结构体
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 );
}
相关文章
|
24天前
|
算法 数据处理 C++
Franca IDL与CommonAPI C++ D-Bus实战教程:方法、信号和属性传递详解
Franca IDL与CommonAPI C++ D-Bus实战教程:方法、信号和属性传递详解
88 0
|
6月前
|
JSON 数据格式
调用 sap.ui.base.ManagedObject 的构造函数时,如何传递绑定路径进去
调用 sap.ui.base.ManagedObject 的构造函数时,如何传递绑定路径进去
25 0
|
3月前
|
C++
Qt定义属性类信息报错‘Qstring‘ was not declared in this scope; did you mean ‘xxx‘?并且还有有一堆报错,问题还出现在moc文件
Qt定义属性类信息报错‘Qstring‘ was not declared in this scope; did you mean ‘xxx‘?并且还有有一堆报错,问题还出现在moc文件
26 0
|
6月前
|
XML 测试技术 数据格式
SAP UI5 Page 控件的构造函数参数讲解
SAP UI5 Page 控件的构造函数参数讲解
32 0
|
并行计算 PyTorch 算法框架/工具
如何将自己定义的函数,也传给cuda进行处理?
要将自己定义的函数传递到CUDA进行处理,需要使用PyTorch提供的CUDA扩展功能。具体来说,可以使用torch.cuda.jit模块中的@torch.jit.script装饰器将Python函数转换为Torch脚本,并使用.cuda()方法将其移动到GPU上。
579 0
【QT】解决QT两个类之间传递变量失败,或者extern变量引用无效。
【QT】解决QT两个类之间传递变量失败,或者extern变量引用无效。
Qt信号槽使用结构体作为参数:Q_DECLARE_METATYPE和qRegisterMetaType的作用
Qt信号槽使用结构体作为参数:Q_DECLARE_METATYPE和qRegisterMetaType的作用
1348 0
|
C# C++
C#调用C/C++ DLL 参数传递和回调函数的总结
原文:C#调用C/C++ DLL 参数传递和回调函数的总结 Int型传入: Dll端: extern "C" __declspec(dllexport) int Add(int a, int b) ...
5472 0
|
安全 C# C++
C#调用带结构体指针的C Dll的方法
原文:C#调用带结构体指针的C Dll的方法 在C#中调用C(C++)类的DLL的时候,有时候C的接口函数包含很多参数,而且有的时候这些参数有可能是个结构体,而且有可能是结构体指针,那么在C#到底该如何安全的调用这样的DLL接口函数呢?本文将详细介绍如何调用各种参数的方法。
3415 0