Click here to Skip to main content
15,885,767 members
Articles / Programming Languages / C#

Building COM Servers in .NET

Rate me:
Please Sign up or sign in to vote.
4.87/5 (95 votes)
2 Feb 2006CPOL98 min read 458.5K   4K   301  
Learn the fundamental principles of building COM DLL and EXE Servers using a .NET language.
// SimpleCOMObject_CPPImpl_.h : Declaration of the CSimpleCOMObject_CPPImpl

#pragma once
#include "resource.h"       // main symbols

#include "SimpleCOMObject_CPPImpl.h"
#include "_ISimpleCOMObject_CPPImplEvents_CP.h"


// CSimpleCOMObject_CPPImpl

class ATL_NO_VTABLE CSimpleCOMObject_CPPImpl : 
	public CComObjectRootEx<CComSingleThreadModel>,
	public CComCoClass<CSimpleCOMObject_CPPImpl, &CLSID_SimpleCOMObject_CPPImpl>,
	public ISupportErrorInfo,
	public IConnectionPointContainerImpl<CSimpleCOMObject_CPPImpl>,
	public CProxy_ISimpleCOMObject_CPPImplEvents<CSimpleCOMObject_CPPImpl>, 
	public IDispatchImpl<ISimpleCOMObject_CPPImpl, &IID_ISimpleCOMObject_CPPImpl, &LIBID_SimpleCOMObject_CPPImplLib, /*wMajor =*/ 1, /*wMinor =*/ 0>,
	public IDispatchImpl<ISimpleCOMObject, &__uuidof(ISimpleCOMObject), &LIBID_SimpleCOMObjectLib, /* wMajor = */ 1, /* wMinor = */ 0>
{
public:
	CSimpleCOMObject_CPPImpl()
	{
	}

	~CSimpleCOMObject_CPPImpl()
	{
	}

	DECLARE_REGISTRY_RESOURCEID(IDR_SIMPLECOMOBJECT_CPPIMPL1)


	BEGIN_COM_MAP(CSimpleCOMObject_CPPImpl)
		COM_INTERFACE_ENTRY(ISimpleCOMObject_CPPImpl)
		COM_INTERFACE_ENTRY2(IDispatch, ISimpleCOMObject)
		COM_INTERFACE_ENTRY(ISupportErrorInfo)
		COM_INTERFACE_ENTRY(IConnectionPointContainer)
		COM_INTERFACE_ENTRY(ISimpleCOMObject)
	END_COM_MAP()

	BEGIN_CONNECTION_POINT_MAP(CSimpleCOMObject_CPPImpl)
		CONNECTION_POINT_ENTRY(__uuidof(_ISimpleCOMObject_CPPImplEvents))
	END_CONNECTION_POINT_MAP()
	// ISupportsErrorInfo
	STDMETHOD(InterfaceSupportsErrorInfo)(REFIID riid);

	DECLARE_PROTECT_FINAL_CONSTRUCT()

	HRESULT FinalConstruct()
	{
	    m_lLongProperty = 0;
		return S_OK;
	}

	void FinalRelease() 
	{
	}

public:

protected :
	// Member functions and data.
	long	m_lLongProperty;

	// ISimpleCOMObject Methods
public:
	STDMETHOD(get_LongProperty)(long * pVal)
	{
	    *pVal = m_lLongProperty;
	    
		return S_OK;
	}
	STDMETHOD(put_LongProperty)(long pVal)
	{
	    m_lLongProperty = pVal;
	    
		return S_OK;
	}
	STDMETHOD(Method01)(BSTR strMessage)
	{
	    std::string strTemp;
	    _bstr_t		_bstMessage(strMessage, true);
	    
	    strTemp.resize(256, 0);
	    
	    sprintf (&(strTemp[0]), "%s %d", (LPCTSTR)_bstMessage, m_lLongProperty);
	    
	    MessageBox (NULL, strTemp.c_str(), "SimpleCOMObject_CPPImpl", MB_OK);
	    
		return S_OK;
	}
};

OBJECT_ENTRY_AUTO(__uuidof(SimpleCOMObject_CPPImpl), CSimpleCOMObject_CPPImpl)

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Systems Engineer NEC
Singapore Singapore
Lim Bio Liong is a Specialist at a leading Software House in Singapore.

Bio has been in software development for over 10 years. He specialises in C/C++ programming and Windows software development.

Bio has also done device-driver development and enjoys low-level programming. Bio has recently picked up C# programming and has been researching in this area.

Comments and Discussions