Qt ActiveQt Server
解決方案項目配置:如下文件須要修改 1. 項目屬性頁->項目屬性->常規->目標文件擴展名 2. 項目屬性頁->項目屬性->連接器->全部選項->輸出文件 *.dll 修改成 *.exe 文件 3. 項目屬性頁->項目屬性->常規->配置類型 生成文件 修改成 *.exe
main.cpp
源文件添加如下內容main.cpp
html
//# 靜態編譯須要 #include <QtPlugin> Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin) // End 靜態編譯須要 #include <QApplication> #include <QAxFactory> #include "ActiveQtServer.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); if (!QAxFactory::isServer()) { ActiveQtServer1 w; w.show(); return app.exec(); } return app.exec(); } //導出接口 QAXFACTORY_BEGIN("{ad90301a-849e-4e8b-9a91-0a6dc5f6461f}","{a8f21901-7ff7-4f6a-b939-789620c03d83}") //導出類 QAXCLASS(ActiveQtServer1) QAXFACTORY_END()
ActiveQtServer.h
安全
#pragma once #include <QtWidgets/QWidget> #include <ActiveQt/QAxBindable> #include "ui_ActiveQtServer.h" //# 設置內存執行編碼 UTF-8 #ifdef Q_OS_WIN #pragma execution_character_set("UTF-8") #endif //# 控件安全標記類 #include <QAxAggregated> #include <objsafe.h> #include <QUuid> class ObjectSafety : public QAxAggregated, public IObjectSafety { public: ObjectSafety(){ } QAXAGG_IUNKNOWN; long queryInterface(const QUuid &iid, void **iface) { *iface = NULL; if (iid == IID_IObjectSafety) { *iface = (IObjectSafety*)this; } else { return E_NOINTERFACE; } AddRef(); return S_OK; } HRESULT WINAPI GetInterfaceSafetyOptions(REFIID riid, DWORD *pdwSupportedOptions, DWORD *pdwEnabledOptions) { *pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA | INTERFACESAFE_FOR_UNTRUSTED_CALLER; *pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA | INTERFACESAFE_FOR_UNTRUSTED_CALLER; return S_OK; } HRESULT WINAPI SetInterfaceSafetyOptions(REFIID riid, DWORD pdwSupportedOptions, DWORD pdwEnabledOptions) { return S_OK; } }; //導出控件類 class ActiveQtServer : public QWidget, public QAxBindable { Q_OBJECT //聲明類信息 Q_CLASSINFO("ClassID", "{1D9928BD-4453-4bdd-903D-E525ED17FDE5}") Q_CLASSINFO("InterfaceID", "{99F6860E-2C5A-42ec-87F2-43396F4BE389}") Q_CLASSINFO("EventsID", "{0A3E9F27-E4F1-45bb-9E47-63099BCCD0E3}") public: ActiveQtServer(QWidget *parent = Q_NULLPTR); public Q_SLOTS: QString Version(); private: Ui::ActiveQtServerClass ui; //重寫 QAxBindable 類 createAggregate 方法 QAxAggregated* createAggregate(); };
ActiveQtServer.cpp
app
#include "ActiveQtServer.h" #include <ActiveQt/QAxFactory> ActiveQtServer::ActiveQtServer(QWidget *parent) : QWidget(parent) { ui.setupUi(this); } QString ActiveQtServer::Version() { return QString("Hello Word!"); } QAxAggregated * ActiveQtServer::createAggregate() { return new ObjectSafety; }
ActiveQtServer.def
ui
; Declares the module parameters. EXPORTS DllCanUnloadNow PRIVATE DllGetClassObject PRIVATE DllRegisterServer PRIVATE DllUnregisterServer PRIVATE DumpIDL PRIVATE
<!DOCTYPE html> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <meta http-equiv="X-UA-Compatible" content="IE=8" > <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" > <html lang="en"> <head> <meta charset="UTF-8"> <title>ActiveQtServer</title> </head> <body> <div> <div> <object id="ActiveQtServer" width="0" height="0" classid="CLSID:1D9928BD-4453-4bdd-903D-E525ED17FDE5"></object> </div> <input type="button" onclick="alert(ActiveQtServer.Version())" value="獲取版本信息"/> </div> </body> <script> </script> </html>
Qt 和 COM 類型映射關係this