WinUtils.cpp 893 B

123456789101112131415161718192021222324252627282930
  1. #include "WinUtils.h"
  2. #include "windows.h"
  3. /////////////////////////////////////////////////////////////////////////////////////////
  4. bool WinUtils::getPowerManagementPrivileges()
  5. {
  6. HANDLE hToken = nullptr;
  7. // Get a token for this process.
  8. if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
  9. {
  10. // Get the LUID for the shutdown privilege.
  11. TOKEN_PRIVILEGES tkp = {};
  12. if (LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid))
  13. {
  14. tkp.PrivilegeCount = 1; // one privilege to set
  15. tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  16. // Get the shutdown privilege for this process.
  17. if (AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0))
  18. {
  19. CloseHandle(hToken);
  20. return true;
  21. }
  22. }
  23. }
  24. if (hToken)
  25. CloseHandle(hToken);
  26. return false;
  27. }