Library tutorials & articles

COM Interoperability in .NET Part 1

Page 2 of 3
  1. Introduction
  2. The .NET and COM Mediator
  3. The .NET Code

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.

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.

Comments

  1. 01 Jan 1999 at 00:00

    This thread is for discussions of COM Interoperability in .NET Part 1.

Leave a comment

Sign in or Join us (it's free).

G.Gnana Arun Ganesh 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 Technologie...
AddThis

Related discussion

Related podcasts

  • More jQuery in ASP.NET

    In this episode Chris Brandsma, Rick Strahl, Dave Ward, Bertrand Le Roy, and Scott Koon conclude their discussion of Microsoft's jQuery in ASP.NET announcement1.This episode of the Alt.NET Podcast is brought to you by LLBLGen Pro, the most mature O/R mapper and code generator out there.Are ...

Events coming up

  • Nov 18

    15 Minutes of Fame

    Dresher, United States

    This is a yearly tradition. We select 10 of the favorite speakers from monthly meetings, code camps, and hands on labs. Each one does a 15 minute talk on their favorite .NET technology. This is our 10th anniversary so we plan a gala event with special prizes and refreshments.

Want to stay in touch with what's going on? Follow us on twitter!