c++ - 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 IsConnected")] HRESULT IsConnected([out] BOOL* pVal);
};
interface IClientControl : 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
.
解决方案
通过使用选项--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
下面,它IUnknown
专门IDispatch
处理。Q_DECLARE_METATYPE
在这里没用,因为所需接口确实是派生自 的类型(例如IServerConnection
)IDispatch
,而不是派生自 的生成版本QAxObject
。
推荐阅读
- python - 如何将应用程序文件导入不同目录Python中的测试文件
- laravel - @cypress/vue 与 Laravel-Mix
- mysql - 如何恢复/恢复损坏的 RDS 实例?
- mysql - 在 MySQL 中存储 MatDatepicker 时间戳
- amazon-web-services - 当从 SQS 队列中删除一条消息时,配置了较高的可见性超时值,如果我删除该消息,它会立即被删除吗?
- java - 当我用jsoup解析时,标签的内容消失了
- python - 使用python打印硬拷贝时选择打印机
- reactjs - 用于实现模态对话框的动态导入 next.js
- python - 如何在 for 循环中将用户输入保存为整数和字符串
- java - Spring Boot ModelMapper - 无会话