Windows 核心编程之Dll 延时加载

来源:互联网 发布:李天生sql视频教程 编辑:程序博客网 时间:2024/05/24 01:17

DLL和Lib自己建立个工程,自己生成吧


代码是控制台的

#include <iostream>#include <map>#include <Windows.h>#include <tchar.h>#include <process.h>#include <delayimp.h>#include "../../dynamic_DLL/dynamic_DLL/dynamic_DLL.h"//系统的lib#pragma comment(lib,"delayimp.lib")//自定义的lib#pragma comment(lib,"dynamic_DLL.lib")using namespace std;FARPROC WINAPI delayHook(unsigned dliNotify, PDelayLoadInfo pdli){switch (dliNotify) {case dliStartProcessing :// If you want to return control to the helper, return 0.// Otherwise, return a pointer to a FARPROC helper function// that will be used instead, thereby bypassing the rest // of the helper.break;case dliNotePreLoadLibrary :// If you want to return control to the helper, return 0.// Otherwise, return your own HMODULE to be used by the // helper instead of having it call LoadLibrary itself.break;case dliNotePreGetProcAddress :// If you want to return control to the helper, return 0.// If you choose you may supply your own FARPROC function // address and bypass the helper's call to GetProcAddress.break;case dliFailLoadLib : // LoadLibrary failed.// If you don't want to handle this failure yourself, return 0.// In this case the helper will raise an exception // (ERROR_MOD_NOT_FOUND) and exit.// If you want to handle the failure by loading an alternate // DLL (for example), then return the HMODULE for // the alternate DLL. The helper will continue execution with // this alternate DLL and attempt to find the// requested entrypoint via GetProcAddress.break;case dliFailGetProc :// GetProcAddress failed.// If you don't want to handle this failure yourself, return 0.// In this case the helper will raise an exception // (ERROR_PROC_NOT_FOUND) and exit.// If you choose you may handle the failure by returning // an alternate FARPROC function address.break;case dliNoteEndProcessing : // This notification is called after all processing is done. // There is no opportunity for modifying the helper's behavior// at this point except by longjmp()/throw()/RaiseException. // No return value is processed.break;default :return NULL;}return NULL;}PfnDliHook   __pfnDliNotifyHook2 = delayHook ;PfnDliHook   __pfnDliFailureHook2 = delayHook ;void IsDllExist(LPCSTR lpName){HMODULE hInt = GetModuleHandleA(lpName);if (NULL == hInt){printf("no\n");}else{printf("yes\n");}}char chDllName[]="dynamic_DLL.dll";int main(){IsDllExist(chDllName);MyAdd(1,2);IsDllExist(chDllName);__FUnloadDelayLoadedDLL2(chDllName);IsDllExist(chDllName);MyAdd(2,4);IsDllExist(chDllName);getchar();return 0;}


下图是工程属性


设置它,支持动态卸载DLL



添加lib是为了编译程序link时能找到导出的函数,   Delay Loaded DLLs 表示哪些Dll支持延迟加载