c++ - 为什么不是我可以加载(打开)的所有 DLL 模块?

标签 c++ c windows dll

我有 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/

相关文章:

c++ - C++函数的参数

c++ - 传递模板参数

c - 指针初始化无法识别定义的类型

c - Bresenham 的线图代码

c++ winapi listview项目已选中但未突出显示

windows - RunWait() 的错误代码值是什么意思?

c++ - 动态创建一个函数指针来调用给定实例上的方法

c - 两个不同程序中数据类型的复杂性有何区别?

c++ - 在 wxwidgets 中,如何锁定在 gui 线程和工作线程之间共享的 vector ?

c++ - 局部敏感哈希或 pHash?