Home »

How can I get at the Win32 API from a .NET program?

Question ListCategory: ASP.NETHow can I get at the Win32 API from a .NET program?
shah_kajal184 author asked 8 years ago
1 Answers
denielshakespeare5 author answered 8 years ago

Use P/Invoke. This uses similar technology to COM Interop, but is used toaccess static DLL entry points instead of COM objects. Here is an example of
C# calling the Win32 MessageBox function:
using System;
using System.Runtime.InteropServices;
class MainApp
{
[DllImport(“user32.dll”, EntryPoint=”MessageBox”, SetLastError=true,
CharSet=CharSet.Auto)]
public static extern int MessageBox(int hWnd, String strMessage, String strCaption, uint
uiType);
public static void Main()
{
MessageBox( 0, “Hello, this is PInvoke in operation!”, “.NET”, 0 );
}
}
Pinvoke.net is a great resource for off-the-shelf P/Invoke signatures.

Please login or Register to Submit Answer