Search This Blog

Tuesday, March 5, 2013

DllImportAttribute C#

C# > InteropServices > DllImportAttribute

Indicates that the attributed method is exposed by an unmanaged dynamic-link library (DLL) as a static entry point.

Example:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        // Import Win32 MessageBox function.
        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
        public static extern int MessageBox(IntPtr hWnd, String text, String caption, uint type);
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            // Call the MessageBox function with platform invoke.
            MessageBox(new IntPtr(0), "Text!", "Title", 0);
        }
    }
}