Click here to Skip to main content
13,557,707 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


25 bookmarked
Posted 2 Feb 2008
Licenced CPOL


, 2 Feb 2008
Rate this:
Please Sign up or sign in to vote.
An article about COM in .NET


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”);


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


About the Author

Technical Lead
India India

You may also be interested in...

Comments and Discussions

GeneralA reference for Windows COM and COM+ Pin
Ali Taghvajou16-Nov-08 1:37
memberAli Taghvajou16-Nov-08 1:37 
GeneralSink Event in C++ Pin
Michael@medicscan.com3-Feb-08 15:00
memberMichael@medicscan.com3-Feb-08 15:00 
GeneralRe: Sink Event in C++ Pin
sudeesh4-Feb-08 21:23
membersudeesh4-Feb-08 21:23 
GeneralInteresting information Pin
Cyril Gupta3-Feb-08 5:54
memberCyril Gupta3-Feb-08 5:54 
Interesting... This could be used to connect some legacy app to a new .Net component.

Thanks for writing about it.

You can do anything with a little bit of 'magination.

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180515.1 | Last Updated 3 Feb 2008
Article Copyright 2008 by sudeesh
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid