標準C C 的DLL編寫

2021-04-01 06:15:52 字數 1045 閱讀 7412

dll也就是動態鏈結庫,使用dll程式設計的好處大家應當都知道了吧,可是怎麼樣來作呢,今天我就來說說。

首先,你要確定你要匯出那些個函式,然後你就在你要匯出的函式名前加上下面一句話:

// 輸出函式的字首

#define  dll_export   extern "c" __declspec( dllexport )

dll_export

void  exportfun

()是不是很簡單啊。如果你要匯出整個類或者全域性變數,你需要這樣做:

// 輸出類的字首

#define  dll_class_export   __declspec( dllexport )

// 輸出全域性變數的字首

#define  dll_global_export   extern __declspec( dllexport )

完成了這些以後,我們就要在主程式中呼叫這些個函式了,用下面的方法:

hinstance hinst = null;

hinst = loadlibrary("*.dll");        // 你的dll檔名

if (!hinst)

還記得上面我宣告的那個exportfun()函式嗎?我不能直接得到那個函式,但是可以把那個函式的位址取出來。其實函式位址使用起來和函式是一樣的。只不過,為了使用方便,需要定義乙個函式指標的型別。如果要指向上面的那個exportfun(),則它的函式指標的型別定義如下:

typedef void (callback* lpexportfun)(void)

之後需要做的是宣告乙個指標,然後得到dll中exportfun()的位址。getprocaddress函式的第乙個引數是之前得到的dll的例項控制代碼,後面乙個是dll中那個函式的函式名。

lpexportfun pfun = null;

lpexportfun pfun = (lpexportfun)getprocaddress(hinst, "exportfun");

好了,到這裡已經就要大功告成了,還差最後一步,呼叫那個函式:

pfun();

大功告成!!

標準的dll檔案編寫,簡單

標頭檔案 door.h extern c declspec dllexport int say something char sth door.cpp include include door.h int say something char sth messagebox null,sth hell...

標準動態dll編寫和呼叫

dll的編寫 include stdio.h include stdafx.h bool apientry dllmain handle hmodule,dword ul reason for call,lpvoid lpreserved case dll process detach 程序關閉時,...

DLL的編寫方法

以add 函式為例 一 建testdll 1 在標頭檔案testdll.h中 ifdef dll api else define dll api extern c declspec dllimport endif dll api int add int a,int b 2 在testdll.cpp檔...