Click here to Skip to main content
15,885,278 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.4K   4K   301  
Learn the fundamental principles of building COM DLL and EXE Servers using a .NET language.
using System;
using System.Runtime.InteropServices;

namespace ManagedCOMLocalServer_Impl01
{
	class ClassFactoryBase : IClassFactory
	{
		// CoRegisterClassObject() is used to register a Class Factory
		// into COM's internal table of Class Factories.
		[DllImport("ole32.dll")]
		static extern int CoRegisterClassObject([In] ref Guid rclsid,
			[MarshalAs(UnmanagedType.IUnknown)] object pUnk, uint dwClsContext,
			uint flags, out uint lpdwRegister);

		// Called by an COM EXE Server that can register multiple class objects 
		// to inform COM about all registered classes, and permits activation 
		// requests for those class objects. 
		// This function causes OLE to inform the SCM about all the registered 
		// classes, and begins letting activation requests into the server process.
		[DllImport("ole32.dll")]
		static extern int CoResumeClassObjects();

		// CoRevokeClassObject() is used to unregister a Class Factory
		// from COM's internal table of Class Factories.
		[DllImport("ole32.dll")]
		static extern int CoRevokeClassObject(uint dwRegister);

		public ClassFactoryBase()
		{
		}

		protected UInt32	m_locked = 0;
		protected uint		m_ClassContext = (uint)CLSCTX.CLSCTX_LOCAL_SERVER;
		protected Guid		m_ClassId;
		protected uint		m_Flags;
		protected uint		m_Cookie;

		public virtual void virtual_CreateInstance(IntPtr pUnkOuter, ref Guid riid, out IntPtr ppvObject)
		{
			IntPtr nullPtr = new IntPtr(0);
			ppvObject = nullPtr;
		}

		public uint ClassContext
		{
			get
			{
				return m_ClassContext;
			}
			set
			{
				m_ClassContext = value;
			}
		}

		public Guid ClassId
		{
			get
			{
				return m_ClassId;
			}
			set
			{
				m_ClassId = value;
			}
		}

		public uint Flags
		{
			get
			{
				return m_Flags;
			}
			set
			{
				m_Flags = value;
			}
		}

		public bool RegisterClassObject()
		{
			// Register the class factory
			int i = CoRegisterClassObject
				(
				ref m_ClassId, 
				this, 
				ClassContext, 
				Flags,
				out m_Cookie
				);

			if (i == 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		public bool RevokeClassObject()
		{
			int i = CoRevokeClassObject(m_Cookie);

			if (i == 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		public static bool ResumeClassObjects()
		{
			int i = CoResumeClassObjects();

			if (i == 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		#region IClassFactory Implementations
		public void CreateInstance(IntPtr pUnkOuter, ref Guid riid, out IntPtr ppvObject)
		{
			virtual_CreateInstance(pUnkOuter, ref riid, out ppvObject);
		}

		public void LockServer(bool bLock)
		{
			if (bLock)
			{
				ManagedCOMLocalServer_Impl01.InterlockedIncrementServerLockCount();
			}
			else
			{
				ManagedCOMLocalServer_Impl01.InterlockedDecrementServerLockCount();
			}

			// Always attempt to see if we need to shutdown this server application.
			ManagedCOMLocalServer_Impl01.AttemptToTerminateServer();
		}
		#endregion
	}
}

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