Category Archives: Interops

Interops: .NET and Mono

@Corbett National Park,Photo by Ajey godbole

Ever wondered a scenario where you want to develop a cross platform .NET application ?

What kind of framework is required for this kind of applications ??

Well lets discuss!

Mono software platform allows developers to create cross platform applications which is an open source implementation of Microsoft’s .NET framework based on ECMA standards.

Main components of Mono

1) C# compiler for compiling C# 1.0 and C# 2.0 features and  additionally many of the C# 3.0 features.

2) Mono runtime which implements Common Language Infrastructure (CLI) , Just-In-Time (JIT) compiler, Ahead-of-Time (AOT) compiler, a library loader, the garbage collector, threading system and interoperability functionality.

3)Base Class Library which provides comprehensive set of classes for building applications.

4) Mono Class Library provides additional classes  for Gtk+, Zip files,LDAP,OpenGL etc.

The whole advantage of this Mono framework is , it provides a framework for user for Cross Platform development which is varied on Linux, Microsoft Windows, Mac OS X, BSD, and Sun Solaris, Nintendo Wii, Sony PlayStation 3, Apple iPhone and many others with a additional advantage that developer is already used to .NET framework and programming language such as C#, VB.NET and Eiffel etc. Scripting and Embedding can also be used for various purposes.

This Wiki Page gives detailed information about Mono framework  and its extensions.

As given on Wikipedia Several projects extend Mono such as



Updates and Resources

Mono 2.6.1  is now available.

There are various forums and blogs which discuss latest updates about Mono which can be accessed here.

Mono Tools for visual studio is available for download with 30 day trial.


.NET <-> Java Interoperability


Jugaad,Photo by Ajey godbole


As we have been discussing this earlier necessity of Interoperability arises in various cases such as

  1. Reuse of existing systems
  2. Proof of concepts
  3. Migration
  4. To maintain lower project costs by using existing legacy apps.

Considering above key points, lets check out some of the important aspects related to .NET and Java Interoperability.

While referring to various web sites for practical options available in  .NET and Java Interoperability,  I came across some info given on code project written by Guy Balteriski. This article talks in depth about various aspects related to .NET and Java Interoperability such as

  1. Part I: Introduction to Java & .NET interoperability and the suggested solution
  2. Part II: Implement .NET proxy to the Java classes
  3. Part III: Using Attributes to extend the Java API solution
  4. Part IV: Java to .NET API calls
  5. Part V: Implement Java proxy to the .NET classes
  6. Part VI: Adding Annotations to extend the .NET API solution


A must see website on this topic is jnbridge.

This MSDN example shows  Java/.NET Interoperability with the Web Service.

Overall with this frameworks and other options a true blend of interoperability can be achieved between Java and .NET for delivering real time problem scenarios.

Hope this helps !


Diwali '09

Technorati Tags:

In my previous posts we saw important aspects regarding

  1. Interops-Why and Ways to do it
  2. Interops-Win32<->.NET
  3. Interops-COM<->.NET

Moving further into world of interops lets now check some of  interesting aspects of Interlanguage interoperability on .NET framework.

The problems of interoperability have been around for many years and I found this useful info on MSDN which describes number of standards and architectures developed to address this issues

  1. Representation Standards
  2. Architecture Standards
  3. Language Standards
  4. Execution Environments

Representation Standards

External Data Representation(XDR) and Network Data Representation (NDR ), address the issue of passing data types between different machines. ( e.g. big-endian issues, little-endian issues,  and different word sizes)

Architecture Standards

Remote Procedure Call by Distributed Computing Environment

Common Object Request Broker Architecture

Component Object Model

All these standards handle issues of calling methods across language, process and machine boundaries.

Language Standards

ECMA C# and Common Language Infrastructure Standards

ANSI C standards which allow distribution of source code across compilers and machines.

Execution Environments

Common Language Runtime

Dynamic Language Runtime

Referring to CLR info on MSDN we can easily figure out it consists of 3 main components

  1. Common Type System which supports many of the operations found in modern programming languages.
  2. A metadata system, which allows metadata to be persisted with types at compile time and the used by execution system at run time.
  3. A execution system which allows which allows .NET programs execution providing features such as memory management.

Dynamic Language Runtime provides following services

  1. A dynamic type system
  2. Dynamic method dispatch
  3. Dynamic code generation
  4. Hosting API

DLR is used to implement dynamic languages like Python and Ruby on the .NET framework.

There are still interesting aspects to be covered in interoperability and I will be covering it in next two posts. Keep visiting 🙂

Interops – Inter Language Approach

Interops – COM <-> .NET


In my last post I mentioned ,

There are two ways that C# code can directly call unmanaged code:

  1. Directly call a function exported from DLL
  2. Call an interface method on a COM object

In this post lets concentrate on how to call an interface method on a COM object and how this can be implemented for managed environment.

.NET framework provides following facilities to perform COM Interop

  1. Creating COM objects
  2. Determining if a COM interface is implemented by an object
  3. Calling methods on COM interfaces
  4. Implementing objects and interfaces that can be called by COM clients.

