-
Notifications
You must be signed in to change notification settings - Fork 1
/
resource_buffer.cpp
65 lines (61 loc) · 1.7 KB
/
resource_buffer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// CEZEO software Ltd. https://www.cezeo.com
#include "resource_buffer.h"
resource_buffer::resource_buffer(const UINT resource_id, const std::wstring& resource_type, const HINSTANCE resource_instance)
{
HINSTANCE instance_handle = resource_instance;
if (NULL == instance_handle)
{
// get current exe instance handle
instance_handle = GetModuleHandle(NULL);
}
HRSRC resource = FindResource(instance_handle, MAKEINTRESOURCE(resource_id), resource_type.c_str());
if (NULL != resource)
{
size_t resource_size = SizeofResource(instance_handle, resource);
if (resource_size > 0)
{
HGLOBAL global_memory = LoadResource(instance_handle, resource);
if (NULL != global_memory)
{
uint8_t* resource_ptr = (uint8_t*)LockResource(global_memory);
if (NULL != resource_ptr)
{
if (NULL != resource_instance)
{
// allocate memory and copy data to it
buffer_data.set(resource_ptr, resource_size, buffer::TYPE::HOLDER);
}
else
{
// create keeper buffer without copying/allocation memory
buffer_data.set(resource_ptr, resource_size, buffer::TYPE::KEEPER);
}
}
else
{
throw std::runtime_error("can't lock resource handle");
}
}
else
{
throw std::runtime_error("can't load resource handle");
}
}
else
{
throw std::runtime_error("resource empty");
}
}
else
{
throw std::runtime_error("resource not found");
}
}
uint8_t* resource_buffer::data() const noexcept
{
return buffer_data.data();
}
size_t resource_buffer::size() const noexcept
{
return buffer_data.size();
}