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).

AddThis

Related podcasts

  • CodeCast Episode 4: State of .NET, IE8, ASP.NET MVC, and O'Reilly Media

    CodeCast Episode 4: State of .NET, IE8, ASP.NET MVC, and O'Reilly MediaHosts Ken Levy and Markus Egger discuss the new State of .NET events, IE8, ASP.NET MVC, followed by an interview from PDC with two editors from O'Reilly Media. More on ASP.NET MVC can be found at http://asp.net/mvc. Interview...

Related jobs

Events coming up

  • Dec 6

    Developing AJAX Web Applications with Castle Monorail

    London, United Kingdom

    Monorail is the model-view-controller engine of the Castle Project, bringing many of the best ideas of Ruby on Rails to the .NET world. In this talk, David De Florinier and Gojko Adzic show how Monorail makes it easy to develop .NET based AJAX applications, and how to use the Castle Project to build Web 2.0 applications effectively. Come to this session if you are a .NET web developer. Everyone is welcome!