Skip to content

Commit

Permalink
feat: WeaselSetup 默认启动不请求管理员权限,必要时使用管理员权限重启 (#1390)
Browse files Browse the repository at this point in the history
* feat: 默认启动不请求管理员权限,必要时使用管理员权限重启

- 写 HKCU 的命令不需要管理员权限,工程文件里的 RequireAdministrator
  改为 AsInvoker
- 调用 uninstall 和 install/CustomInstall 前判断一下进程的状态,
  如果不是管理就提权重启

* fix: 把 Release|Win32 的 UACExecutionLevel 也改成 AsInvoker
  • Loading branch information
WindyValley authored Oct 18, 2024
1 parent 3c70aed commit ba768a6
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
56 changes: 54 additions & 2 deletions WeaselSetup/WeaselSetup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
CAppModule _Module;

static int Run(LPTSTR lpCmdLine);
static bool IsProcAdmin();
static int RestartAsAdmin(LPTSTR lpCmdLine);

int WINAPI _tWinMain(HINSTANCE hInstance,
HINSTANCE /*hPrevInstance*/,
Expand Down Expand Up @@ -138,8 +140,12 @@ static int Run(LPTSTR lpCmdLine) {
constexpr bool silent = true;
constexpr bool old_ime_support = false;
bool uninstalling = !wcscmp(L"/u", lpCmdLine);
if (uninstalling)
return uninstall(silent);
if (uninstalling) {
if (IsProcAdmin())
return uninstall(silent);
else
return RestartAsAdmin(lpCmdLine);
}

if (!wcscmp(L"/ls", lpCmdLine)) {
return SetRegKeyValue(HKEY_CURRENT_USER, L"Software\\Rime\\weasel",
Expand Down Expand Up @@ -177,6 +183,11 @@ static int Run(LPTSTR lpCmdLine) {
return SetRegKeyValue(HKEY_CURRENT_USER, L"Software\\Rime\\weasel",
L"UpdateChannel", L"release", REG_SZ);
}

if (!IsProcAdmin()) {
return RestartAsAdmin(lpCmdLine);
}

bool hans = !wcscmp(L"/s", lpCmdLine);
if (hans)
return install(false, silent, old_ime_support);
Expand All @@ -186,3 +197,44 @@ static int Run(LPTSTR lpCmdLine) {
bool installing = !wcscmp(L"/i", lpCmdLine);
return CustomInstall(installing);
}

// https://learn.microsoft.com/zh-cn/windows/win32/api/securitybaseapi/nf-securitybaseapi-checktokenmembership
bool IsProcAdmin() {
BOOL b = FALSE;
SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
PSID AdministratorsGroup;
b = AllocateAndInitializeSid(&NtAuthority, 2, SECURITY_BUILTIN_DOMAIN_RID,
DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0,
&AdministratorsGroup);

if (b) {
if (!CheckTokenMembership(NULL, AdministratorsGroup, &b)) {
b = FALSE;
}
FreeSid(AdministratorsGroup);
}

return (b);
}

int RestartAsAdmin(LPTSTR lpCmdLine) {
SHELLEXECUTEINFO execInfo{0};
TCHAR path[MAX_PATH];
GetModuleFileName(GetModuleHandle(NULL), path, _countof(path));
execInfo.lpFile = path;
execInfo.lpParameters = lpCmdLine;
execInfo.lpVerb = _T("runas");
execInfo.cbSize = sizeof(execInfo);
execInfo.nShow = SW_SHOWNORMAL;
execInfo.fMask = SEE_MASK_NOASYNC | SEE_MASK_NOCLOSEPROCESS;
execInfo.hwnd = NULL;
execInfo.hProcess = NULL;
if (::ShellExecuteEx(&execInfo) && execInfo.hProcess != NULL) {
::WaitForSingleObject(execInfo.hProcess, INFINITE);
DWORD dwExitCode = 0;
::GetExitCodeProcess(execInfo.hProcess, &dwExitCode);
::CloseHandle(execInfo.hProcess);
return dwExitCode;
}
return -1;
}
6 changes: 3 additions & 3 deletions WeaselSetup/WeaselSetup.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
<SubSystem>Windows</SubSystem>
<AdditionalDependencies>Imm32.lib;Kernel32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<OutputFile>$(SolutionDir)output\$(ProjectName)$(TargetExt)</OutputFile>
<UACExecutionLevel>RequireAdministrator</UACExecutionLevel>
<UACExecutionLevel>AsInvoker</UACExecutionLevel>
<LinkTimeCodeGeneration>UseLinkTimeCodeGeneration</LinkTimeCodeGeneration>
</Link>
<ResourceCompile>
Expand Down Expand Up @@ -108,7 +108,7 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>Imm32.lib;Kernel32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<OutputFile>$(SolutionDir)output\$(ProjectName)$(TargetExt)</OutputFile>
<UACExecutionLevel>RequireAdministrator</UACExecutionLevel>
<UACExecutionLevel>AsInvoker</UACExecutionLevel>
<LinkTimeCodeGeneration>
</LinkTimeCodeGeneration>
</Link>
Expand Down Expand Up @@ -155,4 +155,4 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>
2 changes: 1 addition & 1 deletion WeaselSetup/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ target("WeaselSetup")
add_rules("add_rcfiles", "use_weaselconstants", "subwin")
add_links("imm32", "kernel32")

set_policy("windows.manifest.uac", "admin")
set_policy("windows.manifest.uac", "invoker")
add_files("$(projectdir)/PerMonitorHighDPIAware.manifest")
add_ldflags("/DEBUG /OPT:REF /OPT:ICF /LARGEADDRESSAWARE /ERRORREPORT:QUEUE")

Expand Down

0 comments on commit ba768a6

Please sign in to comment.