19

我需要确定我的程序是否以完全管理员权限运行。我的意思是如果打开了 uac(对于 win vista/7),我需要确定程序是否真的具有管理员权限(比如用户右键单击并选择“以管理员身份运行”)并且不受 uac 的限制。我如何在 C++ 中做到这一点?

4

2 回答 2

12

Other alternatives are: IsUserAnAdmin or AccessCheck

Checking the TOKEN_ELEVATION* stuff in the token is not required for testing the current process but it is useful if you need to find out if the user could elevate because they have a split token etc.

于 2010-11-20T02:33:00.433 回答
0

对于那些(像我一样)不太懂 Windows 的人来说,安德斯的答案的扩展:

    BOOL isMember;
    PSID administratorsGroup = NULL;
    SID_IDENTIFIER_AUTHORITY SIDAuthNT =
        SECURITY_NT_AUTHORITY;

    if (!AllocateAndInitializeSid(&SIDAuthNT, 2,
        SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS,
        0, 0, 0, 0, 0, 0,
        &administratorsGroup))
    {
        throw(oops_t(GetLastError(), "AllocateAndInitializeSid"));
    }

    if (!CheckTokenMembership(nullptr, administratorsGroup, &isMember))
    {
        throw(oops_t(GetLastError(), "CheckTokenMembership"));
    }

    if (!isMember)
    {
        throw(oops_t(ERROR_ACCESS_DENIED, "Test for Admin privileges"));
    }
于 2019-02-19T15:03:15.590 回答