libPeConv
A library to load, manipulate, dump PE files.
Loading...
Searching...
No Matches
resource_util.cpp
Go to the documentation of this file.
2
3#include "peconv/logger.h"
4
6{
7 HMODULE hMod = NULL;
8 GetModuleHandleExW(
9 GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
10 reinterpret_cast<LPCWSTR>(&peconv::get_current_module_handle),
11 &hMod);
12 return hMod;
13}
14
15peconv::ALIGNED_BUF peconv::load_resource_data(OUT size_t &out_size, int res_id, const LPSTR res_type, HMODULE hInstance)
16{
17 if (hInstance == nullptr) {
18 hInstance = GetModuleHandleA(NULL);
19 }
20 HRSRC res = FindResourceA(hInstance, MAKEINTRESOURCEA(res_id), res_type);
21 if (!res) {
22 LOG_ERROR("Cannot find resource.");
23 return nullptr;
24 }
25 HGLOBAL res_handle = LoadResource(hInstance, res);
26 if (res_handle == nullptr) {
27 LOG_ERROR("Cannot get resource handle.");
28 return nullptr;
29 }
30 BYTE* res_data = (BYTE*) LockResource(res_handle);
31 size_t r_size = static_cast<size_t>(SizeofResource(hInstance, res));
32 if (out_size != 0 && out_size <= r_size) {
33 r_size = out_size;
34 }
35
36 peconv::ALIGNED_BUF out_buf = peconv::alloc_aligned(r_size, PAGE_READWRITE);
37 if (out_buf != nullptr) {
38 memcpy(out_buf, res_data, r_size);
39 out_size = r_size;
40 } else {
41 out_size = 0;
42 }
43 FreeResource(res_handle);
44 return out_buf;
45}
46
#define LOG_ERROR(fmt,...)
Definition logger.h:36
bool free_aligned(ALIGNED_BUF buffer, size_t buffer_size=0)
PBYTE ALIGNED_BUF
Definition buffer_util.h:46
HMODULE get_current_module_handle()
ALIGNED_BUF alloc_aligned(size_t buffer_size, DWORD protect, void *desired_base=nullptr)
void free_resource_data(peconv::ALIGNED_BUF buffer)
peconv::ALIGNED_BUF load_resource_data(OUT size_t &out_size, const int res_id, const LPSTR res_type=RT_RCDATA_A, HMODULE hInstance=nullptr)
Functions related to manual retrieving of PE resources.