Leaked source code of windows server 2003
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.

152 lines
4.7 KiB

  1. /*++
  2. Copyright (c) 2001 Microsoft Corporation
  3. Module Name:
  4. power.c
  5. Abstract:
  6. This module contains code to set the default power scheme and hibernation settings in Windows.
  7. [ComputerSettings]
  8. Hibernation = YES | NO - Specifies whether we want hibernation.
  9. PowerScheme = Desktop | - These are the standard power schemes in Whistler.
  10. Laptop |
  11. Presentation |
  12. AlwaysOn | Always On |
  13. Minimal |
  14. MaxBattery | Max Battery
  15. Author:
  16. Adrian Cosma (acosma) - 1/31/2001
  17. Revision History:
  18. --*/
  19. //
  20. // Includes
  21. //
  22. #include "factoryp.h"
  23. // For setting default power scheme
  24. #define REG_KEY_WINLOGON _T("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon")
  25. #define REG_VALUE_HIBERNATION_PREVIOUSLY_ENABLED _T("HibernationPreviouslyEnabled")
  26. //
  27. // Function implementations
  28. //
  29. /*
  30. Returns: TRUE on success, FALSE if there is some failure.
  31. */
  32. BOOL SetPowerOptions(LPSTATEDATA lpStateData)
  33. {
  34. LPTSTR lpszWinBOMPath = lpStateData->lpszWinBOMPath;
  35. TCHAR szBuf[MAX_INF_STRING_LENGTH] = NULLSTR;
  36. // BOOLEAN is 1 byte, bEnable has to be BOOLEAN, not BOOL (which is 4 bytes).
  37. BOOLEAN bEnable;
  38. UINT uiPwrPol = UINT_MAX;
  39. BOOL bRet = TRUE;
  40. BOOL bHiber = FALSE;
  41. //
  42. // Is Hibernation specified?
  43. //
  44. if ( GetPrivateProfileString( WBOM_SETTINGS_SECTION, INI_KEY_WBOM_HIBERNATION, NULLSTR, szBuf, AS(szBuf), lpszWinBOMPath) &&
  45. szBuf[0]
  46. )
  47. {
  48. if ( 0 == LSTRCMPI(szBuf, WBOM_NO) )
  49. {
  50. bEnable = FALSE;
  51. bHiber = TRUE;
  52. }
  53. else if ( 0 == LSTRCMPI(szBuf, WBOM_YES) )
  54. {
  55. bEnable = TRUE;
  56. bHiber = TRUE;
  57. }
  58. else
  59. {
  60. FacLogFile(0 | LOG_ERR, IDS_ERR_WINBOMVALUE, lpszWinBOMPath, INI_SEC_WBOM_SETTINGS, INI_KEY_WBOM_HIBERNATION, szBuf);
  61. bRet = FALSE;
  62. }
  63. if ( bHiber )
  64. {
  65. NTSTATUS Status;
  66. // Request the privilege to create a pagefile. Oddly enough this is needed
  67. // to disable hibernation.
  68. //
  69. EnablePrivilege(SE_CREATE_PAGEFILE_NAME, TRUE);
  70. Status = NtPowerInformation ( SystemReserveHiberFile, &bEnable, sizeof (bEnable), NULL, 0 );
  71. if ( Status != STATUS_SUCCESS )
  72. FacLogFile(0 | LOG_ERR, IDS_ERR_NTPOWERINFO, Status );
  73. else
  74. {
  75. // Do this so winlogon doesn't decide to re-enable hibernation for us if we disabled it.
  76. //
  77. RegSetDword(NULL, REG_KEY_WINLOGON, REG_VALUE_HIBERNATION_PREVIOUSLY_ENABLED, 1);
  78. }
  79. }
  80. }
  81. //
  82. // Set Power Scheme
  83. //
  84. if ( GetPrivateProfileString( WBOM_SETTINGS_SECTION, INI_KEY_WBOM_PWRSCHEME, NULLSTR, szBuf, AS(szBuf), lpszWinBOMPath) &&
  85. szBuf[0]
  86. )
  87. {
  88. if ( 0 == LSTRCMPI(szBuf, INI_VAL_WBOM_PWR_DESKTOP) )
  89. uiPwrPol = 0;
  90. else if ( 0 == LSTRCMPI(szBuf, INI_VAL_WBOM_PWR_LAPTOP) )
  91. uiPwrPol = 1;
  92. else if ( 0 == LSTRCMPI(szBuf, INI_VAL_WBOM_PWR_PRESENTATION) )
  93. uiPwrPol = 2;
  94. else if ( 0 == LSTRCMPI(szBuf, INI_VAL_WBOM_PWR_ALWAYSON) || 0 == LSTRCMPI(szBuf, INI_VAL_WBOM_PWR_ALWAYS_ON) )
  95. uiPwrPol = 3;
  96. else if ( 0 == LSTRCMPI(szBuf, INI_VAL_WBOM_PWR_MINIMAL) )
  97. uiPwrPol = 4;
  98. else if ( 0 == LSTRCMPI(szBuf, INI_VAL_WBOM_PWR_MAXBATTERY) || 0 == LSTRCMPI(szBuf, INI_VAL_WBOM_PWR_MAX_BATTERY) )
  99. uiPwrPol = 5;
  100. // If something valid was specified set it.
  101. //
  102. if ( UINT_MAX != uiPwrPol )
  103. {
  104. if ( !SetActivePwrScheme(uiPwrPol, NULL, NULL) )
  105. {
  106. FacLogFile(0 | LOG_ERR, IDS_ERR_SETPWRSCHEME, GetLastError());
  107. bRet = FALSE;
  108. }
  109. }
  110. else
  111. {
  112. FacLogFile(0 | LOG_ERR, IDS_ERR_WINBOMVALUE, lpszWinBOMPath, INI_SEC_WBOM_SETTINGS, INI_KEY_WBOM_PWRSCHEME, szBuf);
  113. bRet = FALSE;
  114. }
  115. }
  116. return bRet;
  117. }
  118. BOOL DisplaySetPowerOptions(LPSTATEDATA lpStateData)
  119. {
  120. return ( IniSettingExists(lpStateData->lpszWinBOMPath, INI_SEC_WBOM_SETTINGS, INI_KEY_WBOM_HIBERNATION, NULL) ||
  121. IniSettingExists(lpStateData->lpszWinBOMPath, INI_SEC_WBOM_SETTINGS, INI_KEY_WBOM_PWRSCHEME, NULL) );
  122. }