Library tutorials & articles
COM Interoperability in .NET Part 1
- Introduction
- The .NET and COM Mediator
- The .NET Code
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.
Related articles
Related discussion
-
Unable to access AxInterop.AcoPdflib.dll on 64 bit OS
by Shaila14041981 (0 replies)
-
hey developers out there
by pitsophera (0 replies)
-
Capture a Screen Shot
by impalax (1 replies)
-
Selecting Multiple Line using Mouse Event?
by morizan (2 replies)
-
How can i develop opc server using .net?
by vairajaig (1 replies)
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
-
Dec
9
GL.net Group Meeting - December 2009
Gloucester, United Kingdom
The beginning of this year holiday season will belong to mocks. Ronnie and Stephen will take us for a tour around exciting world of unit testing.
This thread is for discussions of COM Interoperability in .NET Part 1.