Additionally, .NET framework also handles reference counting issues with COM Interop so there is no need to call or implement Addref and Release.

COM Interop provides access to existing COM components without requiring that the original components need to be modified and for this we can use a COM Interop utility (Tlbimp.exe).  Type library importer converts the type definitions found within a COM type library into equivalent definitions in a CLR assembly.

Type library Importer performs following conversions :

  1. COM coclasses are converted to C# classes with a parameter less constructor
  2. COM structs are converted to C# structs with public fields.

Microsoft Intermediate Language Disassembler provides a great way to check out the output of Tlbimp.exe to view the result of the conversion.

In addition to this, COM Interop allows programmers to access managed objects as easily as they access other COM objects Here COM Interop provides a Assembly Registration Tool  that exports the managed types into a type library and registers the managed component as a traditional COM component.

I referred MSDN for following steps and key points are as follows

Creating a COM Class Wrapper

Tlbimp converts a COM type library into .NET framework metadata- effectively creating a managed wrapper that can be called from any managed language.

  1. .NET framework metadata created with Tlbimp can be included in a C# build via /R compiler option.
  2. Using Visual Studio we need to add only COM type library and conversion is done automatically.

Important attributes in understanding COM mapping are

  1. ComImport
  2. GUID
  3. Interface Type
  4. PreserveSig

Declaring and Creating Comcoclass object

COM coclasses are represented as a classes with parameterless constructors  in C#. ComImport attribute is a must for them. Creating instance of this class using the new operator is the C# equivalent of calling CoCreateInstance.

Additional restrictions are as follows

  1. The class must not inherit from any other class
  2. The class must implement no interfaces
  3. GUID for the class

Declaring a COM Interface

  1. COM interfaces are represented in C# as interfaces with COMImport and GUID attributes.
  2. COM Interfaces declared in C# must include declarations for all members of their base interfaces with the exception of members of IUnknown and IDispatch. These are added by .NET framework automatically.
  3. COM interfaces which derive from IDispatch must be marked with the InterfaceType attribute.
  4. When calling a COM interface method from C# code, CLR runtime must marshal the parameters and return values to/ from the COM object.
  5. Common way to return success or failure is to return an HRESULT and have an out paramter marked as “retval” in MIDL for the real return value of method.

Apart from this following are the links, one should must see for        COM <-> .NET interoperability given on MSDN

Introduction to COM Interop

COM Interop Part 1: C# Client Tutorial

COM Interop Part 2: C# Server Tutorial

COM Callable Wrapper

Exposing .NET Framework Components to COM

Advanced COM Interoperability

Simplify App Deployment with ClickOnce and Registration-Free COM

I will be writing few more posts on Interops and some of the interesting aspects discovered using this. Meanwhile, do let me know your feedback and expectations from this blog.

Interops – Win32 <-> .NET


Technorati Tags: ,

As discussed in my first post , a common scenario where Interops are needed is accessing a legacy code written using Win32 APIs  in .NET applications for various reasons.

There are two ways that C# code can directly call unmanaged code:

  1. Directly call a function exported from DLL
  2. Call an interface method on a COM object

Luckily Platform Invoke (P/Invoke) service provided by .NET framework supports invoking unmanaged code residing in DLLs to be called in .NET environment.

C# provides a mechanism for providing declarative tags, called attributes which can be placed in certain entities in source code to specify additional information. Information contained in attributes can be ret rived through reflection, or one can use predefined attributes.

Use of DllImport can be made as follows in C# for calling MathFuncimportClass from unmanaged DLL.

[DllImport] public class MathFuncimportClass { … }

In addition to this , we can have few named parameters as well as unnamed(positional) parameters.

[DllImport(“user32.dll”, SetLastError=false,ExactSpelling=false)]
[DllImport(“user32.dll”, ExactSpelling=false,SetLastError=false)]

For a broader look one must have a reading of DllImportAttribute Members

An excellent info on EntryPoint, Charset, SetLastError and CallingConventions is given on Jason Clark’s post on MSDN Magazine. These are all optional properties in DllImportAttribute.

Data Marshaling

While transferring data between managed and unmanaged combination of code , CLR follows number of rules that one must know so as to tackle any tricky scenario.

While passing parameter to a Windows API function important points needs to be considered as follows

  1. Is the data in a form of integer or float ?
  2. Is the data signed integer or unsigned integer ?
  3. Bitwise presentation of integer data
  4. Floating point data and its precision.

Some special scenarios encountered while marshalling are

  1. Marshaling pointers
  2. Marshaling Opaque pointers
  3. Marshaling Text
  4. Marshaling a complex structure.

A detailed explanation for all these four points is here on MSDN Magazine. Figure 4 on this link also describes actual MSIL signature seen by CLR in various cases.

Copying and Pinning

While CLR performs Data Marshaling , it used two options copying and pinning .

1) While marshaling the data, the interop marshaler can copy or pin the data being marshaled. Copying the data will place a copy of data from one memory to another memory location.

Value types passed by value and by reference explains this scenario.

2) Pinning temporarily locks the data in its current memory location, and thus it is kept away from relocating by CLR’s garbage collector.

