Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.9 KiB

  1. //=============================================================================*
  2. // COPYRIGHT� 2001 Microsoft Corporation and Executive Software International, Inc.
  3. //=============================================================================*
  4. // File: AdminPrivs.cpp
  5. //=============================================================================*
  6. // DfrgVols.cpp : Implementation of CDfrgVols
  7. #include "stdafx.h"
  8. #include <windows.h>
  9. #include "message.h"
  10. #include "AdminPrivs.h"
  11. #include "assert.h"
  12. /****************************************************************************************************************
  13. COPYRIGHT 2001 Microsoft Corporation and Executive Software International, Inc.
  14. ROUTINE DESCRIPTION:
  15. This routine checks if the current user belongs to an Administrator Group.
  16. GLOBAL VARIABLES:
  17. INPUT:
  18. None.
  19. RETURN:
  20. TRUE = success - User does belong to an Administrator group
  21. FALSE = failure - User does Not belong to an Administrator group
  22. */
  23. BOOL
  24. CheckForAdminPrivs()
  25. {
  26. BOOL bIsAdministrator = FALSE;
  27. HRESULT hr = S_OK;
  28. DWORD dwErr = 0;
  29. TCHAR cString[300];
  30. DWORD dwInfoBufferSize = 0;
  31. PSID psidAdministrators;
  32. SID_IDENTIFIER_AUTHORITY siaNtAuthority = SECURITY_NT_AUTHORITY;
  33. BOOL bResult = AllocateAndInitializeSid(&siaNtAuthority, 2,
  34. SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS,
  35. 0, 0, 0, 0, 0, 0, &psidAdministrators);
  36. if (bResult) {
  37. bResult = CheckTokenMembership(0, psidAdministrators, &bIsAdministrator);
  38. assert(bResult);
  39. if (!bResult) {
  40. wsprintf(cString, TEXT("Error = %d"), GetLastError());
  41. Message(TEXT("CheckForAdminPrivs::CheckTokenMembership"), E_FAIL, cString);
  42. }
  43. FreeSid(psidAdministrators);
  44. }
  45. else {
  46. wsprintf(cString, TEXT("Error = %d"), GetLastError());
  47. Message(TEXT("CheckForAdminPrivs::AllocateAndInitializeSid"), E_FAIL, cString);
  48. }
  49. return bIsAdministrator;
  50. }