Learn how to a build a cloud-first strategyRegister Now


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

Posted on 2007-10-04
Medium Priority
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
  • 2
LVL 86

Accepted Solution

jkr earned 1500 total points
ID: 20015359
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;
 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.

  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 http://msdn2.microsoft.com/en-us/library/ms995339.aspx ("Windows NT Security")
LVL 86

Expert Comment

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a little timesaver I have been using for setting up Microsoft Small Business Server (SBS) in the simplest possible way. It may not be appropriate for every customer. However, when you get a situation where the person who owns the server is i…
When you upgrade from Windows 8 to 8.1 or to Windows 10 or if you are like me you are on the Insider Program you may find yourself with many 450MB recovery partitions.  With a traditional disk that may not be a problem but with relatively smaller SS…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
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. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

810 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