资源描述
//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
展开阅读全文