關於C#中的DLLImport

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、環境變量目錄

 

 

 

 

 

 

 

 

 

 

 

發佈了43 篇原創文章 · 獲贊 3 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章