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
-
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)
-
need a help for multithreading in vb.net
by morizan (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
-
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.
This thread is for discussions of COM Interoperability in .NET Part 1.