diff --git a/plugins/windows-notification/api/include/win32.hpp b/plugins/windows-notification/api/include/win32.hpp index 79d9e610..48987791 100644 --- a/plugins/windows-notification/api/include/win32.hpp +++ b/plugins/windows-notification/api/include/win32.hpp @@ -3,14 +3,35 @@ #include #ifdef __cplusplus - #include -#include -#include -#include +#include +#include +#include -std::optional GetCurrentModulePath(); -std::optional GetShortcutPath(); +#include "make_array.hpp" +#include "hexify.hpp" + +struct win32_error : std::exception +{ + std::uint32_t code; + explicit win32_error() noexcept; // initializes with GetLastError() + explicit win32_error(const std::uint32_t code) noexcept + : code{code} + {} + const char *what() const noexcept override + { + // NOTE: thread-unsafe + // TODO: decimal representation seems to be more usual for win32 errors + msg = make_array("win32 error 0x01234567\0"); + hexify32(code, std::end(msg)-1); + return std::data(msg); + } +private: + mutable std::array msg; +}; + +std::wstring GetCurrentModulePath(); +std::wstring GetShortcutPath(); #define EXTERN extern "C" #define NOEXCEPT noexcept diff --git a/plugins/windows-notification/api/src/shortcutcreator.cpp b/plugins/windows-notification/api/src/shortcutcreator.cpp index e7e93168..8814bacc 100644 --- a/plugins/windows-notification/api/src/shortcutcreator.cpp +++ b/plugins/windows-notification/api/src/shortcutcreator.cpp @@ -136,19 +136,15 @@ bool ImplEnsureAumiddedShortcutExists(const char *const aumid) auto exePath = GetCurrentModulePath(); auto shortcutPath = GetShortcutPath(); - if (shortcutPath && exePath) + auto path = shortcutPath + LR"(\Microsoft\Windows\Start Menu\Programs\Dino.lnk)"; + if (!std::filesystem::exists(path)) { - auto path = shortcutPath.value() + LR"(\Microsoft\Windows\Start Menu\Programs\Dino.lnk)"; - if (!std::filesystem::exists(path)) - { - return SUCCEEDED(InstallShortcut(exePath.value(), waumid, path)); - } - else - { - return SUCCEEDED(ValidateShortcut(path, waumid)); - } + return SUCCEEDED(InstallShortcut(exePath, waumid, path)); + } + else + { + return SUCCEEDED(ValidateShortcut(path, waumid)); } - return false; } extern "C" diff --git a/plugins/windows-notification/api/src/win32.cpp b/plugins/windows-notification/api/src/win32.cpp index 8f1051b6..88f8eb0b 100644 --- a/plugins/windows-notification/api/src/win32.cpp +++ b/plugins/windows-notification/api/src/win32.cpp @@ -5,7 +5,11 @@ #include "converter.hpp" #include "ginvoke.hpp" -std::optional GetCurrentModulePath() +win32_error::win32_error() noexcept + : win32_error{::GetLastError()} +{} + +std::wstring GetCurrentModulePath() { std::wstring exePath(MAX_PATH, 0); auto charWritten = GetModuleFileName(nullptr, exePath.data(), exePath.size()); @@ -14,10 +18,10 @@ std::optional GetCurrentModulePath() exePath.resize(charWritten); return exePath; } - return std::nullopt; + throw win32_error{}; } -std::optional GetShortcutPath() +std::wstring GetShortcutPath() { std::wstring shortcutPath(MAX_PATH, 0); auto charWritten = GetEnvironmentVariable(L"APPDATA", shortcutPath.data(), shortcutPath.size()); @@ -26,7 +30,7 @@ std::optional GetShortcutPath() shortcutPath.resize(charWritten); return shortcutPath; } - return std::nullopt; + throw win32_error{}; } bool ImplSetProcessAumid(const char *const aumid)