c++ - 如何在程序中查找数据段和代码段范围

标签 c++ c windows

我的老师要求我在 Windows 系统上执行此任务。 您能帮我通过程序找到一些数据段和代码段的地址范围吗?

最佳答案

这些 Windows API 可以帮助您。

//store the base address the loaded Module
dllImageBase = (char*)hModule; //suppose hModule is the handle to the loaded Module (.exe or .dll)

//get the address of NT Header
IMAGE_NT_HEADERS *pNtHdr = ImageNtHeader(hModule);

//after Nt headers comes the table of section, so get the addess of section table
IMAGE_SECTION_HEADER *pSectionHdr = (IMAGE_SECTION_HEADER *) (pNtHdr + 1);

ImageSectionInfo *pSectionInfo = NULL;

//iterate through the list of all sections, and check the section name in the if conditon. etc
for ( int i = 0 ; i < pNtHdr->FileHeader.NumberOfSections ; i++ )
{
     char *name = (char*) pSectionHdr->Name;
     if ( memcmp(name, ".data", 5) == 0 )
     {
          pSectionInfo = new ImageSectionInfo(".data");
          pSectionInfo->SectionAddress = dllImageBase + pSectionHdr->VirtualAddress;

          **//range of the data segment - something you're looking for**
          pSectionInfo->SectionSize = pSectionHdr->Misc.VirtualSize;
          break;
      }
      pSectionHdr++;
}

将 ImageSectionInfo 定义为,

struct ImageSectionInfo
{
      char SectionName[IMAGE_SIZEOF_SHORT_NAME];//the macro is defined WinNT.h
      char *SectionAddress;
      int SectionSize;
      ImageSectionInfo(const char* name)
      {
            strcpy(SectioName, name); 
       }
};

阅读纳瓦兹的 answer .

关于c++ - 如何在程序中查找数据段和代码段范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22588151/

相关文章:

c++ - 在 Qt 中如何从另一个文件中的函数访问小部件?

c++ - 对原子类 : memory_order_relaxed 感到困惑

c++ - 是否可以禁止取得文件的所有权?

正则表达式:将单词开头的 <tags> 中的小写字母替换为大写字母

c# - 如何为我的坐标系获取 "thinner"图?

java - 在 c++/c 中使用 JAR 文件

c - 在 C 中运行时检测库特性

c++ - bitwise not操作的编译器优化

windows - 无效和有效区域

c++ - 使用虚拟成员从类制作 POD