windows 核心编程DLL 高级技术-延迟载入dll,02
延迟载入DLL—测试程序
`
演示延迟载入DLL—测试程序
文章目录
- 延迟载入DLL---测试程序
- 演示延迟载入DLL---测试程序
演示延迟载入DLL—测试程序
/*------------------------------------------------------------------------20_ImportDelay.cpp演示延迟载入DLL---测试程序
-----------------------------------------------------------------------*/
#include <windows.h>
#include <tchar.h>
#include <strsafe.h>
#include <locale.h>
#include <Psapi.h> //For EnumProcessModules函数
#include "ExportDelay.h"//#pragma comment(lib,"psapi")
#pragma comment(lib,"20-Export-ImportDelay.lib")
//1、延迟加载是针对Dll的隐式链接的
//2、为了延迟加载Dll,还需要在解决方案的该项目“属性”->“配置属性”->
//“链接器”->“输入”->“延迟加载的Dll”中输入20_ImportDelay.dllvoid PrintModules(DWORD dwProcessID) {HMODULE* phMods = NULL;HANDLE hProcess = NULL;DWORD dwNeeded = 0;TCHAR szModName[MAX_PATH] = {};hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,FALSE, dwProcessID);if (NULL == hProcess) {_tprintf(_T("不能打开进程[ID:0x%X],错误码:[%u]\n"), dwProcessID,GetLastError());return;}//检索指定进程中每个模块的句柄EnumProcessModules(hProcess, NULL, 0, &dwNeeded);//获取所需字节数phMods = (HMODULE*)malloc(dwNeeded);if (EnumProcessModules(hProcess, phMods, dwNeeded, &dwNeeded)) {for (DWORD i = 0; i < (dwNeeded / sizeof(HMODULE)); i++) {ZeroMemory(szModName, MAX_PATH * sizeof(TCHAR));if (GetModuleFileNameEx(hProcess, phMods[i], szModName, MAX_PATH)) {_tprintf(_T("\t(0x%08X)\t%s\n"), (UINT)phMods[i], szModName);}}}free(phMods);CloseHandle(hProcess);
}int _tmain() {_tsetlocale(LC_ALL, _T("chs"));//显示进程中己加载的模块(此时不含(ExportDelay.dll)PrintModules(GetCurrentProcessId());_tsystem(_T("PAUSE"));int iVal1 = 10;int iVal2 = 20;int iVal3 = 30;_tprintf(_T("Func_A(%d)=%d\n"), iVal1, Func_A(iVal1));_tprintf(_T("Func_B(%d,%d)=%d\n"), iVal1, iVal2, Func_B(iVal1, iVal2));_tprintf(_T("Func_C(%d,%d,%d)=%d\n"), iVal1, iVal2, iVal3, Func_C(iVal1, iVal2, iVal3));PrintModules(GetCurrentProcessId());_tsystem(_T("PAUSE"));return 0;
}