How to determine whether the program runs as admin

You may wonder which account is your program running under, and following code might help:

BOOL bIsSystem = FALSE;
BOOL bIsFullAdmin = FALSE;

CAccessToken token;
if(token.GetProcessToken(TOKEN_READ))
{

    CSid sid;
    if(token.GetUser(&sid))
        bIsSystem = (sid == Sids::System());

    CTokenGroups groups;
    if(token.GetGroups(&groups))
    {
        DWORD dwAttrs = 0;
        if((groups.LookupSid(Sids::Admins(), &dwAttrs)))
            bIsFullAdmin = !(dwAttrs & SE_GROUP_USE_FOR_DENY_ONLY);
    }
}

Advertisements

Posted on April 18, 2011, in Uncategorized. Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: