get user role using visual c++ without using managed code

Posted on 2007-07-20
Last Modified: 2008-01-09
I am writing an applicaiton in Visual C++. I have been able to get the the user of the machine using GetUserName function. Now I would like to get the role of this user in the windows operating system. Basically I want to check if the user is administrator. Based on that I can then execute certain functionality in my application.

Now can anyone help me how I can achive this without using managed code?

Thanks in advance.
Question by:randhirshinde
    LVL 22

    Accepted Solution

    The function below shows you how to determine if the user is an administrator or not....

    BOOL IsAdmin()

       TOKEN_GROUPS* tgTokens;
       DWORD         group_no;
       PSID          admin_psid;
       HANDLE        hToken, hProcess;

       hProcess = GetCurrentProcess();
       OpenProcessToken(hProcess, TOKEN_QUERY, &hToken);

       GetTokenInformation(hToken, TokenGroups, NULL,0, &group_no);

       tgTokens = (TOKEN_GROUPS*)new BYTE[group_no];  

       GetTokenInformation(hToken, TokenGroups, tgTokens, group_no, &group_no);


       AllocateAndInitializeSid(&authsid, 2, SECURITY_BUILTIN_DOMAIN_RID,DOMAIN_ALIAS_RID_ADMINS,0,0,0,0,0,0, &admin_psid);

       for (int i = 0; i < tgTokens->GroupCount; i++)
        if (EqualSid(tgTokens->Groups[i].Sid, admin_psid))            
       delete [] tgTokens;
       return true; // User is admin

        return false;

    LVL 22

    Expert Comment

    Additionally if you have shell32.dll version 5+ you may use IsUserAnAdmin() from this dll....


    typedef BOOL (WINAPI *LPFNIUA)(void);

    HMODULE hShell32 = LoadLibrary(_T("shell32.dll"));
    LPFNIUA pIsAdmin = NULL;

    if (hShell32)
        pIsAdmin = (LPFNIUA)GetProcAddress(hShell32, MAKEINTRESOURCE(680));
        if (pIsAdmin)
            BOOL bIsAdmin = pIsAdmin();
            _tprintf(_T("User %s an admin\r\n"), bIsAdmin ? _T("is") : _T("is not"));

    Otherwise use code above


    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
    In Easy String Encryption Using CryptoAPI in C++ ( I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    This video discusses moving either the default database or any database to a new volume.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now