using System.Runtime.InteropServices;static extern 方法,標註 [DllImport("dll名稱", EntryPoint="函數名", ...)]。using System;
using System.Runtime.InteropServices;
namespace NetMeeting.API
{
public class Win32
{
[DllImport("user32.dll", EntryPoint = "MessageBox", ExactSpelling = false)]
public static extern int MessageBox(int hWnd, string text, string caption, uint type);
}
}
// 呼叫端
using System;
using NetMeeting.API;
class Test
{
public static void Main(string[] args)
{
Win32.MessageBox(0, "hello, this is a c# invoke win32 api", "test", 2);
}
}
hWnd:視窗控制代碼,0 表示無擁有者。text:訊息內容。caption:標題列文字。type:按鈕/圖示組合(如 2 = Abort/Retry/Ignore + 警告圖示)。複雜結構體、回呼函數、字串編碼(CharSet)、SetLastError 等將於後續文章討論。