Tuesday 16 October 2012

How do I get DllImport to work?

Reference System.Runtime.InteropServices. Mark all methods with the DllImport attribute as public static extern.

using System;
using System.Runtime.InteropServices;
 
class Example
{
   // Use DllImport to import the Win32 MessageBox function.
 
   [DllImport ("user32.dll", CharSet = CharSet.Auto)]
   public static extern int MessageBox 
      (IntPtr hWnd, String text, String caption, uint type);
 
   static void Main()
   {
      // Call the MessageBox function using platform invoke.
      MessageBox (new IntPtr(0), "Hello, World!", "Hello Dialog", 0);    
   }
}

No comments :