Qt-ActiveX-如何处理自定义输出参数?

考虑像下面这样定义的idl

interface IServerConnection : IDispatch {
    [id(1),helpstring("method IsConnected")] HRESULT IsConnected([out] BOOL* pVal);
};

interface IClientControl : IDispatch {
    [id(1),helpstring("method GetServerConnection")] HRESULT GetServerConnection([out] IServerConnection** ppServerConnection);
};

dumpcpp生成类似下面的代码:

class IServerConnection : public QAxObject
{
public:
    ...
    inline void IsConnected(int& pVal);
    ...
};


class ClientControl : public QAxWidget
{
public:
    ...

    ClientControl (IClientControl *iface)
    : QAxWidget()
    {
        initializeFrom(iface);
        delete iface;
    }

    inline void GetServerConnection(IServerConnection** ppServerConnection);
    ...
};

如果我直接致电ClientControl::GetServerConnection,它将返回类型不匹配的内容。

QAxBase:调用IDispatch成员GetServerConnection时出错:参数0中的类型不匹配

如何从IServerConnection获取ClientControl


在@Remy Lebeau的建议下,idl更改为:

interface IServerConnection : IDispatch {
    [id(1),helpstring("method GetServerConnection")] HRESULT GetServerConnection([out,retval] IServerConnection** ppServerConnection);
};

然后由dumpcpp生成源:

class ClientControl : public QAxWidget
{
public:
    ...
    inline IServerConnection* GetServerConnection();
    ...
};

通过像这样呼叫GetServerConnection

ClientControl ctrl;
auto conn = ctrl.GetServerConnection();

它输出:

QVariantToVARIANT: out-parameter not supported for "subtype".
QAxBase: Error calling IDispatch member GetServerConnection: Member not found

idl后面更改源代码或实现是不可能的。 我无法将接口更改为返回类型,这是另一个问题。

它更像是一个Qt问题,而不是idl

myidgy 回答:Qt-ActiveX-如何处理自定义输出参数?

使用选项--compat

Options
  ...
  --compat        Treat all coclass parameters as IDispatch.

dumpcpp产生如下波纹源:

class ClientControl : public QAxWidget
{
public:
    ...
    inline void GetServerConnection(IDispatch** ppServerConnection);
    ...
};

IDispatch的类型可以由Qt处理。

Qt使用QVariant存储参数并将其传递到下面的VARIANT,它专门处理IUnknownIDispatchQ_DECLARE_METATYPE在这里是没有用的,因为这样的论点是所需的接口确实是从IServerConnection派生的类型(例如IDispatch),而不是从QAxObject派生的生成版本

本文链接:https://www.f2er.com/2748457.html

大家都在问