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

67 lines
1.5 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()}
{}
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
{
2021-02-22 09:18:53 +00:00
std::wstring shortcutPath(MAX_PATH, 0);
2021-03-05 22:50:36 +00:00
auto charWritten = GetEnvironmentVariable(variable_name, shortcutPath.data(), shortcutPath.size());
2021-03-26 11:22:55 +00:00
if (charWritten > 0)
{
2021-02-23 10:04:43 +00:00
shortcutPath.resize(charWritten);
2021-02-22 09:18:53 +00:00
return shortcutPath;
2021-03-26 11:22:55 +00:00
}
throw win32_error{};
2021-03-26 11:22:55 +00:00
}
2021-03-05 22:15:09 +00:00
bool ImplSetProcessAumid(const char *const aumid)
2021-03-26 11:22:55 +00:00
{
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-05 22:15:09 +00:00
gboolean SetProcessAumid(const gchar* 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
}
}