MSDN中對DllImportAttribute的解釋是這樣的:可將該屬性應用於方法。DllImportAttribute 屬性提供對從非託管 DLL 導出的函數進行調用所必需的信息。作爲最低要求,必須提供包含入口點的 DLL 的名稱。
[DllImport("user32.dll", EntryPoint = "MessageBox")]
public static extern int MessageBox(int h, string m, string c, int type);
static int Main(string[] args)
{
string myString;
Console.Write("Enter your message: ");
myString = Console.ReadLine();
return MessageBox(0, myString, "My Message Box", 0);
}
利用code::blocks生成DLL
C#引用C的DLL
DllImport會按照順序自動去尋找的地方:
1、exe所在目錄
2、System32目錄
3、環境變量目錄