Click here to Skip to main content
15,881,812 members
Articles / Programming Languages / C#
Article

COM in .NET

Rate me:
Please Sign up or sign in to vote.
2.73/5 (20 votes)
2 Feb 2008CPOL2 min read 73.7K   842   26   4
An article about COM in .NET

Introduction

Component Object Model is a method to facilitate communication between different applications and languages. There are many other ways to structure software components. What makes COM interesting is that it is a widely accepted interoperability standard. In addition, the standard explains the way the memory should be organized for all the objects.

Interfaces are the contract between the client and the server, i.e. an interface is the way in which an object exposes its functionality to the outside world. In COM, an interface is a table of pointers to functions implemented by the object. The table represents the interface, and the functions to which it points are the methods of that interface.

Creating COM components in .NET

Creating COM components in .NET is not that difficult as in C++. The following steps explain the way to create the COM server in C#:

  1. Create a new Class Library project.
  2. Create a new interface, say IManagedInterface, and declare the methods required. Then provide the Guid (this is the IID) for the interface using the GuidAttribute defined in System.Runtime.InteropServices. The Guid can be created using the Guidgen.exe. [Guid("3B515E51-0860-48ae-B49C-05DF356CDF4B")]
  3. Define a class that implements this interface. Again provide the Guid (this is the CLSID) for this class also.
  4. Mark the assembly as ComVisible. For this go to AssemblyInfo.cs file and add the following statement [assembly: ComVisible (true)]. This gives the accessibility of all types within the assembly to COM.
  5. Build the project. This will generate an assembly in the output path. Now register the assembly using regasm.exe (a tool provided with the .NET Framework)- regasm <projectpath>\bin\debug\ComInDotNet.dll \tlb:ComInDotNet.tlb This will create a TLB file after registration.
  6. Alternatively this can be done in the Project properties -> Build -> check the Register for COM interop.

The COM server is ready. Now a client has to be created. It can be in any language. If the client is .NET, just add the above created COM assembly as reference and use it.

The following steps explain the method to create an unmanaged client. Here the client is in VC++ 6.0.

  1. Create an MFC AppWizard EXE using the wizard in MSDEV 6.0.
  2. Import the COM server (TLB file generated by regasm.exe) using the #import directive. #import “ComInDotNet.tlb” no_namespace named_guids raw_interfaces_only.
  3. Now the COM library should be loaded using the CoInitialize and component can be created using CoCreateInstance.
  4. Next the methods defined in the COM Server can be invoked. pInterface->Execute(“blah blah blah”);

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
India India
Sudeesh

Comments and Discussions

 
GeneralA reference for Windows COM and COM+ Pin
Ali Taghvajou16-Nov-08 1:37
professionalAli Taghvajou16-Nov-08 1:37 
GeneralSink Event in C++ Pin
Coding 1013-Feb-08 15:00
Coding 1013-Feb-08 15:00 
GeneralRe: Sink Event in C++ Pin
sudeesh4-Feb-08 21:23
sudeesh4-Feb-08 21:23 
GeneralInteresting information Pin
Cyril Gupta3-Feb-08 5:54
Cyril Gupta3-Feb-08 5:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.