diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2016-10-20 03:20:49 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2016-10-20 03:20:49 +0300 |
commit | bd3116f43658fdd4ac145693e8ff4576061f22fa (patch) | |
tree | 93f0d7f0084a26b8db9de201323192a7711fbb0b | |
parent | code style & compiler warnings fixes (diff) | |
download | privilege-check-bd3116f43658fdd4ac145693e8ff4576061f22fa.tar.gz privilege-check-bd3116f43658fdd4ac145693e8ff4576061f22fa.zip |
refactoring
-rw-r--r-- | src/error.cpp | 35 | ||||
-rw-r--r-- | src/error.hpp | 23 | ||||
-rw-r--r-- | src/os.cpp | 24 | ||||
-rw-r--r-- | src/os.hpp | 12 | ||||
-rw-r--r-- | src/process.hpp | 9 | ||||
-rw-r--r-- | src/resource.cpp | 37 | ||||
-rw-r--r-- | src/resource.hpp | 21 |
7 files changed, 58 insertions, 103 deletions
diff --git a/src/error.cpp b/src/error.cpp deleted file mode 100644 index edce89f..0000000 --- a/src/error.cpp +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright (c) 2016 Egor Tensin <Egor.Tensin@gmail.com> -// This file is part of the "Privilege check" project. -// For details, see https://github.com/egor-tensin/privilege-check. -// Distributed under the MIT License. - -#include "error.hpp" - -#include <Windows.h> - -#include <exception> -#include <system_error> - -namespace error -{ - Error make(const char* function_name) - { - const auto ec = GetLastError(); - return {static_cast<int>(ec), std::system_category(), function_name}; - } - - void raise(const char* function_name) - { - throw make(function_name); - } - - void report(const std::exception& e) - { - MessageBoxA(NULL, e.what(), NULL, MB_OK); - } - - int get_code(const Error& e) - { - return e.code().value(); - } -} diff --git a/src/error.hpp b/src/error.hpp index b8ba468..40522bb 100644 --- a/src/error.hpp +++ b/src/error.hpp @@ -5,6 +5,8 @@ #pragma once +#include <Windows.h> + #include <exception> #include <system_error> @@ -12,11 +14,24 @@ typedef std::system_error Error; namespace error { - Error make(const char* function_name); + inline Error make(const char* function_name) + { + const auto ec = GetLastError(); + return {static_cast<int>(ec), std::system_category(), function_name}; + } - void raise(const char* function_name); + inline void raise(const char* function_name) + { + throw make(function_name); + } - void report(const std::exception&); + inline void report(const std::exception& e) + { + MessageBoxA(NULL, e.what(), NULL, MB_OK); + } - int get_code(const Error&); + inline int get_code(const Error& e) + { + return e.code().value(); + } } diff --git a/src/os.cpp b/src/os.cpp deleted file mode 100644 index 13fd766..0000000 --- a/src/os.cpp +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright (c) 2016 Egor Tensin <Egor.Tensin@gmail.com> -// This file is part of the "Privilege check" project. -// For details, see https://github.com/egor-tensin/privilege-check. -// Distributed under the MIT License. - -#include "error.hpp" -#include "os.hpp" - -#include <Windows.h> - -namespace os -{ - OSVERSIONINFOW get_version_info() - { - OSVERSIONINFOW info; - ZeroMemory(&info, sizeof(info)); - info.dwOSVersionInfoSize = sizeof(info); - - if (!GetVersionExW(&info)) - error::raise("GetVersionExW"); - - return info; - } -} @@ -11,7 +11,17 @@ namespace os { - OSVERSIONINFOW get_version_info(); + inline OSVERSIONINFOW get_version_info() + { + OSVERSIONINFOW info; + ZeroMemory(&info, sizeof(info)); + info.dwOSVersionInfoSize = sizeof(info); + + if (!GetVersionExW(&info)) + error::raise("GetVersionExW"); + + return info; + } inline bool is_vista_or_later(const OSVERSIONINFOW& info) { diff --git a/src/process.hpp b/src/process.hpp index c422aac..53fcdf8 100644 --- a/src/process.hpp +++ b/src/process.hpp @@ -5,6 +5,7 @@ #pragma once +#include "error.hpp" #include "cmd_line.hpp" #include <Windows.h> @@ -13,6 +14,14 @@ namespace process { + inline HMODULE load_exe_module() + { + const auto module = GetModuleHandle(NULL); + if (module == NULL) + error::raise("GetModuleHandle"); + return module; + } + std::wstring get_executable_path(); inline std::wstring get_command_line() diff --git a/src/resource.cpp b/src/resource.cpp deleted file mode 100644 index e8ac300..0000000 --- a/src/resource.cpp +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright (c) 2016 Egor Tensin <Egor.Tensin@gmail.com> -// This file is part of the "Privilege check" project. -// For details, see https://github.com/egor-tensin/privilege-check. -// Distributed under the MIT License. - -#include "error.hpp" -#include "resource.hpp" - -#include <Windows.h> - -#include <cstddef> - -#include <string> - -namespace resource -{ - HMODULE load_exe_module() - { - const auto module = GetModuleHandle(NULL); - if (module == NULL) - error::raise("GetModuleHandle"); - return module; - } - - std::wstring load_string(unsigned int id) - { - wchar_t* s = nullptr; - - const auto ret = LoadStringW( - load_exe_module(), id, reinterpret_cast<wchar_t*>(&s), 0); - - if (ret <= 0) - error::raise("LoadStringW"); - - return {s, static_cast<std::size_t>(ret)}; - } -} diff --git a/src/resource.hpp b/src/resource.hpp index 0e3186c..a0f1cc3 100644 --- a/src/resource.hpp +++ b/src/resource.hpp @@ -5,13 +5,30 @@ #pragma once +#include "error.hpp" +#include "process.hpp" + #include <Windows.h> +#include <cstddef> + #include <string> namespace resource { - HMODULE load_exe_module(); + inline std::wstring load_string(unsigned int id) + { + wchar_t* s = nullptr; + + const auto ret = LoadStringW( + process::load_exe_module(), + id, + reinterpret_cast<wchar_t*>(&s), + 0); + + if (ret <= 0) + error::raise("LoadStringW"); - std::wstring load_string(unsigned int id); + return {s, static_cast<std::size_t>(ret)}; + } } |