我有 notepad.exe 路径,我需要输出 notepad.exe 使用(导入)的所有 DLL 模块和函数。
int InitWork()
{
LPCWSTR fileName = L"C:\\Windows\\System32\\notepad.exe";
PEinfo.handle = CreateFile(fileName, GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
/*....*/
PVOID pVirtual = VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE);
/*...*/
// Get pointer to headers
PEinfo. pNTHeader = (PIMAGE_NT_HEADERS)(PCHAR(pVirtual) + PEinfo.pDOSHead->e_lfanew);
PEinfo.pSech = IMAGE_FIRST_SECTION(PEinfo.pNTHeader);
PEinfo.OptHeader32 = (IMAGE_OPTIONAL_HEADER32) PEinfo.pNTHeader->OptionalHeader;
WCHAR* funcname = (wchar_t*)malloc(sizeof(wchar_t));
size_t i=0;
LPSTR libname = (char*)malloc(sizeof(char));
if(PEinfo.OptHeader32.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].Size != 0)
{
PEinfo.pImportDescriptor = (PIMAGE_IMPORT_DESCRIPTOR)((DWORD_PTR)pVirtual +\
Rva2Offset(PEinfo.OptHeader32.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress,PEinfo.pSech,PEinfo.pNTHeader));
printf("DLLName.FunctionName\n");
while(PEinfo.pImportDescriptor->Name != NULL)
{
//Get the name of each DLL
libname = (PCHAR)((DWORD_PTR)pVirtual + Rva2Offset(PEinfo.pImportDescriptor->Name,PEinfo.pSech,PEinfo.pNTHeader));
funcname = ANSItoUnicode(libname, funcname);
ImportFuncList(funcname);
PEinfo.pImportDescriptor++;
i++;
}
}
return 0;
}
/*Convert Virtual Address to File Offset */
DWORD Rva2Offset(DWORD rva,PIMAGE_SECTION_HEADER psh,PIMAGE_NT_HEADERS pnt)
{
size_t i = 0;
PIMAGE_SECTION_HEADER pSeh;
if(rva == 0)
{
return (rva);
}
pSeh = psh;
for(i = 0; i < pnt->FileHeader.NumberOfSections; i++)
{
if(rva >= pSeh->VirtualAddress && rva < pSeh->VirtualAddress +
pSeh->Misc.VirtualSize)
{
break;
}
pSeh++;
}
return (rva - pSeh->VirtualAddress + pSeh->PointerToRawData);
}
int ImportFuncList(LPWSTR dllName)
{
PEinfo.DLLModule = NULL;
PEinfo.DLLModule = GetModuleHandle(dllName);
if (PEinfo.DLLModule == NULL)
{
wprintf(L"Error Load %s\n", dllName);
return 1;
}
}
结果: ADVAPI32.dll。 KERNEL32.dll。 GDI32.dll。 USER32.dll。 msvcrt.dll。 错误加载 COMDLG32.dll 错误加载 SHELL32.dll 错误加载 WINSPOOL.DRV 错误加载 ole32.dll 错误加载 SHLWAPI.dll 错误加载 COMCTL32.dll 错误加载 OLEAUT32.dll ntdll.dll。 错误加载 VERSION.dll
什么?
为什么有些 DLL 加载了有些没有???
最佳答案
失败的模块只是还没有被加载。这样做,我预测您的代码将按预期工作:
LoadLibrary(dllName);
PEinfo.DLLModule = GetModuleHandle(dllName);
这不是您应该执行此操作的方式,但它表明 DLL 并非在程序启动时全部加载,如果它们尚未加载,GetModuleHandle
将失败。
关于c++ - 为什么不是我可以加载(打开)的所有 DLL 模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25419075/