收藏 分销(赏)

Qt下实现COM.doc

上传人:xrp****65 文档编号:7654943 上传时间:2025-01-11 格式:DOC 页数:5 大小:24.50KB 下载积分:10 金币
下载 相关 举报
Qt下实现COM.doc_第1页
第1页 / 共5页
Qt下实现COM.doc_第2页
第2页 / 共5页


点击查看更多>>
资源描述
//DEMO_COM_01.h #ifndef DEMO_COM_01_H #define DEMO_COM_01_H #include <stdio.h> #include <malloc.h> #include <iostream> #include <objbase.h> using namespace std; // {49B57171-DCD3-436e-9D07-CDD3F3630246} static const IID IID_IX = { 0x49b57171, 0xdcd3, 0x436e, { 0x9d, 0x7, 0xcd, 0xd3, 0xf3, 0x63, 0x2, 0x46 } }; // {2B16C197-D2F1-4bab-9226-92928FF35E8A} static const IID IID_IY = { 0x2b16c197, 0xd2f1, 0x4bab, { 0x92, 0x26, 0x92, 0x92, 0x8f, 0xf3, 0x5e, 0x8a } }; // {BF42DDD8-032A-414e-8D9C-EE1EDC1CE863} static const IID IID_IZ = { 0xbf42ddd8, 0x32a, 0x414e, { 0x8d, 0x9c, 0xee, 0x1e, 0xdc, 0x1c, 0xe8, 0x63 } }; //define the Ix interface interface IX:public IUnknown { virtual void __stdcall fx(void) = 0; }; interface IY:public IUnknown { virtual void __stdcall fy(void) = 0; }; //define COM object class CCOM_OBJECT: public IX, public IY { public: CCOM_OBJECT():ref_count(0){} ~CCOM_OBJECT(){} private: virtual HRESULT __stdcall QueryInterface(const IID &iid,void **iface); virtual ULONG __stdcall AddRef(); virtual ULONG __stdcall Release(); virtual void __stdcall fx(void) {std::cout << "Function fx has been called" << std::endl;} virtual void __stdcall fy(void) {std::cout << "Function fy has been called" << std::endl;} int ref_count; }; #endif // DEMO_COM_01_H //////////////////////////DEMO_COM_01.cpp//////////////////////////////////////////////////// #include "DEMO_COM_01.h" HRESULT CCOM_OBJECT::QueryInterface(const IID &iid, void **iface) { /* this function basically casts the this pointer or the IUnknown pointer into the inferface requested,notice the comparision with the GUIDs generated and defined in the beginning of the program **/ if(iid == IID_IUnknown) { cout << "Requesting IUnkown interface" << endl; *iface = (IX *)this; } if(iid == IID_IX) { cout << "Requesting IX interface" << endl; *iface = (IX *)this; } else if(iid == IID_IY) { cout << "Requesting IX interface" << endl; *iface = (IY *)this; } else { cout << "Requesting unknown interface" << endl; *iface = NULL; return (E_NOINTERFACE); } ((IUnknown*)(*iface))->AddRef(); return (S_OK); } ULONG CCOM_OBJECT::AddRef(void) { ++ref_count; cout << "Adding a reference" << " current: " << ref_count << endl; return (ref_count); } ULONG CCOM_OBJECT::Release(void) { cout << "deleting a reference " << ref_count << endl; if (--ref_count == 0) { delete this; return (0); } else { return (ref_count); } } /////////////////////main.cpp////////////////////////////////////////////////////////////////// #include <QtCore/QCoreApplication> #include "DEMO_COM_01.h" IUnknown *CoCreateInstance(void) { IUnknown *comm_obj = (IX *)new(CCOM_OBJECT); cout << "Createing Comm onject" << endl; comm_obj->AddRef(); return (comm_obj); } int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); IUnknown *punknown = CoCreateInstance(); if(punknown == NULL) return 0; IX *pix = NULL; IY *piy = NULL; punknown->QueryInterface(IID_IX,(void **)&pix); if(pix != NULL) { pix->fx(); pix->Release(); } punknown->QueryInterface(IID_IY,(void **)&piy); if(piy != NULL) { piy->fy(); piy->Release(); } punknown->Release(); return a.exec(); } ////////////pro file ////////////////////////////////////////////////// QT -= gui TARGET = ComForQt CONFIG += console CONFIG -= app_bundle TEMPLATE = app SOURCES += main.cpp \ DEMO_COM_01.cpp DEPENDPATH += D:/Qt/2010.01/mingw/lib LIBS += D:/Qt/2010.01/mingw/lib/libuuid.a 或者 LIBS += ./LIB/UUID.LIB(vc6.0) HEADERS += DEMO_COM_01.h
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 教育专区 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服