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.

94 lines
3.1 KiB

  1. /* Copyright 1999 American Power Conversion, All Rights Reserved
  2. *
  3. * Description:
  4. * The file implements the Hibernation funcationality. It is responsible
  5. * for performing a hibernation of the operating system.
  6. *
  7. *
  8. * Revision History:
  9. * sberard 14May1999 initial revision.
  10. * sberard 20May1999 modified to use the Power Management Profile Interface API
  11. *
  12. */
  13. #include "hibernate.h"
  14. // These prototypes are needed because we do not have access to powrprof.h
  15. BOOLEAN WINAPI IsPwrHibernateAllowed(VOID);
  16. BOOLEAN WINAPI SetSuspendState(IN BOOLEAN bHibernate, IN BOOLEAN bForce, IN BOOLEAN bWakeupEventsDisabled);
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. /**
  21. * HibernateSystem
  22. *
  23. * Description:
  24. * This function initiates hibernation of the operating system. This is
  25. * performed through a call to the Win32 function SetSystemPowerStae(..).
  26. * When called hibernation is initated immediately and, if successful, the
  27. * function will return TRUE when the system returns from hibernation.
  28. * Otherwise, FALSE is retuned to indicate the the system did not hibernate.
  29. *
  30. * Parameters:
  31. * none
  32. *
  33. * Returns:
  34. * TRUE - if hibernation was initiated successfully and subsequently restored
  35. * FALSE - if errors occur while initiating hibernation
  36. */
  37. BOOL HibernateSystem() {
  38. BOOL ret_val = FALSE;
  39. TOKEN_PRIVILEGES tkp;
  40. HANDLE process_token;
  41. // get the current process token so that we can
  42. // modify our current process privs.
  43. //
  44. if (OpenProcessToken(GetCurrentProcess(),
  45. TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &process_token)) {
  46. // Find the local unique id for SeShutdownPrivilege
  47. if (LookupPrivilegeValue(NULL, TEXT("SeShutdownPrivilege"),
  48. &tkp.Privileges[0].Luid)) {
  49. // we only want to enable one priv
  50. tkp.PrivilegeCount = 1;
  51. tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  52. // now, add it all back to our current process.
  53. if (AdjustTokenPrivileges(process_token, // do it to us
  54. FALSE, // don't turn all privs off
  55. &tkp, // what we want to do
  56. 0, // don't want any prev info
  57. (PTOKEN_PRIVILEGES)NULL,
  58. 0)) {
  59. // Check to see if hibernation is enabled
  60. if (IsPwrHibernateAllowed() == TRUE) {
  61. // Attempt to hibernate the system
  62. if (SetSuspendState(TRUE, TRUE, FALSE) == TRUE) {
  63. // Hibernation was successful, system has been restored
  64. ret_val = TRUE;
  65. }
  66. else {
  67. // There was an error attempting to hibernate the system
  68. ret_val = FALSE;
  69. }
  70. }
  71. else {
  72. // Hibernation is not selected as the CriticalPowerAction, return an error
  73. ret_val = FALSE;
  74. }
  75. }
  76. }
  77. CloseHandle (process_token);
  78. }
  79. return ret_val;
  80. }
  81. #ifdef __cplusplus
  82. }
  83. #endif