系统开启UAC情形下开机自启动程序如何以管理员权限启动
题记:本文阐述的是在Windows系统开启UAC的情况下,开机自启动程序需要以管理员权限启动,
系统弹出UAC对话框,用户同意的情形下启动程序
处理步骤:
1.判断Windows系统版本(Windows XP以上系统才有UAC,不包括Windows XP);若系统版本高于Windows XP,则执行步骤2;
2.判断系统是否开启UAC;若开启,则执行步骤3;
3.判断进程当前是否是以管理员权限启动;若不是管理员权限,则执行步骤4;
4.以特殊参数调用ShellExecuteEx函数,重新启动进程。
以下为参考代码:
//判断进程是否以管理员身份运行
bool isProcessRunAsAdmin()
{
SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
PSID AdministratorsGroup;
BOOL b = AllocateAndInitializeSid(
&NtAuthority,
2,
SECURITY_BUILTIN_DOMAIN_RID,
DOMAIN_ALIAS_RID_ADMINS,
0, 0, 0, 0, 0, 0,
&AdministratorsGroup);
if (b) {
CheckTokenMembership(NULL, AdministratorsGroup, &b);
FreeSid(AdministratorsGroup);
}
return b == TRUE;
}
//main函数
int main(int argc, char* argv[])
{
//任何对文件的操作在这里都不允许,不然会导致进程启动失败
OSVERSIONINFO osvi;
ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&osvi);
if (5 < osvi.dwMajorVersion) {
std::wstring wstrValue;
//以下函数为查询注册表,读者可自行实现
int iReturn = CUtility::queryRegValue(HKEY_LOCAL_MACHINE, L"Software\\Microsoft
\\Windows\\CurrentVersion\\Policies\\System", L"EnableLUA", wstrValue);
if (Success == iReturn) {
if (1 == *(int*)wstrValue.c_str() && false == isProcessRunAsAdmin()) {
wchar_t szPath[MAX_PATH] = {0};
GetModuleFileName(NULL, szPath, MAX_PATH);
std::wcout << szPath << std::endl;
std::wstring wstrParams;
for (int i = 1; i < argc; ++i) {
for (int j = 0; j < std::strlen(argv[i]); ++j) {
wstrParams.push_back(static_cast<wchar_t>(argv[i][j]));
}
}
SHELLEXECUTEINFO sei = {0};
sei.cbSize = sizeof(SHELLEXECUTEINFOW);
sei.fMask = SEE_MASK_NOCLOSEPROCESS | SEE_MASK_FLAG_NO_UI;
sei.lpFile = szPath;
sei.lpParameters = wstrParams.c_str();
sei.nShow = SW_SHOW;
sei.lpVerb = TEXT("runas");
sei.lpDirectory = NULL;
BOOL iRet = ShellExecuteEx(&sei);
if (TRUE != iRet) {
fprintf(stdout, "ShellExecuteEx Failure. Error Code is : %d", GetLastError());
}
return 0;
}
} else {
return 0;
}
}
//接下来为程序正常处理逻辑
}