码迷,mamicode.com
首页 > 其他好文 > 详细

系统开启UAC情形下开机自启动程序如何以管理员权限启动

时间:2015-03-28 18:43:51      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:

题记:本文阐述的是在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;
}

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;
        }
    }

}

系统开启UAC情形下开机自启动程序如何以管理员权限启动

标签:

原文地址:http://www.cnblogs.com/WONDERFUL-cnblogs/p/4374569.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!