ofen/src/of_win.cpp

75 lines
2.2 KiB
C++

#include "of_win.h"
#ifdef _WIN32
#include <windows.h>
constexpr auto g_VSReg = ofT("");
constexpr auto g_EnvReg = ofT("SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment");
COfWin::COfWin()
{
}
COfWin::~COfWin()
{
}
OfStatus COfWin::getVSInstallations(std::map<ofString, ofString>& out)
{
return STA_ERROR;
}
OfStatus COfWin::appendPathVariable(const ofString& path)
{
HKEY hKey;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, g_EnvReg, 0, KEY_READ | KEY_WRITE, &hKey) != ERROR_SUCCESS) {
return STA_CANOT_OPEN_REG;
}
DWORD bufferSize = 0;
if (RegQueryValueEx(hKey, ofT("Path"), nullptr, nullptr, nullptr, &bufferSize) != ERROR_SUCCESS) {
RegCloseKey(hKey);
return STA_CANOT_QUERY_REG;
}
ofString currentPath(bufferSize, ofT('\0'));
if (RegQueryValueEx(hKey, ofT("Path"), nullptr, nullptr, reinterpret_cast<LPBYTE>(&currentPath[0]), &bufferSize) != ERROR_SUCCESS) {
RegCloseKey(hKey);
return STA_CANOT_QUERY_REG;
}
currentPath.resize(bufferSize - 1);
ofString updatedPath = currentPath + ofT(";") + path;
if (RegSetValueEx(hKey, ofT("Path"), 0, REG_EXPAND_SZ, reinterpret_cast<const BYTE*>(updatedPath.c_str()), updatedPath.size() + 1) != ERROR_SUCCESS) {
RegCloseKey(hKey);
return STA_CANOT_SET_REG;
}
RegCloseKey(hKey);
SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, reinterpret_cast<LPARAM>("Environment"), SMTO_ABORTIFHUNG, 5000, nullptr);
return STA_SUCESS;
}
OfStatus COfWin::removePathVariable(const ofString& path)
{
return STA_ERROR;
}
bool COfWin::isAdminRun(bool& is_admin)
{
BOOL isAdmin = FALSE;
HANDLE tokenHandle = nullptr;
if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &tokenHandle)) {
// 获取管理员组的 SID
SID_IDENTIFIER_AUTHORITY ntAuthority = SECURITY_NT_AUTHORITY;
PSID adminGroup;
if (AllocateAndInitializeSid(&ntAuthority, 2, SECURITY_BUILTIN_DOMAIN_RID,
DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &adminGroup)) {
// 检查当前令牌是否属于管理员组
if (!CheckTokenMembership(nullptr, adminGroup, &isAdmin)) {
isAdmin = FALSE;
}
FreeSid(adminGroup);
}
CloseHandle(tokenHandle);
}
return static_cast<bool>(isAdmin);
}
#endif