diff options
Diffstat (limited to '')
-rw-r--r-- | um/service/src/device.cpp | 10 | ||||
-rw-r--r-- | um/service/src/service.cpp | 22 | ||||
-rw-r--r-- | um/service/src/service_manager.cpp | 5 | ||||
-rw-r--r-- | um/service/src/windows_error.cpp | 41 |
4 files changed, 33 insertions, 45 deletions
diff --git a/um/service/src/device.cpp b/um/service/src/device.cpp index 003c19d..870930f 100644 --- a/um/service/src/device.cpp +++ b/um/service/src/device.cpp @@ -12,7 +12,6 @@ #include <limits> #include <stdexcept> #include <string> -#include <system_error> namespace service { @@ -32,8 +31,7 @@ namespace service if (INVALID_HANDLE_VALUE == raw) { const auto ec = GetLastError(); - throw std::system_error( - ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); + throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); } return Handle(raw); @@ -75,8 +73,7 @@ namespace service return nbreq; default: - throw std::system_error( - ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); + throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); } } @@ -110,8 +107,7 @@ namespace service if (0 == nbwritten) { const auto ec = GetLastError(); - throw std::system_error( - ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); + throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); } return nbwritten; diff --git a/um/service/src/service.cpp b/um/service/src/service.cpp index 26cf35d..b7384ce 100644 --- a/um/service/src/service.cpp +++ b/um/service/src/service.cpp @@ -8,7 +8,6 @@ #include <Windows.h> #include <string> -#include <system_error> namespace service { @@ -26,8 +25,7 @@ namespace service if (NULL == raw) { const auto ec = GetLastError(); - throw std::system_error( - ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); + throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); } return raw; @@ -56,8 +54,7 @@ namespace service if (NULL == raw) { const auto ec = GetLastError(); - throw std::system_error( - ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); + throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); } return raw; @@ -68,8 +65,7 @@ namespace service if (!StartService(handle, 0, NULL)) { const auto ec = GetLastError(); - throw std::system_error( - ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); + throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); } } @@ -80,8 +76,7 @@ namespace service if (!ControlService(handle, SERVICE_CONTROL_STOP, &service_status)) { const auto ec = GetLastError(); - throw std::system_error( - ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); + throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); } } @@ -90,8 +85,7 @@ namespace service if (!DeleteService(handle)) { const auto ec = GetLastError(); - throw std::system_error( - ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); + throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); } } @@ -118,8 +112,7 @@ namespace service return false; default: - throw std::system_error( - ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); + throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); } } @@ -135,8 +128,7 @@ namespace service handle, SC_STATUS_PROCESS_INFO, buf_ptr, buf_size, &nbreq)) { const auto ec = GetLastError(); - throw std::system_error( - ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); + throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); } return status; diff --git a/um/service/src/service_manager.cpp b/um/service/src/service_manager.cpp index 58377ed..48c6bfe 100644 --- a/um/service/src/service_manager.cpp +++ b/um/service/src/service_manager.cpp @@ -7,8 +7,6 @@ #include <Windows.h> -#include <system_error> - namespace service { ServiceManager ServiceManager::open() @@ -18,8 +16,7 @@ namespace service if (NULL == raw) { const auto ec = GetLastError(); - throw std::system_error( - ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); + throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); } return ServiceHandle(raw); diff --git a/um/service/src/windows_error.cpp b/um/service/src/windows_error.cpp index 5b358ca..c228536 100644 --- a/um/service/src/windows_error.cpp +++ b/um/service/src/windows_error.cpp @@ -11,29 +11,32 @@ namespace service { - std::string WindowsErrorCategory::message(int code) const + namespace windows_error { - char* buf_ptr; + std::string Category::message(int code) const + { + char* buf_ptr; - const auto nbwritten = FormatMessageA( - FORMAT_MESSAGE_ALLOCATE_BUFFER - | FORMAT_MESSAGE_FROM_SYSTEM - | FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, - code, - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - reinterpret_cast<char*>(&buf_ptr), - 0, - NULL); + const auto nbwritten = FormatMessageA( + FORMAT_MESSAGE_ALLOCATE_BUFFER + | FORMAT_MESSAGE_FROM_SYSTEM + | FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, + code, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + reinterpret_cast<char*>(&buf_ptr), + 0, + NULL); - if (0 == nbwritten) - { + if (0 == nbwritten) + { + LocalFree(buf_ptr); + return "Couldn't format the error message"; + } + + std::string str(buf_ptr, nbwritten - 2); LocalFree(buf_ptr); - return "Couldn't format error message"; + return str; } - - std::string str(buf_ptr, nbwritten - 2); - LocalFree(buf_ptr); - return str; } } |