cout << "Hello World!" << endl; // 张鲁夺 :: 个人博客,幸福着你的幸福!


程序启动时请求UAC权限

作者:鲁夺,2015年9月28日,原创文章,转载请注明出处!

原文:http://zhangluduo.com/article/d569a84d/



BOOL Is_Vista_Win7_Win8()
{
    OSVERSIONINFOEX verinfo;
    ZeroMemory(&verinfo, sizeof(OSVERSIONINFOEX));
    verinfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);

    if (!GetVersionEx((LPOSVERSIONINFO)(&verinfo)))
        return FALSE;

    if (
    (verinfo.dwMajorVersion ==6 && verinfo.dwMinorVersion == 0) || // Vista
    (verinfo.dwMajorVersion ==6 && verinfo.dwMinorVersion == 1) || // Windows 7
    (verinfo.dwMajorVersion ==6 && verinfo.dwMinorVersion == 2) || // Windows 8
    (verinfo.dwMajorVersion ==6 && verinfo.dwMinorVersion == 3)    // Windows 8.1
    )
        return TRUE;

    return FALSE;
}

BOOL StartupRequestUac(TCHAR* FileName)
{
    SHELLEXECUTEINFO sei;
    ZeroMemory(&sei, sizeof(SHELLEXECUTEINFO));

    sei.cbSize = sizeof(SHELLEXECUTEINFO);
    sei.fMask = SEE_MASK_NOCLOSEPROCESS | SEE_MASK_FLAG_NO_UI;
    sei.lpFile = (LPCTSTR)FileName;
    sei.lpVerb = _T("runas");

    return ShellExecuteEx(&sei);
}

BOOL IsRunasAdmin()
{
    BOOL fIsRunAsAdmin = FALSE;
    DWORD dwError = ERROR_SUCCESS;
    PSID pAdministratorsGroup = NULL;

    // Allocate and initialize a SID of the administrators group.
    SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
    if (!AllocateAndInitializeSid(
        &NtAuthority, 
        2, 
        SECURITY_BUILTIN_DOMAIN_RID, 
        DOMAIN_ALIAS_RID_ADMINS, 
        0, 0, 0, 0, 0, 0, 
        &pAdministratorsGroup))
    {
        dwError = GetLastError();
        goto Cleanup;
    }

    // Determine whether the SID of administrators group is enabled in 
    // the primary access token of the process.
    if (!CheckTokenMembership(NULL, pAdministratorsGroup, &fIsRunAsAdmin))
    {
        dwError = GetLastError();
        goto Cleanup;
    }

Cleanup:
    // Centralized cleanup for all allocated resources.
    if (pAdministratorsGroup)
    {
        FreeSid(pAdministratorsGroup);
        pAdministratorsGroup = NULL;
    }

    // Throw the error if something failed in the function.
    if (ERROR_SUCCESS != dwError)
    {
        throw dwError;
    }

    return fIsRunAsAdmin;
}

CString GetAppFileName()
{
    TCHAR Buffer[MAX_PATH] = { 0 };
    ::GetModuleFileName(NULL, Buffer, MAX_PATH);

    CString Temp = Buffer;
    return Temp;
}

BOOL CXxxApp::InitInstance()
{
    ...

    if (Is_Vista_Win7_Win8())
    {
        if (!IsRunasAdmin())
        {
            StartupRequestUac((LPTSTR)(LPCTSTR)(GetAppFileName()));
            return FALSE;
        }
    }

    CXxxDlg dlg;
    m_pMainWnd = &dlg;
    INT_PTR nResponse = dlg.DoModal();
    if (nResponse == IDOK)
    {
        // TODO: Place code here to handle when the dialog is
        //  dismissed with OK
    }
    else if (nResponse == IDCANCEL)
    {
        // TODO: Place code here to handle when the dialog is
        //  dismissed with Cancel
    }

    // Since the dialog has been closed, return FALSE so that we exit the
    //  application, rather than start the application's message pump.
    return FALSE;
}

Copyright © 2015 Zhang Luduo.

All rights reserved.