By Ganesh Khadanga
Introduction
The common language runtime provides built in support for language interoperability. The common language runtime provides the necessary foundation for language interoperability by specifying and enforcing a common type system and by providing metadata. Metadata enables language interoperability by defining a uniform mechanism for storing and retrieving information about types. Every type and member defined, attributes defined and referenced in a module or assembly is described within metadata. Compilers store type information as metadata, and the common language runtime uses this information to provide services during execution. The runtime can manage the execution of multilanguage applications because all type information is stored and retrieved in the same way, regardless of the language the code was written in. To ensure that your managed code is accessible to developers using any programming language, the .NET Framework provides the Common Language Specification(CLS). The CLS describes a fundamental set of language features and defines rules for how those features are to be used.
DLL in C#
A dynamic-link library (DLL) is an executable file that acts as a shared library of functions. C# also includes native support for component object model and windows based applications. One need not have to implement the interface unknown and its member functions queryinterface, addref or release. The simple class program can be compiler to create a library. Once the library is generated the same can be referred in any other program using compiler options.
//program #: 1
//Compile using csc /t:library /out:industry.dll industry.cs
using System;
namespace industrial
{
public class greaternoida
{
public string display()
{
return ("Provides plot for industrial development.");
}
}
}
This generated the industry.dll. The /t option specifies that the output is a dll. The /out option indicates that the output will be named as mentioned.
//program #: 2
//Compile: csc /r:industry.dll program2.cs
using System;
using industrial;
class noida:greaternoida
{
public new string display()
{
return ("Noida also provides plots for industrial and residential purposes.");
}
}
class Test
{
public static void Main()
{
greaternoida gnoida=new greaternoida();
noida nda=new noida();
Console.WriteLine(gnoida.display());
Console.WriteLine(nda.display());
}
}
output:
Provides plot for industrial development.
Noida also provides plots for industrial and residential purposes.
This program uses the class greaternoida class as developed in the previous program. Then displays the output of the display function. Another class noida is derived from greaternoida class and it also uses a new display function to display it’s information.
Managed C++ Class
Managed Extensions for C++ are a set of language extensions to C++. Managed Extensions allow you to intermix traditional unmanaged and managed C++ code within the same application. With Managed Extensions one can directly create, and call, a .NET Framework class from C++ code.
The Managed Extensions provide built-in support for metadata, and enable declaration of new types and categories of metadata. The limitations of type libraries and restrictive automation types are removed with managed classes.
The general programming in Manages extensions for C++ has been supplemented with new codes to take care of garbage collected heap and to include the .Net Framework platform. The keyword __gc on a class or struct indicates that it is garbage-collected, and its lifetime is managed by the common language runtime. The #using declaration is used to import the metadata contained in the specified file into the program. In Managed Extensions for C++ program, the file mscorlib.dll must be imported via the #using directive. As mentioned previously, the namespace statement is used to create a new namespace to encapsulate the classes that will be created. The cl compiler is used to compile the managed code. The /clr compiler option enables the use of Managed Extensions for C++ and creates an output file that requires the Common Language Runtime (CLR). The /LD option passes the /DLL option to the linker and creates a dll.The compiler generates Microsoft Intermediate Language (MSIL) code which is a CPU-independent set of instructions that can be efficiently converted to native code. The Just in time compiler(JIT) converts the MSIL to CPU specific code just before the execution of the code.
//program#: 3
//compile: cl /CLR /LD first1.cpp
#using
using namespace System;
namespace gan {
__gc public class testcpp
{
public:
int display()
{
Console::WriteLine("Hello, World! from display function of managed C++");
return 0;
}
};
};
This produces a dll with name first1.dll. This dll contains the necessary assembly for using in any CLR complaint language. The next program demonstrates how to use this dll in C# programme.
C++ Class inside C#
//program # :4
//compile: csc /r:first1.dll program4.cs
using System;
class Test{
public static void Main()
{
gan.testcpp d1;
d1=new gan.testcpp();
d1.display();
}
}
output:
Hello, World! from display function of managed C++
This example demonstrates the use of a C++ class inside a C# programme. The testcpp class belongs to the first1.cpp programme. An instance of the class testcpp is cretaed in the C# is d1. The display function is invoked to display the message.
//prgramme #: 5
//compile:cl /clr /LD program5.cpp
#using
using namespace System;
namespace industrial {
__gc public class greaternoida {
public:
int virtual display()
{
Console::WriteLine("Provides plot for industrial and residential purpose");
return 0;}
};
};
This program creates an dll with name program5.dll
//programm #: 6
//compile: csc /r:program5.dll program6.cs
using System;
using industrial;
class noida:greaternoida
{
public override int display()
{
Console.WriteLine("Noida also provides plot for industrial and residential development");
return 0;
}
}
class society:greaternoida{
public override int display()
{
Console.WriteLine("Society only provides plot for residential development");
return 0;
}
}
class Test{
public static void Main()
{
greaternoida gnoida;
gnoida=new greaternoida();
gnoida.display();
noida nda=new noida();
nda.display();
society sct=new society();
sct.display();
}
}
output:
Provides plot for industrial and residential purpose
Noida also provides plot for industrial and residential development
Society only provides plot for residential development
This program has demonstrated how the virtual function display was overriden using the same display function in the inherited class.
Conclusion
This language interoperability is one of the highest features of the common language run time (CLR).