|
|
|
|
COM Interoperability in .NET
By
G Gnana Arun Ganesh May 08, 2004
In this article I cover the area Interoperability issues. There is no doubt that with the help of .Net one can create powerful components and Distributed applications than any other language. But we have to think over about the past reusable components, which were created by many languages such as VB etc.
|
Author Rank:
 |
|
Technologies:
.NET 1.0/1.1, .NET 1.0/1.1,Visual Basic .NET |
|
Total downloads : |
|
|
Total page views : |
7433 |
|
Rating : |
|
0/5 |
|
This article has been rated : |
0 times |
|
|
Similar ArticlesMost ReadTop RatedLatest
|
|
Related EbooksTop Videos
|
|
|
Description
|
|
The Complete Visual C# Programmer's Guide, written by the authors of C# Corner, covers most of the major components that make up C# and the .NETenvironment including Windows Forms, ADO.NET, GDI+, Web Services, and Security. The book is geared toward the beginner to intermediate programmers.
|
|
|
|
|
|
|
|
|
|
|
|
|
Introduction:
In this article I cover the area Interoperability issues. There is no doubt that with the help of .Net one can create powerful components and Distributed applications than any other language. But we have to think over about the past reusable components, which were created by many languages such as VB etc.
Is it the Usage of those past components is end after evolving of .Net?
No we can use those components in the .Net and the .Net types in the Classic COM clients. Are there any possibilities of communication between managed and unmanaged types? Yes it is possible to make it possible to use existing COM objects (Unmanaged) from within managed applications 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, I focus on how .Net types calling c DLLs (Win32 API). In the Part2, I illustrate how you can Build a .NET Server Callable from COM clients and then in the latter Part-3, I will explain you that how you can Build a .NET Client That Uses a COM Server. Confidently, at the end of the article, you'd have achieved enough information to understand how Classic COM and the .NET framework can peacefully co-exist together. Hence if you're geared up, let's take an expedition through travel around how Classic COM can be used in the .NET world.
The .NET and COM Mediator.
.NET runtime affords COM Interoperability wrapper for overcoming the differences between .NET and COM environments. For example, runtime make an instance of COM Callable Wrapper (CCW) when a COM client accesses a .NET component. In the same way, an instance of Runtime Callable Wrapper (RCW) is formed when a .NET client accesses a COM component. These wrappers abstract the dissimilarity and provide the faultless incorporation between the two environments.
Part1--.NET TYPES CALLING C DLLs (Win32 API).
Platform Invoke:
The platform invoke services offers a method to call functions that are exported from an unmanaged DLL. The most distinctive use of PInvoke is to allow .Net components to interact with the Win32 API. PInvoke is also used to access functions exports defined in custom DLLs.
To exemplify the use of PInvoke I create a C# class that makes a call to theWin32 Message Box () function. Before we move 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 how you can use the Windows API in Vb 6.The Visual Basic MsgBox() function actually wrap the MessageBox API. You don't need to use this API; in fact, it would just be unnecessary work. However to understand the usage 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 to declare 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 the function you are attempting to call,as shown here.
[DllImport("user32")]
public static extern int MessageBox(.......);
After declare the DLL Pass the arguments such as pText,pCaption.It should be clear that it does not matter in which order 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 now know how to call a raw C DLLs (Win32 API) using PInvoke in .NET.This is an end of Part1.
|
|
|
Login
to add your contents and source code to this article
|
|
|
|
|
|
|
|
|
|
|
|
G Gnana Arun Ganesh
G.GNANA ARUN GANESH, an ECE graduate (1997-2001) seeking a profession in either Software or Hardware Company. During Campus interview selected as a Software Engineer at GREEN MICRO SYSTEMS Chennai, a German collaboration company. Due to down stream in software market they have cancelled the offer. His skills includes VB, COM/DCOM, ASP, VB.NET, C# and Embedded systems. He is also a writer of numerous articles for many technical Web sites.
|
|
|
|
|
|
|
|
|
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional
consulting company, our consultants are well-known experts in .NET and many of them
are MVPs, authors, and trainers. We specialize in Microsoft .NET development and
utilize Agile Development and Extreme Programming practices to provide fast pace
quick turnaround results. Our software development model is a mix of Agile Development,
traditional SDLC, and Waterfall models.
|
|
Click here to learn more about C# Consulting. |
|
|
|
|
|
|
|
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon.
Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees.
As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
|
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
|
Boost the performance of your .NET applications
“ANTS Profiler took us straight to the specific areas of our code which were the cause of our performance issues." Terry Phillips, Sr. Developer, Harley-Davidson Dealer Systems. Download your free trial of ANTS Profiler.
|
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
|
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today. With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications. Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|