A clear scenario can be visualized here at Reference types passed by value and by reference

P/Invoke Interop Assistant

For quickly figuring out the P/Invoke signature for Win32 API function PInvoke Interop Assistant can be very useful. This tool assists developers with conversion from C++ to managed P/Invoke signatures and vice a versa.

This tool is available for download on codeplex website.

Interops – Why and Ways to do it

Western Thali

Technorati Tags:

For next few days I intend to write about various aspects of .Net Interops and different scenarios where Interops are extremely useful.

Before moving ahead to interops lets first check out some of the important terminologies in .NET .

Managed Code and Data :

Code developed on .NET platform (e.g. code in C#,VB.NET etc) is referred to as managed code and contains some metadata which is used for Common Language Runtime. ( for more info visit  CLR Team Blog ) Data used by .NET applications is called managed data , since .NET runtime manages data-related tasks such as allocating and reclaiming memory, and type checking.  By default code and data used by .NET applications is managed and while accessing the unmanaged code and data e.g. COM objects we can use interop assemblies which will be discussed in later part of this post.

Assemblies :

A primary building block of .NET application is referred to as assembly. It is a collection of functionality that is built, versioned and deployed as a single implementation unit containing one or more files. Each assembly contains a assembly manifest. Every assembly whether static or dynamic contains collection of data that describes how the elements in the assembly relate to each other. The metadata contained in assembly is needed to specify the assembly’s version requirements and security identity. The assembly manifest can be stored in either a PE file ( an .exe or .dll ) with Microsoft intermediate language (MSIL) code or in a standalone PE file that contains only assembly manifest information.  ( A must see article on assemblies is here  MSDN )

Type Libraries and Assembly Manifests :

A type library declares the classes , interfaces , constants and procedures that are exposed by an application or dynamic link library ( DLL ) . A type library is usually a resource in a program file. It can also be a stand-alone binary file with the extension .tlb or .olb . Manifests also include information about

  1. Assembly identity, version, culture and digital signature
  2. Files that make up the assembly implementation
  3. Types and resources that make up the assembly, including those
  4. that are exported from it
  5. Compile-time dependencies on other assemblies.
  6. Permissions required to run the assemblies to run properly.

For importing information from a type library into .NET application Visual Studio.NET contains a utility called as Tlbimp.exe.  ( more information is given here. )

Why Interops ?

While implementing solutions for various problem statements there is a necessity of invoking features provided by other applications or interaction with APIs provided to invoke important services or features.

Following may be some of the scenarios where interops are required

  1. Invoking Win32 apps in .NET
  2. Invoking COM components in application.
  3. Invoking features of MS Office.
  4. Invoking inter language communication
  5. Invoking features of .NET on other platforms.
  6. Invoking functionality provided by a native code where a conversion process is necessary to translate argument data between managed code and native code.

Ways to implement Interops :

Having seen some of the basic  reasons why interoperability is required , lets see some of the ways these interops can be implemented.


Primary Interop Assembly is a unique, vendor supplied assembly that contains type definitions ( as metadata) of types implemented with COM. There can be only one Primary Interop Assembly , which must be signed with a strong name by the publisher of COM type library. A single primary interop assembly can wrap more than one version of the same type library.

Primary Interop Assemblies must meet following requirements

  1. Include all COM types defined in the original type library and maintain the same GUID identities.
  2. Be signed with a strong name using standard public key cryptography
  3. Contain the PrimaryInteropAssemblyAttribute.
  4. Avoid redefining external COM types.
  5. Reference other primary interop assemblies for external for external COM dependencies.

Having a single type definition ensures that all .NET framework applications bind to the same type at compile time, and that the type is marshaled the same way at run time. It is important to create only one primary interop assembly for each COM type library because multiple assemblies can introduce type incompatibility.

There are several other aspects with PIA such as

  1. Naming a primary interop assembly
  2. Generating a primary interop assembly
  3. Customizing a primary interop assembly
  4. Distributing a primary interop assembly

MSDN explains in-depth approach of all these points.

Office XP PIAs can be downloaded here .

System.Runtime.InteropServices Namespace

System.Runtime.InteropServices namespace provide a wide variety of members that support COM interop and platform invoke services.

A detailed description of its classes and structures is given on MSDN which covers all the aspects about using this namespace and is a must read chapter for everyone who wants to cover this concepts in a systematic approach.

Platform Invoke

Platform Invoke services ( commonly referred as P/Invoke) allows managed code to call unmanaged functions that are implemented in a DLL.

Important aspects one must consider while using P/Invoke are

  1. Using Attributes
  2. DllImportAttribute Class
  3. MarshalAsAttribute Class
  4. StructLayoutAttribute Class
  5. InAttribute Class
  6. OutAttribute Class

Java and .NET interoperability

With Restlet lightweight open source project its now possible to use lightweight REST framework for Java that used Restlet extension for ADO.NET Data Services.

More info about Restlet open source project is here

In coming articles I will be explaining all of these ways in detail and resources that one must have a look for covering several effective practices while using Interops. I will also be covering various areas where these interops can be used.

Do let me know all of your suggestions and areas to be covered in this post.

Keep Visiting !!