資源描述:
《用c語(yǔ)言寫(xiě)動(dòng)態(tài)鏈接庫(kù)》由會(huì)員上傳分享,免費(fèi)在線(xiàn)閱讀,更多相關(guān)內(nèi)容在行業(yè)資料-天天文庫(kù)。
1、近來(lái)又有人在群里問(wèn)如何用c語(yǔ)言編制dll文件(動(dòng)態(tài)鏈接庫(kù))。原來(lái)沒(méi)有對(duì)這個(gè)問(wèn)題太在意過(guò),也沒(méi)有嘗試過(guò)任何解決方案,畢竟原來(lái)我是用vb的(現(xiàn)在用.net),做個(gè)dll只不過(guò)是點(diǎn)選一下建立activeX?dll工程的圖標(biāo)而已。今天在網(wǎng)上與朋友聊天,看了他指給我的幾個(gè)幾個(gè)文件,用MingW將C程序編譯成dll文件的例子,我恍然大悟,原來(lái)講C程序編譯成dll文件只不過(guò)是在要公開(kāi)的接口函數(shù)聲明前面加上幾個(gè)特定的修飾符而已。于是用dev-cpp建了個(gè)dll的默認(rèn)文檔,一切都很明了。(我把源代碼貼在下面)/*dll.h文件*/
2、#ifndef?_DLL_H_#define?_DLL_H_#if?BUILDING_DLL#?define?DLLIMPORT?__declspec?(dllexport)#else?/*?Not?BUILDING_DLL?*/#?define?DLLIMPORT?__declspec?(dllimport)#endif?/*?Not?BUILDING_DLL?*/DLLIMPORT?void?HelloWorld?(void);#endif?/*?_DLL_H_?*//*dllmain.c文件*//*?Rep
3、lace?"dll.h"?with?the?name?of?your?header?*/#include?"dll.h"#include?#include?#include?DLLIMPORT?void?HelloWorld?(){????MessageBox?(0,?"Hello?World?from?DLL!",?"Hi",?MB_ICONINFORMATION);}近來(lái)又有人在群里問(wèn)如何用c語(yǔ)言編制dll文件(動(dòng)態(tài)鏈接庫(kù))。原來(lái)沒(méi)有對(duì)這個(gè)問(wèn)題太
4、在意過(guò),也沒(méi)有嘗試過(guò)任何解決方案,畢竟原來(lái)我是用vb的(現(xiàn)在用.net),做個(gè)dll只不過(guò)是點(diǎn)選一下建立activeX?dll工程的圖標(biāo)而已。今天在網(wǎng)上與朋友聊天,看了他指給我的幾個(gè)幾個(gè)文件,用MingW將C程序編譯成dll文件的例子,我恍然大悟,原來(lái)講C程序編譯成dll文件只不過(guò)是在要公開(kāi)的接口函數(shù)聲明前面加上幾個(gè)特定的修飾符而已。于是用dev-cpp建了個(gè)dll的默認(rèn)文檔,一切都很明了。(我把源代碼貼在下面)/*dll.h文件*/#ifndef?_DLL_H_#define?_DLL_H_#if?BUILDI
5、NG_DLL#?define?DLLIMPORT?__declspec?(dllexport)#else?/*?Not?BUILDING_DLL?*/#?define?DLLIMPORT?__declspec?(dllimport)#endif?/*?Not?BUILDING_DLL?*/DLLIMPORT?void?HelloWorld?(void);#endif?/*?_DLL_H_?*//*dllmain.c文件*//*?Replace?"dll.h"?with?the?name?of?your?heade
6、r?*/#include?"dll.h"#include?#include?#include?DLLIMPORT?void?HelloWorld?(){????MessageBox?(0,?"Hello?World?from?DLL!",?"Hi",?MB_ICONINFORMATION);}BOOL?APIENTRY?DllMain?(HINSTANCE?hInst?????/*?Library?instance?handle.?*/?,?????
7、??????????????????DWORD?reason????????/*?Reason?this?function?is?being?called.?*/?,???????????????????????LPVOID?reserved?????/*?Not?used.?*/?){????switch?(reason)????{??????case?DLL_PROCESS_ATTACH:????????break;??????case?DLL_PROCESS_DETACH:????????break;???
8、???case?DLL_THREAD_ATTACH:????????break;??????case?DLL_THREAD_DETACH:????????break;????}????/*?Returns?TRUE?on?success,?FALSE?on?failure?*/????return?TRUE;}關(guān)于以上代碼的幾點(diǎn)解釋?zhuān)阂?、__declspec?(dllex