What is COM Interop in C#.Net

The ultimate goal of COM Interop is to provide access to the existing COM components without requiring that the original component be modified. This tries to make the .NET types equivalent to the COM Types.

COM Interop and Marshalling:

COM had a mechanism of marshalling and un-marshalling to convert between the source and target data types. This is now totally covered in COM Interop using RCW or Runtime Callable Wrapper. This automatically converts the .Net data types to the corresponding COM data types.

RegAsm and tlbimp in COM Interop

In addition, COM Interop allows COM developers to access managed objects as easily as they access other COM objects. It provides a specialized utility (RegAsm.exe) that exports the managed types into a type library and registers the managed component as a traditional COM component. At run time, the common language runtime marshals data between COM objects and managed objects as needed.

This tutorial shows how C# can use COM objects to develop applications. First thing to be done is the creation of wrapper class for the selected COM object. It can be done manually through the command line application called TlbImp.exe (Type Library Importer). This utility converts COM type library to the .NET Framework metadata. This procedure can be done automatically through the .NET environment. We just need to add reference to the COM object to our C# project. So, type in .NET command line next string: tlbimp $WindowsPath$\system32\quartz.dll /out: quartzNET.dll. This command will create a new dll with types that are compatible with any of Managed .NET languages. Now we can add this dll to our C# project or compile our C# file with additional feature “/r quartzNET.dll”.

The following is an example of usage this COM object in C# managed code:

quartzNET.FilgraphManager manager = new quartzNET.FilgraphManagerClass();
quartzNET.IMediaControl mc = (quartzNET.IMediaControl)manager;
// Wait for completion.
Console.WriteLine(“Press Enter to continue.”);

Here is the MediaControl object, which was created in COM. This application gets a name of video file that we want to play from command line and shows it. So, this is a simple example of usage COM Interop. To compile an attached example we just need this quartzNET.dll (is attached too) and .NET command line. Type here next command csc InteropSample.cs /r:quartzNET.dll. It must create an executable file, but it can be run using command line, just type InteroPsample.exe some.avi. So, it opens a console application and also runs a standard Windows media player control to play the video.

10. Methods and properties

Any class in an object-oriented language has method and property members. These are the places where the actual business logic or functionality is written and executed. This tutorial explains how to create and use methods and properties in C#.

C# Methods:

Method is object-oriented item of any language. All C# programs are constructed from a number of classes and almost all the classes will contain methods. A class when instantiated is called an object. Object-oriented concepts of programming say that the data members of each object represent its state and methods represent the object behavior.

Method Signature in C#:

Each method is declared as follows:

Return-type methodname ( Parameterslist );

For better understanding of methods let consider following example. We have a class Man. It can have many fields like that:

public class Man
public Man(){}
private int m_old;
private string m_name;
public string WhatIsYourName()
return m_name;
public string HowOldAreYou()
return m_old;

The private members m_old and m_name define some state of objects that can be created as instances of our class. Also the class Man has two methods, which serve some of our requests. Method string WhatIsYourName() writes current object’s name to the console and returns it, and the second one similar to first return age of man and also writes an output to the console.

The return type in the example above returns strings, which is an in-built data type. The methods can also return any generic C# type or any custom types created by us.

Passing Parameters to Methods in C#:

The input parameters can be passed in two ways.

  • Value type
  • Reference type.

If parameters are passed as value types a new copy of it will be created and passed inside the function. If they are created as reference types, only the address of the parameters will be passed.

See next example:

public int CalculateBirthYear(int year)
int b = year – m_old;
Console.WriteLine(“Birth year is {0}”,b);
return b;

If input parameter pass as reference type it must use keyword ref, in that way we operate with the same cell in memory. That’s mean it can be changed inside any method. A small example for a parameter passed by reference is:

public int CalculateBirthYear(ref int year)
int b = year – m_old;
Console.WriteLine(“Birth year is {0}”,b);
return b;

Now, the function CalculateBirthYear can even modify the value of year as it is passed by reference.

Output Parameters in Methods:

The return values in any function will be enough for any one if only one value is needed. But in case a function is required to return more than one value, then output parameters are the norm. This is not supported in C++ though it can be achieved by using some programming tricks. In C# the output parameter is declared with the keyword out before the data type. A typical example is as follows.

public void CalculateBirthYear(ref int year, out int birthyear)
int b = year – m_old;
Console.WriteLine(“Birth year is {0}”,b);
birthyear = b;

Strictly speaking there is no difference between ref and out parameters. The only difference is that the ref input parameters need an input value and the out parameters don’t.

Variable arguments in C#:

The C# language supports variable arguments through a keyword called params. A typical example for the declaration of a function with variable argument signature is as follows.

Public void functionName(int a, params int[] varParam);

Method Overloading in C#:

A method is considered to be an overloaded method, if it has two or more signatures for the same method name. These methods will contain different parameters but the same return types.

A simple example for an overloaded methods are:

Public void functionName(int a, params int[] varParam);
Public void functionName(int a);

Property in C#:

Property – it is a special method that can return a current object’s state or set it. Simple syntax of properties can see in the following example:

public int Old
get {return m_old;}
set {m_old = value;}
public string Name
get {return m_name;}

Here are two types of properties. A first one can set or get field of class named m_old, and the second is read only. That’s mean it can only get current object’s state.

The significance of these properties is its usability. These properties need not be called with any function names like objectname.get or objectname.set etc., But they can be directly assigned the values or retrieve the values.

A usage of method and properties you can see in attached example. It can be compiled using MS Visual Studio command line. Do get an executable file must do the next steps: In Start menu of Windows should find Programs->MS Visual Studio .NET->MS Visual Studio .NET Tools-> Visual Studio .NET Command Prompt. So run it, and type csc. It is command that run csharp compiler, after it, should type a lines similar to it: csc /out: My.exe My.cs. Now we can run our program from exe file.

And at least few words about access modifiers of methods and properties. In examples there’re only public, but they can also be declared as private, protected or internal. Also aditional modifiers are new, static, virtual, abstract, override. All these will be dealt in next tutorials.Download the sample code from here.


Thanks a lot for visiting this site .. Please leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s