Monday, October 26, 2009

Interoperating with Unmanaged Code Example-Create a Wrapper Class

To create a wrapper class, declare DLL functions within a class. Then define a static
method for each DLL function you want to call.

For example:


using System;
using System.Runtime.InteropServices;
class Win32MessageBox
{
[DllImport("user32.dll")]
private static extern int MessageBox(IntPtr hWnd, String text,
String caption, uint type);
public static void Show(string message, string caption)
{
MessageBox(new IntPtr(0), message, caption, 0);
}
}

class Program
{
static void Main(string[] args)
{
Win32MessageBox.Show("Hello, world!", "My box");
}
}

No comments: