获取加载到内存中时DLL或EXE的代码段的大小

Getting the size of the code segment of a DLL or EXE when loaded in memory

本文关键字:EXE 代码 DLL 加载 内存 获取      更新时间:2023-10-16

我可以使用GetModuleHandle来获取它的基偏移量,但我也需要知道DLL或EXE的代码段的大小。例如,我需要在以下代码中估计nSize:

char aCrashSignatureBytesAtEIP[] = { 0x87, 0x12, 0x00, ... };
char* pBaseOffset = (char*) GetModuleHandle(NULL);
int nSize = ???;
for (int i = 0; i<nSize; i++)
    if (!memcmp(&pBaseOffset[i], aCrashSignatureBytesAtEIP, sizeof(aCrashSignatureBytesAtEIP)))
    {
        printf("Crash signature found at offset %p", &pBaseOffset[i]);
    }

PSAPI为您提供以下信息:GetModuleInformation函数返回MODULEINFO结构,其中包含图像的基地址和大小。

typedef struct _MODULEINFO {
  LPVOID lpBaseOfDll;
  DWORD  SizeOfImage;
  LPVOID EntryPoint;
} MODULEINFO, *LPMODULEINFO;