Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Building COM Servers in .NET

, 2 Feb 2006
Learn the fundamental principles of building COM DLL and EXE Servers using a .NET language.
buildcomserversindotnet_src.zip
SimpleCOMObject
CSharpExeCOMServers
interfaces
implementations
clients
SimpleCOMObject
SimpleCOMObject.def
SimpleCOMObject.rgs
SimpleCOMObject.snk
SimpleCOMObject1.rgs
SimpleCOMObjectps.def
SimpleCOMObject.vcproj.vspscc
SimpleCOMObjectPS.vcproj.vspscc
Interop.SimpleCOMObject.dll
CPP
C#
SimpleCOMObject_CPPImpl
SimpleCOMObject_CPPImpl.def
SimpleCOMObject_CPPImpl.rgs
SimpleCOMObject_CPPImpl1.rgs
SimpleCOMObject_CPPImplps.def
SimpleCOMObject_CPPImpl.vcproj.vspscc
SimpleCOMObject_CPPImplPS.vcproj.vspscc
SimpleCOMObject_CSharpImpl
KeyFile.snk
KeyFile.snk.bak_
SimpleCOMObject_CSharpImpl.csproj.user
VB
CPP
VBClient01
FormMain.frm
MSSCCPRJ.SCC
VBClient01.exe
VBClient01.vbw
VBClient01.vbp
CPPClient01
UsingStandardCOMInterop
IDotNetClassFactory
ManagedCOMLocalServer
implementations
clients
SimpleCOMObject_CSharpExeImpl
App.ico
keyfile.snk
SimpleCOMObject_CSharpExeImpl.csproj.user
CPP
CPPClient01
testobjects
interfaces
implementations
clients
interface
implementations
ITestCSharpObjectInterfaces
TestCSharpObjectInterfaces.csproj.user
TestCSharpObjectInterfaces.snk
ITestCSharpObjectInterfacesImpl01
App.ico
ITestCSharpObjectInterfacesImpl01.csproj.user
ITestCSharpObjectInterfacesImpl01.snk
IDotNetClassFactory
IDotNetClassFactory.csproj.user
IDotNetClassFactory.snk
IDotNetClassFactory_Impl01
IDotNetClassFactory_Impl01.csproj.user
IDotNetClassFactory_Impl01.snk
CPPClient01
Debug
ITestCSharpObjectInterfacesImpl01.exe
implementations
clients
ManagedCOMLocalServer_Impl01
App.ico
keyfile.snk
ManagedCOMLocalServer_Impl01.csproj.user
CPPClient01
CPPClient01.ncb
// CPPClient01.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#import "SimpleCOMObject.tlb" raw_interfaces_only
using namespace SimpleCOMObjectLib;

// Generic CreateInstance() templated function to help in generic object creation.
template <class SmartPtrClass>
bool CreateInstance(LPCTSTR lpszProgID, SmartPtrClass& spSmartPtrReceiver, DWORD dwClsContext = CLSCTX_ALL)
{
  HRESULT			hrRetTemp = S_OK;
  _bstr_t			bstProgID(lpszProgID);
  CLSID				clsid;
  bool				bRet = false;
  
  hrRetTemp = CLSIDFromProgID
  (
    (LPCOLESTR)bstProgID,  //Pointer to the ProgID
    (LPCLSID)&clsid         //Pointer to the CLSID
  );

  if (hrRetTemp == S_OK)
  {
    if (SUCCEEDED(spSmartPtrReceiver.CreateInstance(clsid, NULL, dwClsContext)))
    {
	  bRet = true;
	}
	else
	{
	  bRet = false;
	}
  }

  return bRet;
}





