Click here to Skip to main content
15,888,286 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 227K   6K   134  
An article about COM Library.
// Component.cpp : Defines the initialization routines for the DLL.
//

#include "stdafx.h"
#include <afxdllx.h>
#include "interface.h"
#include <objbase.h>
#include "iostream.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//
// Component.cpp

//
// Component
//
class CComponent : public IComponent
{
public:
	// 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);

	CComponent() ;
	~CComponent();

private:
	long m_cRef ;
       
} ;

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


//////////////////////////////////////////////////////////////////////////
HRESULT __stdcall CComponent::QueryInterface(const IID& iid, void** ppv)
{ 	
	if (iid == IID_IUnknown)
	{
		*ppv = static_cast<IComponent*>(this) ;
	} 
	else if (iid == IID_IComponent)
	{
	*ppv = static_cast<IComponent*>(this) ;
	}
	else
	{  	   
	
		*ppv = NULL ;
		return E_NOINTERFACE ;
	}
	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;
}





/* is omitted as the class factory does it
// Creation function 
//
extern "C" IUnknown* CreateInstance()
{
	IUnknown* pIUnknown = static_cast<IComponent*>(new CComponent) ;
	pIUnknown->AddRef() ;
	return pIUnknown ;
}
*/


///////////////////////////////////////////////////////////
//
// Class factory
//
class CFactory : public IClassFactory
{
public:
	// IUnknown
	virtual HRESULT __stdcall QueryInterface(const IID& iid,void** ppv) ;
	virtual ULONG   __stdcall AddRef() ;
	virtual ULONG   __stdcall Release() ;
	
	// IClassFactory
	virtual HRESULT __stdcall CreateInstance(IUnknown* pUnkOuter,
	                                         const IID& iid,
	                                         void** ppv) ;
	virtual HRESULT __stdcall LockServer(BOOL bLock) ; 

	// Constructor
	CFactory() : m_cRef(1) {}
   // Destructor
	~CFactory() {}

private:
	long m_cRef ;
} ;





//
// Class factory IUnknown implementation
//
HRESULT __stdcall CFactory::QueryInterface(const IID& iid,LPVOID* ppv)
{ 	
	if ((iid == IID_IUnknown) || (iid == IID_IClassFactory))
		*ppv = static_cast<IClassFactory*>(this) ; 
	else
	{
		*ppv = NULL ;
		return E_NOINTERFACE ;
	}
	reinterpret_cast<IUnknown*>(*ppv)->AddRef() ;
	return S_OK ;
}


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



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


//
// IClassFactory implementation
///////////////////////////////////////////////////////////////
HRESULT __stdcall CFactory::CreateInstance(IUnknown* pUnkOuter, const IID& iid,void** ppv) 
                                          
                                           
{
	
	HRESULT hr;
	if (pUnkOuter != NULL)
	{
		return CLASS_E_NOAGGREGATION ;
	}
	CComponent* pComponent = new CComponent ;
	if (pComponent == NULL)
	{
		
		return E_OUTOFMEMORY ;
	}
	
	// Get the requested interface.
    hr = pComponent->QueryInterface(iid,(void**) ppv) ;

  
if(FAILED(hr))
    pComponent->Release() ;

	return hr ;   
}
//-----------------------------------------------------------------------//
// LockServer
// Called by the client of a class object to keep a server open in memory,
// allowing instances to be created more quickly.
//-----------------------------------------------------------------------//
HRESULT __stdcall CFactory::LockServer(BOOL bLock) 
{
	return S_OK ;
}
///////////////////////////////////////////////////////////
//
// DLL module information
//

 BOOL APIENTRY DllMain(HINSTANCE InsModule, 
                      DWORD dwReason, 
                      void* lpReserved)
{

	return TRUE;
}
/////////////////////////////////////////////////////////////////////////		
STDAPI DllGetClassObject(const CLSID& clsid, const IID& iid, void** ppv)
				
{ 		
 if (clsid != CLSID_Component)
 return CLASS_E_CLASSNOTAVAILABLE;
 // Create class factory.
 CFactory* pFactory = new CFactory ;
 if (pFactory == NULL)
 return E_OUTOFMEMORY;
 // Get requested interface.
 HRESULT hr = pFactory->QueryInterface(iid, ppv); 
 pFactory->Release(); 
 return hr;
		
}




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