2024-11-29 10:19:32 +08:00
|
|
|
#include "of_win.h"
|
2024-11-29 10:53:51 +08:00
|
|
|
#include <windows.h>
|
2024-11-29 10:19:32 +08:00
|
|
|
|
|
|
|
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, "Path", nullptr, nullptr, nullptr, &bufferSize) != ERROR_SUCCESS) {
|
|
|
|
RegCloseKey(hKey);
|
|
|
|
return STA_CANOT_QUERY_REG;
|
|
|
|
}
|
|
|
|
ofString currentPath(bufferSize, ofT('\0'));
|
|
|
|
if (RegQueryValueEx(hKey, "Path", nullptr, nullptr, reinterpret_cast<LPBYTE>(¤tPath[0]), &bufferSize) != ERROR_SUCCESS) {
|
|
|
|
RegCloseKey(hKey);
|
|
|
|
return STA_CANOT_QUERY_REG;
|
|
|
|
}
|
|
|
|
currentPath.resize(bufferSize - 1);
|
|
|
|
ofString updatedPath = currentPath + ofT(";") + path;
|
|
|
|
if (RegSetValueEx(hKey, "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);
|
|
|
|
}
|