make GetCurrentModulePath and GetShortcutPath throw win32 errors

This commit is contained in:
mjk 2021-03-05 22:39:08 +00:00 committed by LAGonauta
parent 2ad659f777
commit 5b40d166d2
3 changed files with 42 additions and 21 deletions

View file

@ -3,14 +3,35 @@
#include <glib.h>
#ifdef __cplusplus
#include <string>
#include <array>
#include <optional>
#include <memory>
#include <cstdint>
#include <exception>
#include <iterator>
std::optional<std::wstring> GetCurrentModulePath();
std::optional<std::wstring> 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<char,22+1> msg;
};
std::wstring GetCurrentModulePath();
std::wstring GetShortcutPath();
#define EXTERN extern "C"
#define NOEXCEPT noexcept

View file

@ -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"

View file

@ -5,7 +5,11 @@
#include "converter.hpp"
#include "ginvoke.hpp"
std::optional<std::wstring> 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<std::wstring> GetCurrentModulePath()
exePath.resize(charWritten);
return exePath;
}
return std::nullopt;
throw win32_error{};
}
std::optional<std::wstring> 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<std::wstring> GetShortcutPath()
shortcutPath.resize(charWritten);
return shortcutPath;
}
return std::nullopt;
throw win32_error{};
}
bool ImplSetProcessAumid(const char *const aumid)