dynamic link | static link | |
compile | with header | |
link | none | with import library(.lib) of dll |
load | app will load/unload dll | dll will be loaded on exe loaded. |
call | func pointer | using proto |
$ dumpbin /exports calc.dll Microsoft (R) COFF/PE Dumper Version 8.00.50727.762 Copyright (C) Microsoft Corporation. All rights reserved. Dump of file calc.dll File Type: DLL Section contains the following exports for calc.dll 00000000 characteristics 4829555F time date stamp Tue May 13 17:46:23 2008 0.00 version 1 ordinal base 2 number of functions 2 number of names ordinal hint RVA name 1 0 00001000 Add 2 1 0000100D Subtract Summary 1000 .bss 1000 .edata 1000 .idata 1000 .reloc 1000 .stab 1000 .stabstr 1000 .text $ dumpbin /imports test.exe
// mydll.h #ifndef __MYDLL_H__ #define __MYDLL_H__ // DLLAPI definition when creating .exe #ifndef DLLAPI #define DLLAPI extern "C" __declspec(dllimport) #endif #include <windows.h> DLLAPI BOOL Swap(int *lp1, int *lp2); #endif
// DLLAPI definition for creating .dll #define DLLAPI extern "C" __declspec(dllexport) #include "mydll.h" BOOL IsValidAddress(LPVOID lp1, LPVOID lp2) { return lp1 == NULL || lp2 == NULL; } DLLAPI BOOL Swap(int *lp1, int *lp2){ int tmp; if (IsValidAddress(lp1, lp2)) return FALSE; tmp = *lp1; *lp1 = *lp2; *lp2 = tmp; return TRUE; }
$(DLLTARGET).lib: $(DLLTARGET).dll $(DLLTARGET).dll: $(OBJS4DLL) #$(DLLTARGET).def link /DLL /OUT:$@ $(OBJS4DLL) $(LOPT4DLL) #/DEF:$(DLLTARGET).def
#ifdef __cplusplus extern "C" { #endif void __declspec(dllexport) __stdcall dll_func(int param); #ifdef __cplusplus } #endif
hInstance hdll; void (__stdcall * myfunc)(int param); hdll=LoadLibraryEx("mydll.dll",NULL,0); if(hdll==NULL) exit(-1); myfunc=(void (__stdcall *)(int))GetProcAddress(hdll,"myfunc"); myfunc(1); FreeLibrary(hdll);
BOOL WINAPI DllMain(HINSTANCE hdll,DWORD msg,LPVOID lpv){ switch(msg){ case DLL_PROCESS_ATTACH: case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: break; } }
#pragma data_seg("brabra") int counter=0; #pragma data_seg() InterlockedIncrement(&counter);