開發環境是DEV C++,采用C語言編寫
創建一個DLL項目,項目名稱hello,DLL編寫采用的是DEV C++中的示例代碼
頭文件dll.h
#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_ */
C文件
dllmain.c
#include <stdio.h>
#include <stdlib.h>
DLLIMPORT void HelloWorld ()
{
??? MessageBox (0, "Hello World from DLL!\n", "Hi", MB_ICONINFORMATION);
}
BOOL APIENTRY DllMain (HINSTANCE hInst???? /* Library instance handle. */ ,
?????????????????????? DWORD reason??????? /* Reason this function is being called. */ ,
?????????????????????? LPVOID reserved???? /* Not used. */ )
{
??? switch (reason)
??? {
????? case DLL_PROCESS_ATTACH:
??????? break;
????? case DLL_PROCESS_DETACH:
??????? break;
????? case DLL_THREAD_ATTACH:
??????? break;
????? case DLL_THREAD_DETACH:
??????? break;
??? }
??? /* Returns TRUE on success, FALSE on failure */
??? return TRUE;
}
還有要注意的在.def文件中指定輸出的函數,編譯生成了hello.dll文件
DLL調用部分
dllcall.c
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
HINSTANCE hDLL; //定義DLL包柄
typedef void ( *func)();??? //定義函數指針原型
func hello;? //定義函數指針
int main()
{
?if (hDLL == NULL)
??? hDLL=LoadLibrary("hello.dll");??//加載DLL
?hello = (func)GetProcAddress(hDLL,"HelloWorld"); //獲取函數指針
?hello();
?FreeLibrary(hDLL);? //釋放DLL
?return 0;
}
編譯執行

?