1、
//DEMO_COM_01.h
#ifndef DEMO_COM_01_H
#define DEMO_COM_01_H
#include
2、 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
3、 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,
4、 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::co
5、ut << "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///////////////////////////////////////////////
6、///// #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
7、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(
8、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
9、 (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)
10、 {
delete this;
return (0);
}
else
{
return (ref_count);
}
}
/////////////////////main.cpp//////////////////////////////////////////////////////////////////
#include
11、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(punknow
12、n == 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) { p
13、iy->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






