本文介绍了如何在dynamicCall中使用VARIANT *?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用COM对象,并且参数类型VARIANT *存在问题.我可以很好地使用COM对象的功能,除非它们具有这种类型的参数.

generateDocumentation生成的文档是:

QVariantList参数= ...

对象-> dynamicCall("GetRanges(int,int,int& ;, QVariant&)",params);

根据COM对象随附的文档,参数应为LONG,LONG,LONG *和VARIANT *类型,并且精确地说,VARIANT *是指向包含BSTR数组的VARIANT的指针./p>

我通常应该能够检索第三个和第四个参数(类型为LONG *和VARIANT *),并且该函数不使用它们的值.

这是我的代码(a和b之前都是int初始化的):

QStringList sl;
QVariantList params;
int i = -1;
params << QVariant (a);
params << QVariant (b);
params << QVariant (i);
params << QVariant (sl);

comobject->dynamicCall("GetRanges(int,int,int&,QVariant&)",params);
sl = params[3].toStringList();
i = param[2].toInt();

现在有了该代码,我得到的只是一个错误QAxBase:调用IDispatch成员GetRanges时出错:未知错误,这不是很有帮助.

我尝试更改某些内容,并通过使用以下代码设法进行了改进(

):

QStringList sl;
QVariant v = qVariantFromValue(sl);
QVariantList params;
int i = -1;
params << QVariant (a);
params << QVariant (b);
params << QVariant (i);
params << qVariantFromValue((void*)&v);

comobject->dynamicCall("GetRanges(int,int,int&,QVariant&)",params);
sl = params[3].toStringList();
i = param[2].toInt();

它消除了错误,并且i的值最后是正确的,但是sl仍然为空.而且我知道不应该这样,因为我在C#中有一个示例演示,可以正常工作.

所以,如果有人对如何使其工作有想法...

否则,我环顾四周,发现还可以直接查询该接口并直接使用它,但是我不太了解,我不确定它是否可以解决我的问题.

我在Windows7 64位平台上,并且我使用msvc2012作为编译器.我现在正在使用Qt 5.1.0,但在5.0.2中也无法使用.

解决方案

我想您真的无法通过dynamicCall做到这一点.我终于找到了解决方法.这比我想象的要容易.安装Qt时会附带一个名为dumpcpp的工具.它的完整路径对我来说是C:\ Qt \ Qt5.1.0x86 \ 5.1.0 \ msvc2012 \ bin \ dumpcpp.exe(显然取决于设置).您只需将bin文件夹添加到您的路径即可使其更易于使用.然后进入项目文件夹并执行以下命令:

它将创建一个头文件,您可以将其包含在尝试使用COM对象的文件中.

就我而言,在此文件中,命名空间(MeasurementLib)中有两个类(IClassMeasurement和ClassMeasurement).再次,名字不是真实的.

在您的初始项目文件中,您可以像这样调用所需的函数:

MeasurementLib::ClassMeasurement test; //Do not use IClassMeasurement, you only get write access violations
QVariant rangesVar;
int p1 = 0;
int p2 = 0;
int p3 = 0;
test.getRanges(p1,p2,p3,ranges);
QStringList ranges = ranges.toStringList();

希望它对某人有帮助!

I'm trying to use a COM object and i'm having problem with the parameter type VARIANT*. I can use the functions of the COM object just fine, except when they have a parameter of this type.

The doc generated by generateDocumentation is :

QVariantList params = ...

object->dynamicCall("GetRanges(int,int,int&, QVariant&)", params);

According to the doc provided with the COM object, the parameters should be of type LONG, LONG, LONG* and VARIANT*, and it is precised that the VARIANT* is a pointer to a VARIANT containing an array of BSTR.

I should normally be able to retrieve the third and fourth parameter (of type LONG* and VARIANT*), and their values are not used by the function.

Here is my code (a and b are int previously initialized):

QStringList sl;
QVariantList params;
int i = -1;
params << QVariant (a);
params << QVariant (b);
params << QVariant (i);
params << QVariant (sl);

comobject->dynamicCall("GetRanges(int,int,int&,QVariant&)",params);
sl = params[3].toStringList();
i = param[2].toInt();

Now with that code, all i get is an error QAxBase: Error calling IDispatch member GetRanges: Unknown error, which is not very helpful.

I tried to change some things and I managed to progress (sort of) by using this code :

QStringList sl;
QVariant v = qVariantFromValue(sl);
QVariantList params;
int i = -1;
params << QVariant (a);
params << QVariant (b);
params << QVariant (i);
params << qVariantFromValue((void*)&v);

comobject->dynamicCall("GetRanges(int,int,int&,QVariant&)",params);
sl = params[3].toStringList();
i = param[2].toInt();

It gets rid of the error, and the value of i is correct at the end, but sl is still empty. And I know it should not be, because I have a sample demo in C# that works correctly.

So if anyone has an idea on how to make it works...

Otherwise I looked around a bit and saw that it was also possible to query the interface ans use it directly, but I didn't understand much, and I'm not sure it will solve my problems.

I'm on a Windows7 64 bits platform, and I'm using msvc2012 as compiler. I'm using Qt 5.1.0 right now, but it didn't work in the 5.0.2 either.

解决方案

I guess you really can't do it with dynamicCall.I finally found how to do it. It was easier than I'd thought. With the installation of Qt comes a tool called dumpcpp. Its full path for me was C:\Qt\Qt5.1.0x86\5.1.0\msvc2012\bin\dumpcpp.exe (obviously depends on settings). You can just add the bin folder to your path to make it easier to use.Then I went into my project folder and executed this command :

It creates a header file, you can include it in the file where you're trying to use the COM Object.

In this file in my case there was two classes (IClassMeasurement and ClassMeasurement) in a namespace (MeasurementLib). Again, the names are not the real ones.

In your initial project file, you can call the desired function like this :

MeasurementLib::ClassMeasurement test; //Do not use IClassMeasurement, you only get write access violations
QVariant rangesVar;
int p1 = 0;
int p2 = 0;
int p3 = 0;
test.getRanges(p1,p2,p3,ranges);
QStringList ranges = ranges.toStringList();

Hopes that it helps someone !

这篇关于如何在dynamicCall中使用VARIANT *?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 02:58
查看更多