一个COM演示代码


#include "stdafx.h"

#include <iostream>
using namespace std;
#include <objbase.h>

void trace(const char* msg)
{
cout << msg << endl;
}

// interface definition
interface IX : IUnknown
{
virtual void __stdcall Fx() = 0;
};

interface IY : IUnknown
{
virtual void __stdcall Fy() = 0;
};

interface IZ : IUnknown
{
virtual void __stdcall Fz() = 0;
};

// Forward references for GUIDs
extern const IID IID_IX;
extern const IID IID_IY;
extern const IID IID_IZ;

//
// implement interface IX,IY (A COM component)
//
class CA : public IX, public IY
{
// IUnknown implementation
virtual HRESULT __stdcall QueryInterface(const IID& iid, void** ppv);
virtual ULONG __stdcall AddRef() { return 0;}
virtual ULONG __stdcall Release() { return 0;}

// Interface IX implementation
virtual void __stdcall Fx() { cout << "This is Fx func" << endl;}

// Interface IY implementation
virtual void __stdcall Fy() { cout << "This is Fy func" << endl;}
};

HRESULT __stdcall CA::QueryInterface(const IID& iid, void** ppv)
{
if (iid == IID_IUnknown)
{
trace("QueryInterface: Return pointer to IUnknown.");
*ppv = static_cast<IX*>(this);
}
else if (iid == IID_IX)
{
trace("QueryInterface: Return pointer to IX.");
*ppv = static_cast<IX*>(this);
}
else if (iid == IID_IY)
{
trace("QueryInterface: Return pointer to IY.");
*ppv = static_cast<IY*>(this);
}
else
{
trace("QueryInterface: Interface not supported.");
*ppv = NULL;
return E_NOINTERFACE;
}
reinterpret_cast<IUnknown*>(*ppv)->AddRef(); // add reference num

return S_OK;
}

//
// create class CA, return a pointer to IUnknown
//
IUnknown* CreateInstance()
{
IUnknown* pI = static_cast<IX*>(new CA);
pI->AddRef();
return pI ;
}

//
// IID of interfaces
//
// {32bb8320-b41b-11cf-a6bb-0080c7b2d682}
static const IID IID_IX =
{0x32bb8320, 0xb41b, 0x11cf,
{0xa6, 0xbb, 0x0, 0x80, 0xc7, 0xb2, 0xd6, 0x82}};

// {32bb8321-b41b-11cf-a6bb-0080c7b2d682}
static const IID IID_IY =
{0x32bb8321, 0xb41b, 0x11cf,
{0xa6, 0xbb, 0x0, 0x80, 0xc7, 0xb2, 0xd6, 0x82}};

// {32bb8322-b41b-11cf-a6bb-0080c7b2d682}
static const IID IID_IZ =
{0x32bb8322, 0xb41b, 0x11cf,
{0xa6, 0xbb, 0x0, 0x80, 0xc7, 0xb2, 0xd6, 0x82}};

//
// main functionclient)
//
int _tmain(int argc, _TCHAR* argv[])
{
HRESULT hr;

trace("Client:get IUnknown pointer.");
IUnknown* pIUnknown = CreateInstance();

trace("Client:get interface IX.");
IX* pIX = NULL;
hr = pIUnknown->QueryInterface(IID_IX, (void**)&pIX);
if (SUCCEEDED(hr))
{
trace("Client: Succeeded getting IX.");
pIX->Fx(); // using IX.
}

trace("Client:get interface IY.");
IY* pIY = NULL;
hr = pIUnknown->QueryInterface(IID_IY, (void**)&pIY);
if (SUCCEEDED(hr))
{
trace("Client: Succeeded getting IY.");
pIY->Fy(); // using IY.
}

trace("Client:whether support interface IZ.");
IZ* pIZ = NULL;
hr = pIUnknown->QueryInterface(IID_IZ, (void**)&pIZ);
if (SUCCEEDED(hr))
{
trace("Client: Succeeded getting IZ.");
pIZ->Fz();
}
else
{
trace("Client: Failed getting IZ,can't support IZ.");
}

trace("Client:using interface IX to query interface IY.");
IY* pIYfromIX = NULL;
hr = pIX->QueryInterface(IID_IY, (void**)&pIYfromIX);
if (SUCCEEDED(hr))
{
trace("Client: Succeeded getting IY.");
pIYfromIX->Fy();
}

trace("Client:using interface IY query interface IUnknown.");
IUnknown* pIUnknownFromIY = NULL;
hr = pIY->QueryInterface(IID_IUnknown, (void**)&pIUnknownFromIY);
if (SUCCEEDED(hr))
{
cout << "pIUnknownFromIY equal to IUnknown ?";
if (pIUnknownFromIY == pIUnknown)
{
cout << "Yes, pIUnknownFromIY == pIUnknown." << endl;
}
else
{
cout << "No, pIUnknownFromIY != pIUnknown." << endl;
}
}

// Delete the component.
delete pIUnknown;

return 0;
}


[img]http://dl.iteye.com/upload/attachment/0065/2410/292f953e-93ea-3514-8baa-50eb3b76c614.png[/img]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值