diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2020-10-18 00:11:25 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2020-10-18 00:24:13 +0300 |
commit | 3eec1310829f6ec7f3c40a35d5991b0ebbf273b5 (patch) | |
tree | ce8257ca7e35f0471bbc1fd67acd8a1c75969ec9 /src | |
parent | process_tests: add stdin redirection test (diff) | |
download | winapi-common-3eec1310829f6ec7f3c40a35d5991b0ebbf273b5.tar.gz winapi-common-3eec1310829f6ec7f3c40a35d5991b0ebbf273b5.zip |
Process: add methods to load resource strings
Diffstat (limited to '')
-rw-r--r-- | src/process.cpp | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/src/process.cpp b/src/process.cpp index f95bc56..d3ce3a4 100644 --- a/src/process.cpp +++ b/src/process.cpp @@ -6,14 +6,18 @@ #include <winapi/cmd_line.hpp> #include <winapi/error.hpp> #include <winapi/process.hpp> +#include <winapi/resource.hpp> #include <winapi/utf8.hpp> #include <boost/config.hpp> #include <windows.h> +#include <cstddef> #include <cstring> +#include <sstream> #include <stdexcept> +#include <string> #include <utility> #include <vector> @@ -110,4 +114,52 @@ int Process::get_exit_code() const { return static_cast<int>(ec); } +HMODULE Process::get_exe_module() { + const auto module = ::GetModuleHandleW(NULL); + if (module == NULL) { + throw error::windows(GetLastError(), "GetModuleHandleW"); + } + return module; +} + +std::string Process::get_resource_string(unsigned int id) { + wchar_t* s = nullptr; + + const auto nch = ::LoadStringW(get_exe_module(), id, reinterpret_cast<wchar_t*>(&s), 0); + + if (nch <= 0) { + throw error::windows(GetLastError(), "LoadStringW"); + } + + return narrow(s, nch * sizeof(wchar_t)); +} + +Resource Process::get_resource(unsigned int id) { + const auto module = get_exe_module(); + + const auto src = ::FindResourceA(module, MAKEINTRESOURCEA(id), RT_RCDATA); + + if (src == NULL) { + throw error::windows(GetLastError(), "FindResourceA"); + } + + const auto resource = ::LoadResource(module, src); + + if (resource == NULL) { + throw error::windows(GetLastError(), "LoadResource"); + } + + const auto data = ::LockResource(resource); + + if (data == NULL) { + std::ostringstream oss; + oss << "Couldn't get data pointer for resource with ID " << id; + throw std::runtime_error{oss.str()}; + } + + const auto nb = ::SizeofResource(module, src); + + return {data, nb}; +} + } // namespace winapi |