Click here to Skip to main content
15,896,500 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 461.1K   4K   301  
Learn the fundamental principles of building COM DLL and EXE Servers using a .NET language.
using System;
using System.Windows.Forms;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
using ITestCSharpObjectInterfaces;


namespace ITestCSharpObjectInterfacesImpl01
{
	/// <summary>
	/// Summary description for TestCSharpObject01.
	/// </summary>
	public class TestCSharpObject01 : MarshalByRefObject, ITestCSharpObjectInterfaces.ITestCSharpObjectInterfaces
	{
		private string m_stringProperty;

		public TestCSharpObject01()
		{
			m_stringProperty = "";
			Console.WriteLine("TestCSharpObject01 constructor.");
		}

		public string stringProperty
		{
			get
			{
				return m_stringProperty;
			}

			set
			{
				m_stringProperty = value;
			}
		}

		public bool DisplayMessage (  )
		{
			if (m_stringProperty != "")
			{
				MessageBox.Show(m_stringProperty, "TestCSharpObject01");
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			//
			// TODO: Add code to start application here
			//
			string strChannelType = "TCP";
			int iPort = 9000;

			if (args.Length > 0)
			{
				for (int i = 0; i < args.Length; i++)
				{
					Console.Write("Argument : ");
					Console.WriteLine(args[i]);
				}

				strChannelType = args[0];
				iPort = Convert.ToInt32(args[1]);
			}

			if (strChannelType == "TCP")
			{
				TcpServerChannel tcp_channel = new TcpServerChannel(iPort);
				ChannelServices.RegisterChannel(tcp_channel);
			}
			else
			{
				HttpServerChannel http_channel = new HttpServerChannel(iPort);
				ChannelServices.RegisterChannel(http_channel);
			}

			ActivatedServiceTypeEntry remObj = new ActivatedServiceTypeEntry(typeof(TestCSharpObject01));

			string strApplicationName = "TestCSharpObject01";

			RemotingConfiguration.ApplicationName = strApplicationName;
			RemotingConfiguration.RegisterActivatedServiceType(remObj);

			Console.WriteLine("Press [ENTER] to exit.");

			Console.ReadLine();
		}
	}
}

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