Click here to Skip to main content
15,894,460 members
Articles / Programming Languages / C++

COM from scratch - PART TWO

Rate me:
Please Sign up or sign in to vote.
4.85/5 (44 votes)
17 Apr 200414 min read 228.8K   6K   134  
An article about COM Library.
//
// Component.cpp

#include <iostream.h>
#include <objbase.h>
#include "Interface.h"

void TRACE(const char* msg) { cout << "Component:\t" << msg << endl ;}

//
// Component
//
class CComponent : public IComponent
{
	// IUnknown implementation
	virtual HRESULT __stdcall QueryInterface(const IID& iid, void** ppv) ;			
	virtual ULONG __stdcall AddRef() ;
	virtual ULONG __stdcall Release() ;

	// Interface IComponent implementation
	virtual void __stdcall Print(const char* msg);

public:
	CComponent() ;
	~CComponent();

private:
	long m_cRef ;
       
} ;

/////////////////////
CComponent::CComponent()
{
	m_cRef=0;
}
//////////////////////
CComponent::~CComponent()
{
	TRACE("Destructing the component...") ;
}


///////////////////////////////////////////////////////////////////////////
HRESULT __stdcall CComponent::QueryInterface(const IID& iid, void** ppv)
{ 	
	if (iid == IID_IUnknown)
	{
		TRACE("Returning pointer to IUnknown...") ;
		*ppv = static_cast<IComponent*>(this) ;
	} 
	else if (iid == IID_IComponent)
	{
		TRACE("Returning pointer to IComponent interface...") ;
		*ppv = static_cast<IComponent*>(this) ;
	}
	else
	{  	   
		TRACE("Interface is not supported!.") ;
		*ppv = NULL ;
		return E_NOINTERFACE ;
	}

	//The reinterpret_cast operator allows any pointer to be converted into 
	//any other pointer type.
	reinterpret_cast<IUnknown*>(*ppv)->AddRef() ; 
	return S_OK ;
}

//////////////////////////////////////
ULONG __stdcall CComponent::AddRef()
{
	return InterlockedIncrement(&m_cRef) ;
}

//////////////////////////////////////
ULONG __stdcall CComponent::Release() 
{
	if (InterlockedDecrement(&m_cRef) == 0)
	{
		delete this ;
		return 0 ;
	}
	return m_cRef ;
}


//-------------
//Print method
//------------
//////////////////////////////////////////////////
void __stdcall CComponent:: Print(const char* msg)
{
	cout<<"-----------------------"<<endl;
	cout<<msg<<endl;
    cout<<"-----------------------"<<endl;
}

//
// Creation function
//
extern "C" IUnknown* CreateInstance()
{
	IUnknown* pIUnknown = static_cast<IComponent*>(new CComponent) ;
	pIUnknown->AddRef() ;
	return pIUnknown ;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer
Denmark Denmark
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions