Send a suggestion!

We're building a brand new version of the site, and we'd love to hear your ideas

Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 21,580 times

Contents

Related Categories

COM Interoperability in .NET Part 2 - The managed code

ggarung

The managed code

To illustrate COM type communication with managed code, Let us see an example
in which I create a C# class library which has a class named Calculatorwhich supports three methods named Add(),Subtract()and Hello(). Notice that we define another interface named Imuldiv.

namespace Simpleclasslib
{
  using System;
  using System.Runtime.InteropServices;

  public interface Imuldiv
  {
    int Multiply(int x,int y);
    int Division(int x,int y);
  }
  
  public class Calculator:Imuldiv
  {
    public Calculator(){}
    public int Add(int x,int y) {
      return x+y;
    }
    public int Subtract(int x,int y)
    {
      return x-y;
    }
    int Imuldiv.Multiply(int x,int y)
    {
      return x*y;
    }
    int Imuldiv.Division(int x,int y)
    {
      return x/y;
    }
    public string Hello(string strName)
    {
      string str ;
      str = "Hello " + strName ;
      return str ;
    }
  }
}

Once the managed code is written, the compilation process is the same as it would be for any other piece of managed code.

csc /out:Server.dll /target:library Calculator.cs

Now let us see the Server.dll in the ILDasm.exe.In that you can see the calculator class members and Imuldiv Interface members.

G.Gnana Arun Ganesh is the Administrator and the Founder of ARUN MICRO SYSTEMS (www.arunmicrosystems.netfirms.com). He has been programming in C++, Visual Basic, COM, Java and Microsoft Technologies for 3+ years. Arun's background includes Bachelor degree in ECE.He is one of the Top authors writing articles in C# and .NET in various websites. He is an Active person in the panel of Technical Reviewers in Prentice Hall Publishers and Sams Publication. In .NET the last book he reviewed is the "C# how to program" written by Harvey and Paul Deitel. You can contact him through ggarung@rediffmail.com

Comments