C / C ++分配DWORD数组的指针地址

时间:2012-09-26 00:05:30

标签: c++ c winapi visual-c++ portable-executable

我正在编写一个C / C ++ PE解析库,我使用DLL或exe来提取有关目录和头文件的信息。我的问题是当我提取导出地址并获取函数的地址时,我不知道如何使用该地址将其指向具有导出导出函数数量的数组

DWORD ExportRVA = PEHeader->optional.data_directory[0].virtual_address;
image_export_directory* Exports = (image_export_directory*)(RVAToOffset(ExportRVA)+BaseAddress);

ExportTable.nNames = Exports->number_of_names;
ExportTable.nFunctions = Exports->number_of_functions;
ExportTable.pFunctions = Exports->address_of_functions;
ExportTable.nNames = Exports->address_of_names;
ExportTable.pNamesOrdinals = Exports->address_of_name_ordinals;

我是否必须指定一个指向数组的指针,如

DWORD * AddrFunctions;

更改指针地址?

1 个答案:

答案 0 :(得分:1)

address_of_functionsaddress_of_names字段分别是实际功能入口点和名称的RVAs数组的RVA,而address_of_name_ordinals字段是WORD值数组的RVA ,例如:

#define RVAToPtr(RVA) ( ((LPBYTE)BaseAddress) + ((DWORD)(RVA)) )

image_export_directory* Exports = (image_export_directory*) RVAToPtr(PEHeader->optional.data_directory[0].virtual_address); 

ExportTable.nFunctions = Exports->number_of_functions; 
ExportTable.nNames = Exports->number_of_names; 
ExportTable.pFunctions = (PDWORD) RVAToPtr(Exports->address_of_functions); 
ExportTable.pNames = (PDWORD) RVAToPtr(Exports->address_of_names); 
ExportTable.pNamesOrdinals = (PWORD) RVAToPtr(Exports->address_of_name_ordinals); 

for (DWORD i = 0; i < ExportTable.nFunctions; ++i)
{
    void *FuncPtr = (void*) RVAToPtr(ExportTable.pFunctions[i]);
    char* FuncName = (char*) RVAToPtr(ExportTable.pNames[i]);
    WORD FuncOrdinal = ExportTable.Base + ExportTable.pNamesOrdinals[i]; 
    ...
}

有关详细信息,请参阅MSDN