要使用user32.dll的MessageBox彈窗消息,自然需要引入user32.dll到項目中。
一個最簡單的實例如下:
using System; using System.Runtime.InteropServices; class Example { // Use DllImport to import the Win32 MessageBox function. [DllImport("user32.dll", CharSet = CharSet.Unicode)] 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); } }
運行項目后,直接彈出彈窗消息。
這是C#使用外部DLL中的方法,項目的引用中並沒有顯示該user32.dll。