diff --git a/src/flexasio/FlexASIO/CMakeLists.txt b/src/flexasio/FlexASIO/CMakeLists.txt index caf3337..5e7e72b 100644 --- a/src/flexasio/FlexASIO/CMakeLists.txt +++ b/src/flexasio/FlexASIO/CMakeLists.txt @@ -44,7 +44,6 @@ add_library(FlexASIO_control_panel STATIC EXCLUDE_FROM_ALL control_panel.cpp) target_link_libraries(FlexASIO_control_panel PRIVATE FlexASIO_log PRIVATE FlexASIOUtil_windows_com - PRIVATE FlexASIOUtil_windows_error PRIVATE FlexASIOUtil_windows_registry PRIVATE FlexASIOUtil_windows_string PRIVATE dechamps_CMakeUtils_version diff --git a/src/flexasio/FlexASIO/control_panel.cpp b/src/flexasio/FlexASIO/control_panel.cpp index cd89d77..a66a671 100644 --- a/src/flexasio/FlexASIO/control_panel.cpp +++ b/src/flexasio/FlexASIO/control_panel.cpp @@ -3,7 +3,6 @@ #include "control_panel.h" #include "../FlexASIOUtil/windows_com.h" -#include "../FlexASIOUtil/windows_error.h" #include "../FlexASIOUtil/windows_registry.h" #include "../FlexASIOUtil/windows_string.h" @@ -14,6 +13,8 @@ #include +#include + namespace flexasio { namespace { @@ -34,7 +35,7 @@ namespace flexasio { Log() << "Executing: " << ConvertToUTF8(file); if (!::ShellExecuteW(windowHandle, NULL, file.c_str(), NULL, NULL, SW_SHOWNORMAL)) - throw std::runtime_error("Execution failed: " + GetWindowsErrorString(::GetLastError())); + throw std::system_error(::GetLastError(), std::system_category(), "ShellExecuteW failed"); } std::wstring GetStringRegistryValue(HKEY registryKey, LPCWSTR valueName) { @@ -49,7 +50,7 @@ namespace flexasio { value.resize(valueSize); continue; } - if (regQueryValueError != ERROR_SUCCESS) throw std::runtime_error("Unable to query string registry value: " + GetWindowsErrorString(regQueryValueError)); + if (regQueryValueError != ERROR_SUCCESS) throw std::system_error(regQueryValueError, std::system_category(), "Unable to query string registry value"); Log() << "Registry value size: " << valueSize; if (valueType != REG_SZ) throw std::runtime_error("Expected string registry value type, got " + std::to_string(valueType)); value.resize(valueSize); @@ -67,7 +68,7 @@ namespace flexasio { UniqueHKEY OpenFlexAsioGuiInstallRegistryKey() { HKEY registryKey; const auto regOpenKeyError = ::RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"Software\\Fabrikat\\FlexASIOGUI\\Install", {}, KEY_QUERY_VALUE | KEY_WOW64_64KEY, ®istryKey); - if (regOpenKeyError != ERROR_SUCCESS) throw std::runtime_error("Unable to open FlexASIOGUI registry key: " + GetWindowsErrorString(regOpenKeyError)); + if (regOpenKeyError != ERROR_SUCCESS) throw std::system_error(regOpenKeyError, std::system_category(), "Unable to open FlexASIOGUI registry key"); return UniqueHKEY(registryKey); } diff --git a/src/flexasio/FlexASIOUtil/CMakeLists.txt b/src/flexasio/FlexASIOUtil/CMakeLists.txt index f41a798..1d0651a 100644 --- a/src/flexasio/FlexASIOUtil/CMakeLists.txt +++ b/src/flexasio/FlexASIOUtil/CMakeLists.txt @@ -7,18 +7,7 @@ target_link_libraries(FlexASIOUtil_portaudio add_library(FlexASIOUtil_shell STATIC shell.cpp) add_library(FlexASIOUtil_windows_com STATIC windows_com.cpp) -target_link_libraries(FlexASIOUtil_windows_com - PRIVATE FlexASIOUtil_windows_error -) - -add_library(FlexASIOUtil_windows_error STATIC windows_error.cpp) add_library(FlexASIOUtil_windows_registry STATIC windows_registry.cpp) -target_link_libraries(FlexASIOUtil_windows_registry - PRIVATE FlexASIOUtil_windows_error -) add_library(FlexASIOUtil_windows_string STATIC windows_string.cpp) -target_link_libraries(FlexASIOUtil_windows_string - PRIVATE FlexASIOUtil_windows_error -) diff --git a/src/flexasio/FlexASIOUtil/windows_com.cpp b/src/flexasio/FlexASIOUtil/windows_com.cpp index 5594149..ea188ab 100644 --- a/src/flexasio/FlexASIOUtil/windows_com.cpp +++ b/src/flexasio/FlexASIOUtil/windows_com.cpp @@ -1,15 +1,14 @@ #include "windows_com.h" -#include "windows_error.h" - #include +#include namespace flexasio { COMInitializer::COMInitializer(DWORD coInit) { const auto coInitializeResult = CoInitializeEx(NULL, coInit); if (FAILED(coInitializeResult)) - throw std::runtime_error("Failed to initialize COM: " + GetWindowsErrorString(coInitializeResult)); + throw std::system_error(coInitializeResult, std::system_category(), "Failed to initialize COM"); } COMInitializer::~COMInitializer() { diff --git a/src/flexasio/FlexASIOUtil/windows_error.cpp b/src/flexasio/FlexASIOUtil/windows_error.cpp deleted file mode 100644 index b551734..0000000 --- a/src/flexasio/FlexASIOUtil/windows_error.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include "windows_error.h" - -#include - -namespace flexasio { - - std::string GetWindowsErrorString(DWORD error) { - std::string message(4096, 0); - auto messageSize = ::FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, error, 0, message.data(), DWORD(message.size()), NULL); - if (messageSize <= 0 || messageSize >= message.size()) { - message = "failed to format error message - result " + std::to_string(messageSize) + ", error " + std::to_string(GetLastError()) + ")"; - } - else { - for (; messageSize > 0 && isspace(static_cast(message[messageSize - 1])); --messageSize); - message.resize(messageSize); - } - - return "Windows error code " + std::to_string(error) + " \"" + message + "\""; - } - -} \ No newline at end of file diff --git a/src/flexasio/FlexASIOUtil/windows_error.h b/src/flexasio/FlexASIOUtil/windows_error.h deleted file mode 100644 index f088a0f..0000000 --- a/src/flexasio/FlexASIOUtil/windows_error.h +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once - -#include - -#include - -namespace flexasio { - - std::string GetWindowsErrorString(DWORD error); - -} diff --git a/src/flexasio/FlexASIOUtil/windows_registry.cpp b/src/flexasio/FlexASIOUtil/windows_registry.cpp index e54cbb2..d408c7d 100644 --- a/src/flexasio/FlexASIOUtil/windows_registry.cpp +++ b/src/flexasio/FlexASIOUtil/windows_registry.cpp @@ -1,15 +1,14 @@ #include "windows_registry.h" -#include "windows_error.h" - #include +#include namespace flexasio { void HKEYDeleter::operator()(HKEY hkey) const { const auto regCloseKeyError = ::RegCloseKey(hkey); if (regCloseKeyError != ERROR_SUCCESS) - throw std::runtime_error("Unable to close registry key: " + GetWindowsErrorString(regCloseKeyError)); + throw std::system_error(regCloseKeyError, std::system_category(), "Unable to close registry key"); } } diff --git a/src/flexasio/FlexASIOUtil/windows_string.cpp b/src/flexasio/FlexASIOUtil/windows_string.cpp index 6329738..c91d6fc 100644 --- a/src/flexasio/FlexASIOUtil/windows_string.cpp +++ b/src/flexasio/FlexASIOUtil/windows_string.cpp @@ -1,10 +1,9 @@ #include "windows_string.h" -#include "windows_error.h" - #include #include +#include namespace flexasio { @@ -12,11 +11,11 @@ namespace flexasio { if (input.size() == 0) return {}; const auto size = ::WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, input.data(), static_cast(input.size()), NULL, 0, NULL, NULL); - if (size <= 0) throw std::runtime_error("Unable to get size for string conversion to UTF-8: " + GetWindowsErrorString(::GetLastError())); + if (size <= 0) throw std::system_error(::GetLastError(), std::system_category(), "Unable to get size for string conversion to UTF-8"); std::string result(size, 0); if (::WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, input.data(), int(input.size()), result.data(), int(result.size()), NULL, NULL) != int(result.size())) - throw std::runtime_error("Unable to convert string to UTF-8: " + GetWindowsErrorString(::GetLastError())); + throw std::system_error(::GetLastError(), std::system_category(), "Unable to convert string to UTF-8"); return result; } @@ -24,11 +23,11 @@ namespace flexasio { if (input.size() == 0) return {}; const auto size = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, input.data(), int(input.size()), NULL, 0); - if (size <= 0) throw std::runtime_error("Unable to get size for string conversion from UTF-8: " + GetWindowsErrorString(::GetLastError())); + if (size <= 0) throw std::system_error(::GetLastError(), std::system_category(), "Unable to get size for string conversion from UTF-8"); std::wstring result(size, 0); if (::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, input.data(), int(input.size()), result.data(), int(result.size())) != int(result.size())) - throw std::runtime_error("Unable to convert string from UTF-8: " + GetWindowsErrorString(::GetLastError())); + throw std::system_error(::GetLastError(), std::system_category(), "Unable to convert string from UTF-8"); return result; }