简体   繁体   English

如何在C ++中将用户定义的结构转换为变量?

[英]How to convert user-defined struct to variant in c++?

I have a struct in native c++, which should be passed to a COM object; 我在本地c ++中有一个struct ,应该将其传递给COM对象; both share same header file and hence there is no difference in struct definition between the COM object and my code. 两者共享相同的头文件,因此COM对象和我的代码之间的结构定义没有差异。 The COM method expects a pointer to struct as parameter. COM方法希望将指向struct的指针作为参数。 I am able to pass types like int, float, string, etc as parameters to COM by converting them to variant like variant_t vtArg(myIntValue) . 通过将它们转换为variant_t vtArg(myIntValue)类的variant_t vtArg(myIntValue) ,我能够将int,float,string等类型作为参数传递给COM。

How do I convert struct to variant? 如何将struct转换为variant?

The signature of the COM method is like this: COM方法的签名如下所示:

bool ComMethod(HANDLE *myHandle, MyStruct *myStruct)

This is how I construct arguments for the COM method: 这就是我为COM方法构造参数的方式:

SAFEARRAY *pMethodArgs = SafeArrayCreateVector(VT_VARIANT, 0, 2);
LONG index = 0;    
SafeArrayPutElement(pMethodArgs, &index, &myHandle);
index = 1;
SafeArrayPutElement(pMethodArgs, &index, &myStruct);

I tried multiple ways and found that it is not possible to send MyStruct as parameter. 我尝试了多种方法,发现无法将MyStruct作为参数发送。 I made the COM method return an instance of MyStruct and tried to reverse-engineer the return value in C++, but unfortunately nothing was received at C++. 我使COM方法返回MyStruct一个实例,并尝试对C ++中的返回值进行反向工程,但不幸的是,C ++没有收到任何信息。

The solution is to create a SAFEARRAY of VARIANT with individual members of the structure, or to pass the individual members as separate parameters. 解决方案是使用结构的各个成员创建SAFEARRAY of VARIANT ,或将各个成员作为单独的参数传递。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM