新建專案 -> Console Application -> 選 DLL File


1. 在 DLL 檔裡面要先 Define 這行!
#define DLL extern "C" __declspec(dllexport)


2. 開始在 DLL 檔裡面寫 Function !!
DLL int sum(int c){  return ++c; }


--------------------------------------DLL 檔寫完了


如何呼叫 DLL 檔裡面的 Function ?


在一個MFC程式中..



HINSTANCE hinstDLL=LoadLibrary(L"theDLL.dll"); //讀取 DLL


if(hinstDLL!=0){  //如果有找到 DLL 檔


   typedef int(__cdecl *Connect)(int i); //先宣告一下要連結的函式的prototype
   Connect Proc; //宣告一下連結用的Connect指標 (應該是指標吧!?)
   Proc = (Connect)GetProcAddress(hinstDLL,"sum"); //用Proc連結 DLL 中的函式
   int get=Proc(2); //開始使用DLL中的函式,注意DLL中的函式名稱從sum變成Proc了
   FreeLibrary(hinstDLL); //DLL用完沒有利用價值了,就可以扔了


   CString g;
   g.Format((CString)"%d",get);   //這三行是顯示函式回傳的值,應該是2+1=3 才對
   MessageBox(g);


}
else{ printf("找不到你的DLL檔啦!你放在哪裡!?"); }


arrow
arrow
    全站熱搜

    finalfrank 發表在 痞客邦 留言(1) 人氣()