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#ifdef _DEBUG
4#include <iostream>
5#endif
6
8{
9 HMODULE hMod = NULL;
10 GetModuleHandleExW(
11 GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
12 reinterpret_cast<LPCWSTR>(&peconv::get_current_module_handle),
13 &hMod);
14 return hMod;
15}
16
17peconv::ALIGNED_BUF peconv::load_resource_data(OUT size_t &out_size, int res_id, const LPSTR res_type, HMODULE hInstance)
18{
19 if (hInstance == nullptr) {
20 hInstance = GetModuleHandleA(NULL);
21 }
22 HRSRC res = FindResourceA(hInstance, MAKEINTRESOURCEA(res_id), res_type);
23 if (!res) {
24#ifdef _DEBUG
25 std::cerr << "Cannot find resource" << std::endl;
26#endif
27 return nullptr;
28 }
29 HGLOBAL res_handle = LoadResource(hInstance, res);
30 if (res_handle == nullptr) {
31#ifdef _DEBUG
32 std::cerr << "Cannot get resource handle" << std::endl;
33#endif
34 return nullptr;
35 }
36 BYTE* res_data = (BYTE*) LockResource(res_handle);
37 size_t r_size = static_cast<size_t>(SizeofResource(hInstance, res));
38 if (out_size != 0 && out_size <= r_size) {
39 r_size = out_size;
40 }
41
42 peconv::ALIGNED_BUF out_buf = peconv::alloc_aligned(r_size, PAGE_READWRITE);
43 if (out_buf != nullptr) {
44 memcpy(out_buf, res_data, r_size);
45 out_size = r_size;
46 } else {
47 out_size = 0;
48 }
49 FreeResource(res_handle);
50 return out_buf;
51}
52
bool free_aligned(ALIGNED_BUF buffer, size_t buffer_size=0)
ALIGNED_BUF alloc_aligned(size_t buffer_size, DWORD protect, ULONGLONG desired_base=NULL)
PBYTE ALIGNED_BUF
Definition buffer_util.h:46
HMODULE get_current_module_handle()
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.