make GetCurrentModulePath and GetShortcutPath throw win32 errors
This commit is contained in:
parent
2ad659f777
commit
5b40d166d2
|
@ -3,14 +3,35 @@
|
||||||
#include <glib.h>
|
#include <glib.h>
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <array>
|
#include <cstdint>
|
||||||
#include <optional>
|
#include <exception>
|
||||||
#include <memory>
|
#include <iterator>
|
||||||
|
|
||||||
std::optional<std::wstring> GetCurrentModulePath();
|
#include "make_array.hpp"
|
||||||
std::optional<std::wstring> GetShortcutPath();
|
#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 EXTERN extern "C"
|
||||||
#define NOEXCEPT noexcept
|
#define NOEXCEPT noexcept
|
||||||
|
|
|
@ -136,19 +136,15 @@ bool ImplEnsureAumiddedShortcutExists(const char *const aumid)
|
||||||
auto exePath = GetCurrentModulePath();
|
auto exePath = GetCurrentModulePath();
|
||||||
auto shortcutPath = GetShortcutPath();
|
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)";
|
return SUCCEEDED(InstallShortcut(exePath, waumid, path));
|
||||||
if (!std::filesystem::exists(path))
|
}
|
||||||
{
|
else
|
||||||
return SUCCEEDED(InstallShortcut(exePath.value(), waumid, path));
|
{
|
||||||
}
|
return SUCCEEDED(ValidateShortcut(path, waumid));
|
||||||
else
|
|
||||||
{
|
|
||||||
return SUCCEEDED(ValidateShortcut(path, waumid));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C"
|
extern "C"
|
||||||
|
|
|
@ -5,7 +5,11 @@
|
||||||
#include "converter.hpp"
|
#include "converter.hpp"
|
||||||
#include "ginvoke.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);
|
std::wstring exePath(MAX_PATH, 0);
|
||||||
auto charWritten = GetModuleFileName(nullptr, exePath.data(), exePath.size());
|
auto charWritten = GetModuleFileName(nullptr, exePath.data(), exePath.size());
|
||||||
|
@ -14,10 +18,10 @@ std::optional<std::wstring> GetCurrentModulePath()
|
||||||
exePath.resize(charWritten);
|
exePath.resize(charWritten);
|
||||||
return exePath;
|
return exePath;
|
||||||
}
|
}
|
||||||
return std::nullopt;
|
throw win32_error{};
|
||||||
}
|
}
|
||||||
|
|
||||||
std::optional<std::wstring> GetShortcutPath()
|
std::wstring GetShortcutPath()
|
||||||
{
|
{
|
||||||
std::wstring shortcutPath(MAX_PATH, 0);
|
std::wstring shortcutPath(MAX_PATH, 0);
|
||||||
auto charWritten = GetEnvironmentVariable(L"APPDATA", shortcutPath.data(), shortcutPath.size());
|
auto charWritten = GetEnvironmentVariable(L"APPDATA", shortcutPath.data(), shortcutPath.size());
|
||||||
|
@ -26,7 +30,7 @@ std::optional<std::wstring> GetShortcutPath()
|
||||||
shortcutPath.resize(charWritten);
|
shortcutPath.resize(charWritten);
|
||||||
return shortcutPath;
|
return shortcutPath;
|
||||||
}
|
}
|
||||||
return std::nullopt;
|
throw win32_error{};
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ImplSetProcessAumid(const char *const aumid)
|
bool ImplSetProcessAumid(const char *const aumid)
|
||||||
|
|
Loading…
Reference in a new issue