Qt调用COM组件 笔记

左手的ㄟ右手 2022-06-03 23:22 840阅读 0赞

dynamicCall 既可以调用COM方法,也可以设置属性

QVariant QAxBase::dynamicCall ( const char * function, const QVariant & var1 = QVariant(), const QVariant & var2 = QVariant(), const QVariant & var3 = QVariant(), const QVariant & var4 = QVariant(), const QVariant & var5 = QVariant(), const QVariant & var6 = QVariant(), const QVariant & var7 = QVariant(), const QVariant & var8 = QVariant() )

Calls the COM object’s method function, passing the parameters var1, var1, var2, var3, var4, var5, var6, var7 and var8, and returns the value returned by the method, or an invalid QVariant if the method does not return a value or when the function call failed.

If function is a method of the object the string must be provided as the full prototype, for example as it would be written in a QObject::connect() call.

activeX->dynamicCall(“Navigate(const QString&)”, “qt.nokia.com”);
Alternatively a function can be called passing the parameters embedded in the string, e.g. above function can also be invoked using

activeX->dynamicCall(“Navigate(\“qt.nokia.com\“)”);
All parameters are passed as strings; it depends on the control whether they are interpreted correctly, and is slower than using the prototype with correctly typed parameters.

If function is a property the string has to be the name of the property. The property setter is called when var1 is a valid QVariant, otherwise the getter is called.

activeX->dynamicCall(“Value”, 5);
QString text = activeX->dynamicCall(“Text”).toString();
Note that it is faster to get and set properties using QObject::property() and QObject::setProperty().

dynamicCall() can also be used to call objects with a disabled metaobject wrapper, which can improve performance significantely, esp. when calling many different objects of different types during an automation process. ActiveQt will then however not validate parameters.

It is only possible to call functions through dynamicCall() that have parameters or return values of datatypes supported by QVariant. See the QAxBase class documentation for a list of supported and unsupported datatypes. If you want to call functions that have unsupported datatypes in the parameter list, use queryInterface() to retrieve the appropriate COM interface, and use the function directly.

IWebBrowser2 *webBrowser = 0;
activeX->queryInterface(IID_IWebBrowser2, (void **)&webBrowser);
if (webBrowser) {
webBrowser->Navigate2(pvarURL);
webBrowser->Release();
}
This is also more efficient.

QVariant QAxBase::dynamicCall ( const char * function, QList & vars )
This is an overloaded function.

Calls the COM object’s method function, passing the parameters in vars, and returns the value returned by the method. If the method does not return a value or when the function call failed this function returns an invalid QVariant object.

The QVariant objects in vars are updated when the method has out-parameters.

void QAxBase::exception ( int code, const QString & source, const QString & desc, const QString & help ) [signal]
This signal is emitted when the COM object throws an exception while called using the OLE automation interface IDispatch. code, source, desc and help provide information about the exception as provided by the COM server and can be used to provide useful feedback to the end user. help includes the help file, and the help context ID in brackets, e.g. “filename [id]“.

发表评论

表情:
评论列表 (有 0 条评论,840人围观)

还没有评论,来说两句吧...

相关阅读

    相关 PB 调用.NET COM

      今天需要作一下pb引用.net的外接程序类。反复测试都没有成功。看到这篇文章解决了我的问题。在此感谢。综合我的碰壁经验。需要注意以下两点。 1..net项目需要勾选生成

    相关 Com相关学习

    一、C++和C\中COM组件的调用和使用问题       前一阵在工作中做项目的时候,遇到了COM组件的调用和使用问题,当时研究和好一阵,才把中间的环节打通,现在写出