COM Interoperability in C#
Introduction:
In this article I cover the areaInteroperability issues. There is no doubt that with the help of .Netone can create powerful components and Distributed applications thanany other language. But we have to think over about the past reusablecomponents, which were created by many languages such as VB etc.
Is it the Usage of those past components isend after evolving of .Net?No we can use those components in the .Net and the .Net types in theClassic COM clients. Are there any possibilities of communicationbetween managed and unmanaged types? Yes it is possible to make itpossible to use existing COM objects (Unmanaged) from within managedapplications and expose-managed objects to existing COM (Unmanaged)applications. Now let us see those things in detail.
In the first part of this article Part1, Ifocus on how .Net types calling c DLLs (Win32 API). In the Part2, Iillustrate how you can Build a .NET Server Callable from COM clientsand then in the latter Part-3, I will explain you that how you canBuild a .NET Client That Uses a COM Server. Confidently, at the end ofthe article, you'd have achieved enough information to understand howClassic COM and the .NET framework can peacefully co-exist together.Hence if you're geared up, let's take an expedition through travelaround how Classic COM can be used in the .NET world.
The .NET and COM Mediator
.NET runtime affords COM Interoperabilitywrapper for overcoming the differences between .NET and COMenvironments. For example, runtime make an instance of COM CallableWrapper (CCW) when a COM client accesses a .NET component. In the sameway, an instance of Runtime Callable Wrapper (RCW) is formed when a.NET client accesses a COM component. These wrappers abstract thedissimilarity and provide the faultless incorporation between the twoenvironments.
Part1–.NET TYPES CALLING C DLLs (Win32 API).
Platform Invoke:
The platform invoke services offers a methodto call functions that are exported from an unmanaged DLL. The mostdistinctive use of PInvoke is to allow .Net components to interact withthe Win32 API. PInvoke is also used to access functions exports definedin custom DLLs.
To exemplify the use of PInvoke I create a C#class that makes a call to theWin32 Message Box () function. Before wemove into the C# class let us see an example program in VB 6.
The C prototype :
int MessageBox(Hwnd hwnd,
LPCTSTR lpText,
LPCTSTR lpCaption,
UINT uType);
Parameters
hwnd —> Handle to the dialog's owner.
lpText —> The text you wish to display
lpCaption —> The caption of the message box.
wType —> The dialog definition. Ex: vbYesNo
Returns —> Returns the value of the button that was clicked.
VB Declaration:
Private Declare Function MessageBox Lib "user32" Alias "MessageBoxA"_(ByVal hwnd As Long, ByVal lpText As String, ByVal lpCaption As String,_ByVal wType As Long) As Long
Private Sub Command1_Click()
Dim ar As Long
ar = MessageBox(hwnd, "Msgbox using API", "Arungg", vbInformation)
End Sub
In the above example program, I show you howyou can use the Windows API in Vb 6.The Visual Basic MsgBox() functionactually wrap the MessageBox API. You don't need to use this API; infact, it would just be unnecessary work. However to understand theusage of API, I show you the above example.
Let us progress to how to call Win32 MessageBox() function from a C# class using PInvoke.
namespace APIExample
{
using System;
// Must refernce this library to use PI nvoke types
using System.Runtime.InteropServices;
public class PinvokeClient
{
[DllImport("user32")]
public static extern int MessageBox(int hWnd,
String pText ,
String pCaption ,
int uType);
public static int Main(string[] args)
{
String pText = "HELLO INDIA!!";
String pCaption = "Example by Arungg";
MessageBox(0,pText,pCaption,0);
return 0;
}
}
}
Explanation:
Before calling a C-style Dll we have todeclare the function to call using the static and extern C# keywords.After this you have to specify the name of the raw DLL that contain thefunction you are attempting to call,as shown here.
[DllImport("user32")]
public static extern int MessageBox(…….);
After declare the DLL Pass the arguments suchas pText,pCaption.It should be clear that it does not matter in whichorder you specify the values.
In the above way one can use .Net types calling any type of raw C DLLs(Win32 API).This comes to an end of Part1 and I think the users nowknow how to call a raw C DLLs (Win32 API) using PInvoke in .NET.This isan end of Part1.












No comments yet... Be the first to leave a reply!