GetTokenInformation() confusion

This is a discussion on GetTokenInformation() confusion within the Windows Programming forums, part of the Platform Specific Boards category; Ok, thankyou for the information. I am currently using Dev-C++ 4.9.9.2 which may very well be outdated. Since Visual Studio ...

  1. #16
    Registered User
    Join Date
    Jan 2005
    Posts
    183
    Ok, thankyou for the information. I am currently using Dev-C++ 4.9.9.2 which may very well be outdated. Since Visual Studio compilers seem like a very popular choice, and alot of environment specific tutorials etc use Visual Studio, I may consider switching to it. I have read that VS 2008 is available in a free but limited version. Is this limited version worth downloading?
    After getting an up to date compiler I will get back to coding the project. If I have anymore questions, I will probably start a new thread so people don't get annoyed by thread bumping.
    Thanks for all your help

  2. #17
    Registered User
    Join Date
    Dec 2009
    Posts
    1
    Here is the code:
    http://blogs.msdn.com/junfeng/archiv...matically.aspx

    HRESULT IsUserAdmin(BOOL *pIsAdmin)
    {
    int b;
    HANDLE hProcess = NULL;
    HANDLE hProcessToken = NULL;
    HANDLE hLinkedToken = NULL;
    BOOL fIsAdmin = FALSE;
    DWORD dwLength = 0;
    OSVERSIONINFO osver = {sizeof(OSVERSIONINFO)};
    HRESULT hr = S_OK;

    *pIsAdmin = FALSE;

    // get handle to our process token
    hProcess = GetCurrentProcess();
    if (!OpenProcessToken(hProcess, TOKEN_QUERY, &hProcessToken))
    {
    hr = HRESULT_FROM_WIN32(GetLastError());
    goto Exit;
    }

    // get admin SID
    char AdminSID[SECURITY_MAX_SID_SIZE];
    dwLength = sizeof(AdminSID);
    if(!CreateWellKnownSid(WinBuiltinAdministratorsSid , NULL, &AdminSID, &dwLength))
    {
    hr = HRESULT_FROM_WIN32(GetLastError());
    goto Exit;
    }

    // check to see if the current token contains admin SID
    if (!CheckTokenMembership( NULL, &AdminSID, &fIsAdmin))
    {
    hr = HRESULT_FROM_WIN32(GetLastError());
    goto Exit;
    }

    if (fIsAdmin)
    {
    // printf("The user is in admin group, and the process is elevated.\n");
    *pIsAdmin = TRUE;
    goto Exit;
    }

    // if the current token does not contain admin SID, it does not mean
    // that the current user is not admin. In Vista by default the token of
    // users in administrator group has the the admin SID filtered. We nee
    // to use the unfiltered token to do the check.
    if (!GetVersionEx(&osver))
    {
    hr = HRESULT_FROM_WIN32(GetLastError());
    goto Exit;
    }

    // XP and below, we are done.
    if (osver.dwMajorVersion < 6)
    {
    // printf("The user is not in admin group.\n");
    goto Exit;
    }

    // get handle to linked token (will have one if we are lua)
    if (!GetTokenInformation( hProcessToken,
    TokenLinkedToken,
    (VOID*) &hLinkedToken,
    sizeof(HANDLE),
    &dwLength) )
    {
    b = GetLastError();
    if ( b == ERROR_NO_SUCH_LOGON_SESSION
    || b == ERROR_PRIVILEGE_NOT_HELD)
    {
    // printf("The user is not in admin group.\n");
    goto Exit;
    }

    hr = HRESULT_FROM_WIN32(b); // a real error
    goto Exit;
    }

    if (!CheckTokenMembership( hLinkedToken, &AdminSID, &fIsAdmin))
    {
    hr = HRESULT_FROM_WIN32(GetLastError());
    goto Exit;
    }

    if (fIsAdmin)
    {
    // printf("The user is in admin group, and the process is not elevated.\n");
    *pIsAdmin = TRUE;
    }
    else
    {
    // printf("The user is not in admin group.\n");
    }

    Exit:
    if (hProcess)
    {
    CloseHandle(hProcess);
    }

    if (hProcessToken)
    {
    CloseHandle(hProcessToken);
    }

    if (hLinkedToken)
    {
    CloseHandle(hLinkedToken);
    }

    return hr;
    }
    Last edited by Westminster; 12-14-2009 at 01:05 PM. Reason: Found answer

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. relative virtual addresses confusion
    By MrNoobah in forum Windows Programming
    Replies: 7
    Last Post: 10-13-2009, 08:45 PM
  2. Pointer + struct w/arrays confusion
    By Viper187 in forum C Programming
    Replies: 1
    Last Post: 07-23-2009, 09:37 AM
  3. Terrible confusion with time variables
    By LowlyIntern in forum C++ Programming
    Replies: 12
    Last Post: 08-01-2008, 07:23 AM
  4. confusion with increment and decrement operators
    By cBegginer in forum C Programming
    Replies: 6
    Last Post: 03-19-2005, 02:45 PM
  5. Unicode - a lot of confusion...
    By Jumper in forum Windows Programming
    Replies: 11
    Last Post: 07-05-2004, 07:59 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21