39 lines
1.2 KiB
C++
39 lines
1.2 KiB
C++
#pragma once
|
|
|
|
namespace pe
|
|
{
|
|
template < typename _pt, typename _nth >
|
|
__forceinline _pt* get_data_directory_ptr(uintptr_t base, _nth nt_hdr, int index)
|
|
{
|
|
const auto offset =
|
|
nt_hdr->OptionalHeader.DataDirectory[index].VirtualAddress;
|
|
|
|
return reinterpret_cast<_pt*>((base + offset));
|
|
}
|
|
|
|
template < typename _nth >
|
|
__forceinline bool get_data_directory_info(uintptr_t base, _nth nt_hdr, int index, IMAGE_DATA_DIRECTORY& out)
|
|
{
|
|
out = nt_hdr->OptionalHeader.DataDirectory[index];
|
|
|
|
return true;
|
|
}
|
|
|
|
extern bool get_headers(void* image, _pimage_dos_header* out_dh, _pimage_nt_header* out_nh);
|
|
extern uintptr_t find_export_address(uintptr_t base, _pimage_dos_header dos_hdr, _pimage_nt_header nt_hdr, const char* exp_name);
|
|
|
|
template < typename T >
|
|
__forceinline auto find_export(void* image, const char* name)
|
|
{
|
|
const auto base = mem::addr(image).Base();
|
|
|
|
_pimage_dos_header dh = nullptr;
|
|
_pimage_nt_header nh = nullptr;
|
|
|
|
if (!get_headers(image, &dh, &nh))
|
|
return T();
|
|
|
|
return reinterpret_cast<T>(find_export_address(base, dh, nh, name));
|
|
}
|
|
}
|