Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 默认启动不请求管理员权限,必要时使用管理员权限重启 #1390

Merged
merged 2 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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