// Generic CreateInstanceByClassFactory() templated function to help in 
// generic object creation. The class factory of the coclass is used to 
// create the instance.
template <class SmartPtrClass>
bool CreateInstanceByClassFactory
(
  LPCTSTR lpszProgID, 
  SmartPtrClass& spSmartPtrReceiver,
  IClassFactoryPtr& spIClassFactoryPtrReceiver,
  DWORD dwClsContext = CLSCTX_ALL
)
{
  HRESULT			hrRetTemp = S_OK;
  _bstr_t			bstProgID(lpszProgID);
  CLSID				clsid;
  bool				bRet = false;
  
  hrRetTemp = CLSIDFromProgID
  (
    (LPCOLESTR)bstProgID,  //Pointer to the ProgID
    (LPCLSID)&clsid         //Pointer to the CLSID
  );

  if (hrRetTemp == S_OK)
  {
    CoGetClassObject
    (
      (REFCLSID)clsid,  //CLSID associated with the class object
      (DWORD)dwClsContext, //Context for running executable code
      (COSERVERINFO*)NULL, //Pointer to machine on which the object is to be instantiated
      (REFIID)IID_IClassFactory,      //Reference to the identifier of the interface
      (LPVOID *)&spIClassFactoryPtrReceiver      //Address of output variable that receives the 
                    // interface pointer requested in riid
    );  
  }
  
  if (spIClassFactoryPtrReceiver)
  {
    hrRetTemp = spIClassFactoryPtrReceiver -> CreateInstance
    (
      NULL, 
      __uuidof(SmartPtrClass), 
      (LPVOID *)&spSmartPtrReceiver
    );
    
    if (SUCCEEDED(hrRetTemp))
    {
      bRet = true;
    }
    else
    {
      bRet = false;
    }
  }

  return bRet;
}





int _tmain(int argc, _TCHAR* argv[])
{
  ::CoInitialize(NULL);
  
  if (1)
  {
	// ISimpleCOMObjectPtr is a smart pointer class which will manage 
	// a pointer to the COM interface ISimpleCOMObject for us.
    ISimpleCOMObjectPtr	spISimpleCOMObject1 = NULL;
    ISimpleCOMObjectPtr	spISimpleCOMObject2 = NULL;
    IClassFactoryPtr	spIClassFactory = NULL;

	// We create an instance of the class factory of the coclass
	// whose CLSID is synonymous with the ProgID 
	// "ManagedCOMLocalServer_Impl01.SimpleCOMObject".
	// From this class factory object, we will also create an 
	// instance of the above-mentioned coclass and get hold of
	// its ISimpleCOMObject interface.
	CreateInstanceByClassFactory<ISimpleCOMObjectPtr>
    (
      "ManagedCOMLocalServer_Impl01.SimpleCOMObject", 
      spISimpleCOMObject1,
      spIClassFactory,
      CLSCTX_LOCAL_SERVER
    );
    
    if (spIClassFactory)
    {
      spIClassFactory -> LockServer(TRUE);
    }
    
	if (spISimpleCOMObject1)
	{
	  spISimpleCOMObject1 -> put_LongProperty(1002);
	  spISimpleCOMObject1 -> Method01(_bstr_t("C# EXE Local Server. The Long Property Value Is : "));
	  // Next Release() spISimpleCOMObject1.
	  spISimpleCOMObject1 = NULL;
	}    

	// We create an instance of an implementation of the ISimpleCOMObject interface
	// as provided by the COM class whose CLSID is synonymous
	// with the ProgID "ManagedCOMLocalServer_Impl01.SimpleCOMObject".
	CreateInstance<ISimpleCOMObjectPtr>
	(
	  "ManagedCOMLocalServer_Impl01.SimpleCOMObject", 
	  spISimpleCOMObject2, 
	  /*CLSCTX_INPROC_SERVER*/ CLSCTX_LOCAL_SERVER
	);
		
	if (spISimpleCOMObject2)
	{
	  spISimpleCOMObject2 -> put_LongProperty(1002);
	  spISimpleCOMObject2 -> Method01(_bstr_t("C# EXE Local Server. The Long Property Value Is : "));
	  spISimpleCOMObject2 = NULL;
	}
		
    if (spIClassFactory)
    {
      spIClassFactory -> LockServer(FALSE);
      spIClassFactory = NULL;
    }
  }

  ::CoUninitialize();
  
  return 0;
}

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)

Share

About the Author

Lim Bio Liong
Web Developer
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.

| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 2 Feb 2006
Article Copyright 2006 by Lim Bio Liong
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid