anotherim-desktop/plugins/windows-notification/api/src/win32.cpp

73 lines
1.8 KiB
C++
Raw Normal View History

2021-03-26 11:22:55 +00:00
#include <windows.h>
#include <shlobj.h>
#include "win32.hpp"
#include "converter.hpp"
#include "ginvoke.hpp"
2021-03-26 11:22:55 +00:00
win32_error::win32_error() noexcept
: win32_error{::GetLastError()}
{}
constexpr auto noncharacter = L'\uFFFF';
2021-03-05 22:44:05 +00:00
std::wstring GetExePath()
2021-03-26 11:22:55 +00:00
{
2021-02-22 09:18:53 +00:00
std::wstring exePath(MAX_PATH, 0);
2021-03-26 11:22:55 +00:00
auto charWritten = GetModuleFileName(nullptr, exePath.data(), exePath.size());
if (charWritten > 0)
{
2021-02-23 10:04:43 +00:00
exePath.resize(charWritten);
2021-02-22 09:18:53 +00:00
return exePath;
2021-03-26 11:22:55 +00:00
}
throw win32_error{};
2021-03-26 11:22:55 +00:00
}
2021-03-05 22:50:36 +00:00
std::wstring GetEnv(const wchar_t *const variable_name)
2021-03-26 11:22:55 +00:00
{
const auto bufsize = ::GetEnvironmentVariableW(variable_name, nullptr, 0);
if (not bufsize)
throw win32_error{};
std::wstring buf(bufsize, noncharacter);
const auto res =
::GetEnvironmentVariableW(variable_name, buf.data(), bufsize);
if (const auto e = ::GetLastError())
throw win32_error{e};
if (not res or res >= bufsize) // not entirely sure this isn't just paranoia
throw std::runtime_error{"GetEnvironmentVariableW misbehaved"};
buf.resize(res);
return buf;
2021-03-26 11:22:55 +00:00
}
static bool ImplSetProcessAumid(const std::string_view aumid)
2021-03-26 11:22:55 +00:00
{
2021-03-06 00:30:06 +00:00
const auto waumid = sview_to_wstr(aumid);
if (waumid.empty())
{
return false;
}
return SUCCEEDED(SetCurrentProcessExplicitAppUserModelID(waumid.c_str()));
2021-03-26 11:22:55 +00:00
}
extern "C"
{
// Not available in mingw headers, but linking works.
NTSTATUS NTAPI RtlGetVersion(PRTL_OSVERSIONINFOW);
2021-03-05 22:15:09 +00:00
gboolean IsWindows10() noexcept
2021-03-26 11:22:55 +00:00
{
RTL_OSVERSIONINFOW rovi = { 0 };
rovi.dwOSVersionInfoSize = sizeof(rovi);
if (S_OK == RtlGetVersion(&rovi))
{
return rovi.dwMajorVersion > 6;
}
return FALSE;
}
2021-03-06 00:30:06 +00:00
gboolean SetProcessAumid(const gchar *const aumid) noexcept
2021-03-26 11:22:55 +00:00
{
2021-03-05 22:15:09 +00:00
return g_try_invoke(ImplSetProcessAumid, aumid);
2021-03-26 11:22:55 +00:00
}
}