Skip to content

Commit ba768a6

Browse files
authored
feat: WeaselSetup 默认启动不请求管理员权限,必要时使用管理员权限重启 (#1390)
* feat: 默认启动不请求管理员权限,必要时使用管理员权限重启 - 写 HKCU 的命令不需要管理员权限,工程文件里的 RequireAdministrator 改为 AsInvoker - 调用 uninstall 和 install/CustomInstall 前判断一下进程的状态, 如果不是管理就提权重启 * fix: 把 Release|Win32 的 UACExecutionLevel 也改成 AsInvoker
1 parent 3c70aed commit ba768a6

File tree

3 files changed

+58
-6
lines changed

3 files changed

+58
-6
lines changed

WeaselSetup/WeaselSetup.cpp

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
CAppModule _Module;
1515

1616
static int Run(LPTSTR lpCmdLine);
17+
static bool IsProcAdmin();
18+
static int RestartAsAdmin(LPTSTR lpCmdLine);
1719

1820
int WINAPI _tWinMain(HINSTANCE hInstance,
1921
HINSTANCE /*hPrevInstance*/,
@@ -138,8 +140,12 @@ static int Run(LPTSTR lpCmdLine) {
138140
constexpr bool silent = true;
139141
constexpr bool old_ime_support = false;
140142
bool uninstalling = !wcscmp(L"/u", lpCmdLine);
141-
if (uninstalling)
142-
return uninstall(silent);
143+
if (uninstalling) {
144+
if (IsProcAdmin())
145+
return uninstall(silent);
146+
else
147+
return RestartAsAdmin(lpCmdLine);
148+
}
143149

144150
if (!wcscmp(L"/ls", lpCmdLine)) {
145151
return SetRegKeyValue(HKEY_CURRENT_USER, L"Software\\Rime\\weasel",
@@ -177,6 +183,11 @@ static int Run(LPTSTR lpCmdLine) {
177183
return SetRegKeyValue(HKEY_CURRENT_USER, L"Software\\Rime\\weasel",
178184
L"UpdateChannel", L"release", REG_SZ);
179185
}
186+
187+
if (!IsProcAdmin()) {
188+
return RestartAsAdmin(lpCmdLine);
189+
}
190+
180191
bool hans = !wcscmp(L"/s", lpCmdLine);
181192
if (hans)
182193
return install(false, silent, old_ime_support);
@@ -186,3 +197,44 @@ static int Run(LPTSTR lpCmdLine) {
186197
bool installing = !wcscmp(L"/i", lpCmdLine);
187198
return CustomInstall(installing);
188199
}
200+
201+
// https://learn.microsoft.com/zh-cn/windows/win32/api/securitybaseapi/nf-securitybaseapi-checktokenmembership
202+
bool IsProcAdmin() {
203+
BOOL b = FALSE;
204+
SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
205+
PSID AdministratorsGroup;
206+
b = AllocateAndInitializeSid(&NtAuthority, 2, SECURITY_BUILTIN_DOMAIN_RID,
207+
DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0,
208+
&AdministratorsGroup);
209+
210+
if (b) {
211+
if (!CheckTokenMembership(NULL, AdministratorsGroup, &b)) {
212+
b = FALSE;
213+
}
214+
FreeSid(AdministratorsGroup);
215+
}
216+
217+
return (b);
218+
}
219+
220+
int RestartAsAdmin(LPTSTR lpCmdLine) {
221+
SHELLEXECUTEINFO execInfo{0};
222+
TCHAR path[MAX_PATH];
223+
GetModuleFileName(GetModuleHandle(NULL), path, _countof(path));
224+
execInfo.lpFile = path;
225+
execInfo.lpParameters = lpCmdLine;
226+
execInfo.lpVerb = _T("runas");
227+
execInfo.cbSize = sizeof(execInfo);
228+
execInfo.nShow = SW_SHOWNORMAL;
229+
execInfo.fMask = SEE_MASK_NOASYNC | SEE_MASK_NOCLOSEPROCESS;
230+
execInfo.hwnd = NULL;
231+
execInfo.hProcess = NULL;
232+
if (::ShellExecuteEx(&execInfo) && execInfo.hProcess != NULL) {
233+
::WaitForSingleObject(execInfo.hProcess, INFINITE);
234+
DWORD dwExitCode = 0;
235+
::GetExitCodeProcess(execInfo.hProcess, &dwExitCode);
236+
::CloseHandle(execInfo.hProcess);
237+
return dwExitCode;
238+
}
239+
return -1;
240+
}

WeaselSetup/WeaselSetup.vcxproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
<SubSystem>Windows</SubSystem>
6767
<AdditionalDependencies>Imm32.lib;Kernel32.lib;%(AdditionalDependencies)</AdditionalDependencies>
6868
<OutputFile>$(SolutionDir)output\$(ProjectName)$(TargetExt)</OutputFile>
69-
<UACExecutionLevel>RequireAdministrator</UACExecutionLevel>
69+
<UACExecutionLevel>AsInvoker</UACExecutionLevel>
7070
<LinkTimeCodeGeneration>UseLinkTimeCodeGeneration</LinkTimeCodeGeneration>
7171
</Link>
7272
<ResourceCompile>
@@ -108,7 +108,7 @@
108108
<GenerateDebugInformation>true</GenerateDebugInformation>
109109
<AdditionalDependencies>Imm32.lib;Kernel32.lib;%(AdditionalDependencies)</AdditionalDependencies>
110110
<OutputFile>$(SolutionDir)output\$(ProjectName)$(TargetExt)</OutputFile>
111-
<UACExecutionLevel>RequireAdministrator</UACExecutionLevel>
111+
<UACExecutionLevel>AsInvoker</UACExecutionLevel>
112112
<LinkTimeCodeGeneration>
113113
</LinkTimeCodeGeneration>
114114
</Link>
@@ -155,4 +155,4 @@
155155
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
156156
<ImportGroup Label="ExtensionTargets">
157157
</ImportGroup>
158-
</Project>
158+
</Project>

WeaselSetup/xmake.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ target("WeaselSetup")
44
add_rules("add_rcfiles", "use_weaselconstants", "subwin")
55
add_links("imm32", "kernel32")
66

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

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy