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

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.

We'd love to hear what you think! Submit ideas or give us feedback