ActiveXWrapper.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #pragma once
  2. #include "CommWrapperBase.h"
  3. #include "Utils.h"
  4. #include <windows.h>
  5. #include <comdef.h>
  6. #include <string>
  7. #include <vector>
  8. #include <functional>
  9. #include <map>
  10. #include <mutex>
  11. #include <thread>
  12. // ActiveX控件的CLSID和接口IID定义
  13. static const CLSID CLSID_CommModifyKit = {0x97531C46, 0x4E70, 0x414F, {0x9A, 0x8F, 0x75, 0x8D, 0x38, 0xD5, 0x74, 0xC4}};
  14. static const IID IID_ICommModifyKit = {0xAE3785CB, 0x0862, 0x4FD0, {0xB6, 0x24, 0x1E, 0x10, 0x38, 0x2D, 0x21, 0x7A}};
  15. static const IID IID_ICommModifyKitEvents = {0x18A98A73, 0xE96C, 0x4268, {0x88, 0x1E, 0x1E, 0x82, 0xDE, 0x50, 0xB2, 0x10}};
  16. static const IID IID_TypeLib_CommModifyKit = {0x41159543, 0xEA6B, 0x4C44, {0x84, 0xF2, 0xD5, 0xF9, 0xFC, 0xEE, 0x35, 0x41}};
  17. // 使用基类定义的事件类型、数据结构和回调函数类型
  18. // enum class ActiveXEventType -> CommEventType
  19. // struct ActiveXEventData -> CommEventData
  20. // using ActiveXEventCallback -> CommEventCallback
  21. // COM事件接收器类
  22. class ActiveXEventSink : public IDispatch
  23. {
  24. private:
  25. LONG ref_count_;
  26. CommEventCallback callback_;
  27. public:
  28. ActiveXEventSink(CommEventCallback callback);
  29. virtual ~ActiveXEventSink();
  30. // IUnknown methods
  31. STDMETHOD(QueryInterface)(REFIID riid, void **ppvObject);
  32. STDMETHOD_(ULONG, AddRef)();
  33. STDMETHOD_(ULONG, Release)();
  34. // IDispatch methods
  35. STDMETHOD(GetTypeInfoCount)(UINT *pctinfo);
  36. STDMETHOD(GetTypeInfo)(UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo);
  37. STDMETHOD(GetIDsOfNames)(REFIID riid, LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId);
  38. STDMETHOD(Invoke)(DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr);
  39. };
  40. class ActiveXWrapper : public CommWrapperBase
  41. {
  42. private:
  43. IDispatch *activex_control_;
  44. IConnectionPoint *connection_point_;
  45. DWORD connection_cookie_;
  46. ActiveXEventSink *event_sink_;
  47. bool event_connected_;
  48. // COM接口方法ID
  49. DISPID init_monitor_id_;
  50. DISPID monitor_port_id_;
  51. DISPID stop_port_id_;
  52. DISPID read_data_id_;
  53. DISPID write_data_id_;
  54. DISPID free_monitor_id_;
  55. DISPID get_last_error_id_;
  56. // 初始化方法ID
  57. bool InitializeMethodIds();
  58. // 连接事件
  59. bool ConnectEvents();
  60. // 断开事件
  61. void DisconnectEvents();
  62. // 调用COM方法的辅助函数
  63. HRESULT InvokeMethod(DISPID dispid, VARIANT *params, int param_count, VARIANT *result = nullptr);
  64. // 创建SAFEARRAY的辅助函数
  65. SAFEARRAY* CreateSafeArrayFromBytes(const std::vector<BYTE>& bytes);
  66. public:
  67. ActiveXWrapper();
  68. ~ActiveXWrapper();
  69. // 初始化ActiveX控件
  70. bool Initialize();
  71. // 释放资源
  72. void Cleanup();
  73. // 初始化监控
  74. bool InitMonitor(const std::string &key = "C09976511B62F0ADB759D87E6906D865");
  75. // 监控端口
  76. bool MonitorPort(int port);
  77. // 停止监控端口
  78. bool StopPort(int port);
  79. // 读数据
  80. bool ReadData(int port, const std::string &hex_data);
  81. // 写数据
  82. bool WriteData(int port, const std::string &hex_data);
  83. // 释放监控
  84. bool FreeMonitor();
  85. // 获取最后错误
  86. int GetLastError();
  87. // 设置事件回调
  88. void SetEventCallback(CommEventCallback callback) override;
  89. // 获取错误信息
  90. std::string GetErrorMessage(int error_code) override;
  91. // 检查端口是否正在监控
  92. bool IsPortMonitored(int port) override;
  93. // 友元类,允许事件接收器访问私有方法
  94. friend class ActiveXEventSink;
  95. };