-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathImageResourceEnumerator.cpp
More file actions
32 lines (29 loc) · 1.41 KB
/
Copy pathImageResourceEnumerator.cpp
File metadata and controls
32 lines (29 loc) · 1.41 KB
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
#include "common.h"
#include "ImageResourceEnumerator.h"
BOOL __stdcall OnResourceNameFoundProxy(HMODULE moduleHandle, PCWSTR pszType, PWSTR pszName, LONG_PTR lParam)
{
return reinterpret_cast<eoraptor::ImageResourceEnumerator*>(lParam)->OnResourceNameFound(moduleHandle, pszType, pszName, lParam);
}
HRESULT eoraptor::ImageResourceEnumerator::EnumerateImageResourcesInFile(const std::wstring& path, const ImageResourceCallback& callback)
{
_callback = callback;
unique_module moduleHandle(LoadLibraryEx(path.c_str(), nullptr, LOAD_LIBRARY_AS_DATAFILE), FreeLibrary);
if (moduleHandle.get() == INVALID_HANDLE_VALUE)
{
throw std::system_error(std::error_code(GetLastError(), std::system_category()));
}
EnumResourceNamesEx(moduleHandle.get(), L"IMAGE", OnResourceNameFoundProxy, reinterpret_cast<LONG_PTR>(this), RESOURCE_ENUM_LN, 0);
return S_OK;
}
BOOL eoraptor::ImageResourceEnumerator::OnResourceNameFound(HMODULE moduleHandle, PCWSTR pszType, PWSTR lpszName, LONG_PTR lParam)
{
auto resourceHandle = FindResource(moduleHandle, lpszName, pszType);
if (resourceHandle == INVALID_HANDLE_VALUE)
{
throw std::system_error(std::error_code(GetLastError(), std::system_category()));
}
auto resourcePtr = LockResource(LoadResource(moduleHandle, resourceHandle));
_callback(resourcePtr, SizeofResource(moduleHandle, resourceHandle), reinterpret_cast<long>(lpszName));
FreeResource(resourceHandle);
return TRUE; // Continue
}