Additionally, updates are easier to apply to each module without affecting other parts of the program. For example, you may have a payroll program, and the tax rates change each year. When these changes are isolated to a DLL, you can apply an update without needing to build or install the whole program again.app
BOOL APIENTRY DllMain( HANDLE hModule,// Handle to DLL module DWORD ul_reason_for_call,// Reason for calling function LPVOID lpReserved ) // Reserved { switch ( ul_reason_for_call ) { case DLL_THREAD_ATTACH: // A process is loading the DLL. break; case DLL_THREAD_DETACH: // A process is creating a new thread. break; case DLL_PROCESS_ATTACH: // A thread exits normally. break; case DLL_PROCESS_DETACH: // A process unloads the DLL. break; } return TRUE; }
__declspec(dllexport)
__declspec(dllimport)
LIBRARY "DLL" EXPORTS HelloWorld
// dllmain.cpp : Defines the entry point for the DLL application. #include "stdafx.h" #include "DLL.h" #define EXPORTING_DLL BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { return TRUE; } void HelloWorld() { MessageBox( NULL, TEXT("Hello World"), TEXT("In a DLL"), MB_OK); }
#ifndef INDLL_H #define INDLL_H #ifdef EXPORTING_DLL extern __declspec(dllexport) void HelloWorld() ; #else extern __declspec(dllimport) void HelloWorld() ; #endif #endif
// DLLTest.cpp : Defines the entry point for the console application. // #include "stdafx.h" int _tmain(int argc, _TCHAR* argv[]) { HelloWorld(); return 0; }
// stdafx.h : include file for standard system include files, // or project specific include files that are used frequently, but // are changed infrequently // #pragma once #include "targetver.h" #include "DLL.h" #include <stdio.h> #include <tchar.h> #pragma comment(lib,"DLL.lib") // TODO: reference additional headers your program requires here
// DLLTest.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <Windows.h> typedef VOID (*DLLPROC) (); int _tmain(int argc, _TCHAR* argv[]) { HINSTANCE hinstDLL; DLLPROC HelloWorld; BOOL fFreeDLL; hinstDLL = LoadLibrary("DLL.dll"); if (hinstDLL != NULL) { HelloWorld = (DLLPROC) GetProcAddress(hinstDLL, "HelloWorld"); if (HelloWorld!=NULL) { HelloWorld(); } fFreeDLL = FreeLibrary(hinstDLL); } getchar(); ///HelloWorld(); return 0; }
參考地址:https://support.microsoft.com/en-us/help/815065/what-is-a-dllide