From 7d3e33a5466232d36369bda27df593cb61ce8a49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20H=C3=B6glinger-Stelzer?= Date: Wed, 7 Aug 2024 03:05:51 +0200 Subject: [PATCH] Reworked more functions --- src/Devcon.cpp | 33 +++++++++++---------------------- src/Devcon.h | 4 ++-- 2 files changed, 13 insertions(+), 24 deletions(-) diff --git a/src/Devcon.cpp b/src/Devcon.cpp index 94d37f4..1397cc7 100644 --- a/src/Devcon.cpp +++ b/src/Devcon.cpp @@ -398,10 +398,9 @@ bool devcon::enable_disable_bth_usb_device(bool state) return succeeded; } -bool devcon::install_driver(const std::wstring& fullInfPath, bool* rebootRequired) +std::expected devcon::install_driver(const std::wstring& fullInfPath, + bool* rebootRequired) { - el::Logger* logger = el::Loggers::getLogger("default"); - Newdev newdev; BOOL reboot; @@ -414,26 +413,20 @@ bool devcon::install_driver(const std::wstring& fullInfPath, bool* rebootRequire )) { case FunctionCallResult::NotAvailable: - logger->error("Couldn't find DiInstallDriverW export"); - SetLastError(ERROR_INVALID_FUNCTION); - return false; + return std::unexpected(Win32Error(ERROR_INVALID_FUNCTION)); case FunctionCallResult::Failure: - return false; + return std::unexpected(Win32Error(GetLastError())); case FunctionCallResult::Success: if (rebootRequired) - { *rebootRequired = reboot > 0; - } - return true; + return {}; } - return false; + return std::unexpected(Win32Error(ERROR_INTERNAL_ERROR)); } -bool devcon::uninstall_driver(const std::wstring& fullInfPath, bool* rebootRequired) +std::expected devcon::uninstall_driver(const std::wstring& fullInfPath, bool* rebootRequired) { - el::Logger* logger = el::Loggers::getLogger("default"); - Newdev newdev; BOOL reboot; @@ -446,20 +439,16 @@ bool devcon::uninstall_driver(const std::wstring& fullInfPath, bool* rebootRequi )) { case FunctionCallResult::NotAvailable: - logger->error("Couldn't find DiUninstallDriverW export"); - SetLastError(ERROR_INVALID_FUNCTION); - return false; + return std::unexpected(Win32Error(ERROR_INVALID_FUNCTION)); case FunctionCallResult::Failure: - return false; + return std::unexpected(Win32Error(GetLastError())); case FunctionCallResult::Success: if (rebootRequired) - { *rebootRequired = reboot > 0; - } - return true; + return {}; } - return false; + return std::unexpected(Win32Error(ERROR_INTERNAL_ERROR)); } bool devcon::add_device_class_filter(const GUID* classGuid, const std::wstring& filterName, diff --git a/src/Devcon.h b/src/Devcon.h index d070dcc..26f66a8 100644 --- a/src/Devcon.h +++ b/src/Devcon.h @@ -59,9 +59,9 @@ namespace devcon bool enable_disable_bth_usb_device(bool state); - bool install_driver(const std::wstring& fullInfPath, bool* rebootRequired); + std::expected install_driver(const std::wstring& fullInfPath, bool* rebootRequired); - bool uninstall_driver(const std::wstring& fullInfPath, bool* rebootRequired); + std::expected uninstall_driver(const std::wstring& fullInfPath, bool* rebootRequired); bool add_device_class_filter(const GUID* classGuid, const std::wstring& filterName, DeviceClassFilterPosition position);