Click here to Skip to main content
15,885,914 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.
using System;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;  // For using MessageBox.
using Interop.SimpleCOMObject; // In order to implement ISimpleCOMObject.
using System.Runtime.InteropServices;  // For use of the GuidAttribute, ProgIdAttribute and ClassInterfaceAttribute.

namespace ManagedCOMLocalServer_Impl01
{
	/// <summary>
	/// Summary description for SimpleCOMObject.
	/// This is the class that will be exported to unmanaged COM client apps.
	/// </summary>
	[
	  Guid("E1FE1223-45C2-4872-9B1E-634FB850E753"),  // We indicate a specific CLSID for "ManagedCOMLocalServer_Impl01.SimpleCOMObject" for convenience of searching the registry.
	  ProgId("ManagedCOMLocalServer_Impl01.SimpleCOMObject"),  // This ProgId is used by default. Not 100% necessary.
	  ClassInterface(ClassInterfaceType.None)  // Specify that we will not generate any additional interface with a name like _SimpleCOMObject.
    ]
	public class SimpleCOMObject : 
		ReferenceCountedObjectBase, // SimpleCOMObject is derived from ReferenceCountedObjectBase so that we can track its creation and destruction.
		ISimpleCOMObject  // SimpleCOMObject must implement the ISimpleCOMObject interface.
	{
		private int m_iLongProperty;

		public SimpleCOMObject()
		{
			// ReferenceCountedObjectBase constructor will be invoked.
			Console.WriteLine("SimpleCOMObject constructor.");
		}

		~SimpleCOMObject()
		{
			// ReferenceCountedObjectBase destructor will be invoked.
			Console.WriteLine("SimpleCOMObject destructor.");
		}

		public int LongProperty
		{
			get
			{
				return m_iLongProperty;
			}
		  
			set
			{
				m_iLongProperty = value;
			}
		}
		
		public void Method01 (String strMessage)
		{
			StringBuilder sb = new StringBuilder(strMessage);
		  
			sb.Append(LongProperty.ToString());
		  
			MessageBox.Show(sb.ToString(), "ManagedCOMLocalServer_Impl01.SimpleCOMObject");
		}
	}

	class SimpleCOMObjectClassFactory : ClassFactoryBase
	{
		public override void virtual_CreateInstance(IntPtr pUnkOuter, ref Guid riid, out IntPtr ppvObject)
		{
			Console.WriteLine("SimpleCOMObjectClassFactory.CreateInstance().");
			Console.WriteLine("Requesting Interface : " + riid.ToString());

			if (riid == Marshal.GenerateGuidForType(typeof(ISimpleCOMObject)) ||
				riid == ManagedCOMLocalServer_Impl01.IID_IDispatch ||
				riid == ManagedCOMLocalServer_Impl01.IID_IUnknown)
			{
				SimpleCOMObject SimpleCOMObject_New = new SimpleCOMObject();

				ppvObject = Marshal.GetComInterfaceForObject(SimpleCOMObject_New, typeof(ISimpleCOMObject));
			} 
			else
			{
				throw new COMException("No interface",  unchecked((int) 0x80004002));
			}
		}
	}
}

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