/*************************************************************************
Written by Seunghwan
*************************************************************************/
#include<iostream>
#include<objbase.h>
using namespace std;
//////////////////////////////////////////////////////////////////////////
interface ISimpleHelloWorld : public IUnknown
{
virtual void __stdcall PrintHello() = 0;
};
//////////////////////////////////////////////////////////////////////////
extern const IID IID_ISimpleHelloWorld;
//////////////////////////////////////////////////////////////////////////
class SimpleHelloWorld : public ISimpleHelloWorld
{
virtual HRESULT __stdcall QueryInterface(const IID & iid , void **ppv);
virtual ULONG __stdcall AddRef()
{
return 0;
};
virtual ULONG __stdcall Release()
{
return 0;
};
public:
virtual void __stdcall PrintHello()
{
cout << "Hello World" <<endl;
}
};
//////////////////////////////////////////////////////////////////////////
HRESULT __stdcall SimpleHelloWorld :: QueryInterface(const IID & iid , void ** ppv)
{
if(iid == IID_IUnknown)
{
cout<< "QueryInterface : Return pointer to IUnknown." <<endl;
*ppv = static_cast<ISimpleHelloWorld*>(this);
}
else if(iid == IID_ISimpleHelloWorld)
{
cout<< "QueryInterface : Return pointer to ISimpleHelloWorld."<<endl;
*ppv = static_cast<ISimpleHelloWorld*>(this);
}
else
{
cout<< "QueryInterface : Interface not supported."<<endl;
*ppv = NULL;
return E_NOINTERFACE;
}
static_cast<IUnknown*>(*ppv)->AddRef();
return S_OK;
}
//////////////////////////////////////////////////////////////////////////
IUnknown* CreateInstance()
{
IUnknown* pI = static_cast<ISimpleHelloWorld*>( new SimpleHelloWorld );
pI->AddRef();
return pI;
}
//////////////////////////////////////////////////////////////////////////
static const IID IID_ISimpleHelloWorld =
// {D035B70E-55A1-4869-808E-C395C4964C54}
{ 0xd035b70e, 0x55a1, 0x4869, { 0x80, 0x8e, 0xc3, 0x95, 0xc4, 0x96, 0x4c, 0x54 } };
//////////////////////////////////////////////////////////////////////////
void main()
{
HRESULT hr;
cout << "Client : Get and IUNknown pointer."<<endl;
IUnknown * pIUnknown = CreateInstance();
cout << "Client : Get interface ISimpleHelloWorld"<<endl;
ISimpleHelloWorld * pISimpleHelloWorld = NULL;
hr = pIUnknown->QueryInterface(IID_ISimpleHelloWorld , (void**)&pISimpleHelloWorld);
if(SUCCEEDED(hr))
{
cout<<"Client : Succeeded getting ISimpleHelloWorld"<<endl;
pISimpleHelloWorld->PrintHello();
}
delete pIUnknown;
}