考虑像下面这样定义的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
。