COM Interoperability in .NET Part 1
http://www.csharpfriends.com
World's Greatest C# Community    
Home Articles C# Forums Books C# Syntax C# Spec C# Jobs free Source Code Advertise About
 

Control Panel

[ Sign In / register ]
Points   
Notes 
My Forums
My Tutorials
My Profile

Resources

Learn
 Articles
 QuickStarts
 C# Spec
 Whitepapers
 Tools
 Class Browser
 C# Code Generator
 Links
 Misc Rss Feeds
 Code Highlight
 411 Directory
 FREE magazines
 freevb.net

Reviews
  ASP.NET Hosting

Source Code
 Get Version 1.0



C# Consulting
sql server forums
Chapter:   UnCategorized
Current Lesson:
COM Interoperability in .NET Part 1
[Latest Content]
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | ALL
[prev. Lesson]  Hello World - Win Forms [next Lesson]  COM Interoperability in .NET: Part 2
COM Interoperability in .NET Part 1
  by: ggarung

COM Interoperability in .NET Part 1

by: ggarung

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.

About the Author: 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 for technological support and queries through ggarung@rediffmail.com

1 


Build Your Own ASP.NET Website Using C# & VB.NET

Chapter:  UnCategorized
Current Lesson:
COM Interoperability in .NET Part 1
[Latest Content]
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | ALL
[prev. Lesson]  Hello World - Win Forms [next Lesson]  COM Interoperability in .NET: Part 2



Today's Top Movers
vulpes 3705
MadHatter 2051
jal 662
muster 622
George2 535

Yesterday Top Movers
shakti sin.. 9
MadHatter 3
Al_Pennywo.. 2
C#fanatic 2
simboy 1



Monthly Leaders
vulpes 3705
MadHatter 2091
jal 662
muster 622
George2 535

Top Members
mosessaur 18457
Rincewind 7074
stanleytan 6995
Gsuttie 6046
juliet 4679

Great Offers
.net hosting
Go To My Pc
Remote Pc Control
zonealarm
spam blocker
web hosting directory
ad server   C#
snadtech GoToMyPc

Top of Page

Advertise | About | Link To Us | Privacy Notice Copyright © 2003 - 2005 CSharpFriends.com  All Rights Reserved  Visual C# Developer Center