Library tutorials & articles

COM Interoperability in .NET Part 1

The .NET Code

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;
    }
  }
}

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.

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!