determining user access rights on Windows PC before auto-installing DLL

Posted on 2007-10-04
Last Modified: 2013-12-04
Can anyone suggest away of determining whether a user has admin rights to be able to install (in Program Files) on their PC?

I'm trying to find an elegant way to provide a link via a web page that will auto-install a DLL on any Windows PC - but won't attempt an install if the user doesn't have admin rights, instead displays a message back to the browser.

Question by:TangoGirl
    LVL 86

    Accepted Solution

    That would be

    | Name: RunningAsAdministrator
    | Desc: checks if user has administrator privileges
    | Notes: This function returns TRUE if the user identifier associated with
    |   this process is a member of the the Administrators group.
    BOOL RunningAsAdministrator ( VOID)
     BOOL   fAdmin;
     HANDLE  hThread;
     TOKEN_GROUPS *ptg = NULL;
     DWORD  cbTokenGroups;
     DWORD  dwGroup;
     PSID   psidAdmin;


     // First we must open a handle to the access token for this thread.

     if ( !OpenThreadToken ( GetCurrentThread(), TOKEN_QUERY, FALSE, &hThread))
      if ( GetLastError() == ERROR_NO_TOKEN)
       // If the thread does not have an access token, we'll examine the
       // access token associated with the process.

       if (! OpenProcessToken ( GetCurrentProcess(), TOKEN_QUERY,
       return ( FALSE);
       return ( FALSE);

     // Then we must query the size of the group information associated with
     // the token. Note that we expect a FALSE result from GetTokenInformation
     // because we've given it a NULL buffer. On exit cbTokenGroups will tell
     // the size of the group information.

     if ( GetTokenInformation ( hThread, TokenGroups, NULL, 0, &cbTokenGroups))
      return ( FALSE);

     // Here we verify that GetTokenInformation failed for lack of a large
     // enough buffer.

     if ( GetLastError() != ERROR_INSUFFICIENT_BUFFER)
      return ( FALSE);

     // Now we allocate a buffer for the group information.
     // Since _alloca allocates on the stack, we don't have
     // to explicitly deallocate it. That happens automatically
     // when we exit this function.

     if ( ! ( ptg= _alloca ( cbTokenGroups)))
      return ( FALSE);

     // Now we ask for the group information again.
     // This may fail if an administrator has added this account
     // to an additional group between our first call to
     // GetTokenInformation and this one.

     if ( !GetTokenInformation ( hThread, TokenGroups, ptg, cbTokenGroups,
              &cbTokenGroups) )
      return ( FALSE);

     // Now we must create a System Identifier for the Admin group.

     if ( ! AllocateAndInitializeSid ( &SystemSidAuthority, 2,
                0, 0, 0, 0, 0, 0, &psidAdmin) )
      return ( FALSE);

     // Finally we'll iterate through the list of groups for this access
     // token looking for a match against the SID we created above.

     fAdmin= FALSE;

     for ( dwGroup= 0; dwGroup < ptg->GroupCount; dwGroup++)
      if ( EqualSid ( ptg->Groups[dwGroup].Sid, psidAdmin))
       fAdmin = TRUE;


     // Before we exit we must explicity deallocate the SID we created.

     FreeSid ( psidAdmin);

     return ( fAdmin);

    See also ("Windows NT Security")
    LVL 86

    Expert Comment

    I'd say this question is answered, thus I object.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    This article covers how to install the Microsoft Windows Operating System (OS). What is covered in this article:  > Different Versions and Editions of the Windows OS  > Upgrading versus Fresh Installation of the OS           - Steps to take pr…
    Ever notice how you can't use a new drive in Windows without having Windows assigning a Disk Signature?  Ever have a signature collision problem (especially with Virtual Machines?)  This article is intended to help you understand what's going on and…
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

    755 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

    16 Experts available now in Live!

    Get 1:1 Help Now