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.

4484 lines
135 KiB

  1. /*++
  2. Copyright (c) 2001 Microsoft Corporation
  3. Module Name:
  4. powercfg.c
  5. Abstract:
  6. Allows users to view and modify power schemes and system power settings
  7. from the command line. May be useful in unattended configuration and
  8. for headless systems.
  9. Author:
  10. Ben Hertzberg (t-benher) 1-Jun-2001
  11. Revision History:
  12. Ben Hertzberg (t-benher) 15-Jun-2001 - CPU throttle added
  13. Ben Hertzberg (t-benher) 4-Jun-2001 - import/export added
  14. Ben Hertzberg (t-benher) 1-Jun-2001 - created it.
  15. --*/
  16. // app-specific includes
  17. #include <initguid.h>
  18. #include "powercfg.h"
  19. #include "cmdline.h"
  20. #include "cmdlineres.h"
  21. #include "resource.h"
  22. // app-specific structures
  23. // structure to manage the scheme list information.
  24. // note that descriptions are currently not visible in the
  25. // GUI tool (as of 6-1-2001), so they are not visible in this
  26. // app either, although the framework is already there if
  27. // someone decides to add the descriptions at a later date.
  28. typedef struct _SCHEME_LIST
  29. {
  30. LIST_ENTRY le;
  31. UINT uiID;
  32. LPTSTR lpszName;
  33. LPTSTR lpszDesc;
  34. PPOWER_POLICY ppp;
  35. PMACHINE_PROCESSOR_POWER_POLICY pmppp;
  36. } SCHEME_LIST, *PSCHEME_LIST;
  37. // structure to manage the change parameters
  38. typedef struct _CHANGE_PARAM
  39. {
  40. BOOL bVideoTimeoutAc;
  41. ULONG ulVideoTimeoutAc;
  42. BOOL bVideoTimeoutDc;
  43. ULONG ulVideoTimeoutDc;
  44. BOOL bSpindownTimeoutAc;
  45. ULONG ulSpindownTimeoutAc;
  46. BOOL bSpindownTimeoutDc;
  47. ULONG ulSpindownTimeoutDc;
  48. BOOL bIdleTimeoutAc;
  49. ULONG ulIdleTimeoutAc;
  50. BOOL bIdleTimeoutDc;
  51. ULONG ulIdleTimeoutDc;
  52. BOOL bDozeS4TimeoutAc;
  53. ULONG ulDozeS4TimeoutAc;
  54. BOOL bDozeS4TimeoutDc;
  55. ULONG ulDozeS4TimeoutDc;
  56. BOOL bDynamicThrottleAc;
  57. LPTSTR lpszDynamicThrottleAc;
  58. BOOL bDynamicThrottleDc;
  59. LPTSTR lpszDynamicThrottleDc;
  60. } CHANGE_PARAM, *PCHANGE_PARAM;
  61. //
  62. // This structure is defined to allow the usage to be stored in
  63. // non-consecutive resource IDs so lines can be inserted without renumbering
  64. // the resources, which makes a lot of work for localization.
  65. //
  66. typedef struct _USAGE_ORDER
  67. {
  68. UINT InsertAfter;
  69. UINT FirstResource;
  70. UINT LastResource;
  71. } USAGE_ORDER, *PUSAGE_ORDER;
  72. // function types
  73. typedef BOOLEAN (*PWRITEPWRSCHEME_PROC)(PUINT,LPTSTR,LPTSTR,PPOWER_POLICY);
  74. typedef BOOLEAN (*PDELETEPWRSCHEME_PROC)(UINT);
  75. typedef BOOLEAN (*PGETACTIVEPWRSCHEME_PROC)(PUINT);
  76. typedef BOOLEAN (*PSETACTIVEPWRSCHEME_PROC)(UINT,PGLOBAL_POWER_POLICY,PPOWER_POLICY);
  77. typedef BOOLEAN (*PREADPROCESSORPWRSCHEME_PROC)(UINT,PMACHINE_PROCESSOR_POWER_POLICY);
  78. typedef BOOLEAN (*PWRITEPROCESSORPWRSCHEME_PROC)(UINT,PMACHINE_PROCESSOR_POWER_POLICY);
  79. typedef BOOLEAN (*PENUMPWRSCHEMES_PROC)(PWRSCHEMESENUMPROC,LPARAM);
  80. typedef BOOLEAN (*PGETPWRCAPABILITIES_PROC)(PSYSTEM_POWER_CAPABILITIES);
  81. typedef BOOLEAN (*PGETGLOBALPWRPOLICY_PROC)(PGLOBAL_POWER_POLICY);
  82. typedef BOOLEAN (*PGETCURRENTPOWERPOLICIES_PROC)(PGLOBAL_POWER_POLICY, PPOWER_POLICY);
  83. typedef BOOLEAN (*PWRITEGLOBALPWRPOLICY_PROC)(PGLOBAL_POWER_POLICY);
  84. typedef NTSTATUS (*PCALLNTPOWERINFORMATION_PROC)(POWER_INFORMATION_LEVEL, PVOID, ULONG, PVOID, ULONG);
  85. // forward decl's
  86. BOOL
  87. DoList();
  88. BOOL
  89. DoQuery(
  90. LPCTSTR lpszName,
  91. BOOL bNameSpecified,
  92. BOOL bNumerical
  93. );
  94. BOOL
  95. DoCreate(
  96. LPTSTR lpszName
  97. );
  98. BOOL
  99. DoDelete(
  100. LPCTSTR lpszName,
  101. BOOL bNumerical
  102. );
  103. BOOL
  104. DoSetActive(
  105. LPCTSTR lpszName,
  106. BOOL bNumerical
  107. );
  108. BOOL
  109. DoChange(
  110. LPCTSTR lpszName,
  111. BOOL bNumerical,
  112. PCHANGE_PARAM pcp
  113. );
  114. BOOL
  115. DoHibernate(
  116. LPCTSTR lpszBoolStr
  117. );
  118. BOOL
  119. DoGetSupportedSStates(
  120. VOID
  121. );
  122. BOOL
  123. DoGlobalFlag(
  124. LPCTSTR lpszBoolStr,
  125. LPCTSTR lpszGlobalFlagOption
  126. );
  127. BOOL
  128. DoExport(
  129. LPCTSTR lpszName,
  130. BOOL bNumerical,
  131. LPCTSTR lpszFile
  132. );
  133. BOOL
  134. DoImport(
  135. LPCTSTR lpszName,
  136. BOOL bNumerical,
  137. LPCTSTR lpszFile
  138. );
  139. BOOL
  140. DoBatteryAlarm(
  141. LPTSTR lpszName,
  142. LPTSTR lpszBoolStr,
  143. DWORD dwLevel,
  144. LPTSTR lpszAlarmTextBoolStr,
  145. LPTSTR lpszAlarmSoundBoolStr,
  146. LPTSTR lpszAlarmActionStr,
  147. LPTSTR lpszAlarmForceBoolStr,
  148. LPTSTR lpszAlarmProgramBoolStr
  149. );
  150. BOOL
  151. DoUsage();
  152. VOID
  153. SyncRegPPM();
  154. // global data
  155. LPCTSTR g_lpszErr = NULL_STRING; // string holding const error description
  156. LPTSTR g_lpszErr2 = NULL; // string holding dyn-alloc error msg
  157. TCHAR g_lpszBuf[256]; // formatting buffer
  158. BOOL g_bHiberFileSupported = FALSE; // true iff hiberfile supported
  159. BOOL g_bHiberTimerSupported = FALSE; // true iff hibertimer supported
  160. BOOL g_bHiberFilePresent = FALSE; // true if hibernate is enabled
  161. BOOL g_bStandbySupported = FALSE; // true iff standby supported
  162. BOOL g_bMonitorPowerSupported = FALSE; // true iff has power support
  163. BOOL g_bDiskPowerSupported = FALSE; // true iff has power support
  164. BOOL g_bThrottleSupported = FALSE; // true iff has throttle support
  165. BOOL g_bProcessorPwrSchemeSupported = FALSE; // true iff XP or later
  166. CONST LPTSTR g_szAlarmTaskName [NUM_DISCHARGE_POLICIES] = {
  167. _T("Critical Battery Alarm Program"),
  168. _T("Low Battery Alarm Program"),
  169. NULL,
  170. NULL
  171. };
  172. //
  173. // This global data is defined to allow the usage to be stored in
  174. // non-consecutive resource IDs so lines can be inserted without renumbering
  175. // the resources, which makes a lot of work for localization.
  176. //
  177. USAGE_ORDER gUsageOrder [] = {
  178. {IDS_USAGE_04, IDS_USAGE_04_1, IDS_USAGE_04_1},
  179. {IDS_USAGE_60, IDS_USAGE_60_01, IDS_USAGE_60_09},
  180. {IDS_USAGE_END+1, 0, 0}
  181. };
  182. // global function pointers from POWRPROF.DLL
  183. PWRITEPWRSCHEME_PROC fWritePwrScheme;
  184. PDELETEPWRSCHEME_PROC fDeletePwrScheme;
  185. PGETACTIVEPWRSCHEME_PROC fGetActivePwrScheme;
  186. PSETACTIVEPWRSCHEME_PROC fSetActivePwrScheme;
  187. PREADPROCESSORPWRSCHEME_PROC fReadProcessorPwrScheme;
  188. PWRITEPROCESSORPWRSCHEME_PROC fWriteProcessorPwrScheme;
  189. PENUMPWRSCHEMES_PROC fEnumPwrSchemes;
  190. PGETPWRCAPABILITIES_PROC fGetPwrCapabilities;
  191. PGETGLOBALPWRPOLICY_PROC fGetGlobalPwrPolicy;
  192. PWRITEGLOBALPWRPOLICY_PROC fWriteGlobalPwrPolicy;
  193. PCALLNTPOWERINFORMATION_PROC fCallNtPowerInformation;
  194. PGETCURRENTPOWERPOLICIES_PROC fGetCurrentPowerPolicies;
  195. // functions
  196. DWORD _cdecl
  197. _tmain(
  198. DWORD argc,
  199. LPCTSTR argv[]
  200. )
  201. /*++
  202. Routine Description:
  203. This routine is the main function. It parses parameters and takes
  204. apprpriate action.
  205. Arguments:
  206. argc - indicates the number of arguments
  207. argv - array of null terminated strings indicating arguments. See usage
  208. for actual meaning of arguments.
  209. Return Value:
  210. EXIT_SUCCESS if successful
  211. EXIT_FAILURE if something goes wrong
  212. --*/
  213. {
  214. // command line flags
  215. BOOL bList = FALSE;
  216. BOOL bQuery = FALSE;
  217. BOOL bCreate = FALSE;
  218. BOOL bDelete = FALSE;
  219. BOOL bSetActive = FALSE;
  220. BOOL bChange = FALSE;
  221. BOOL bHibernate = FALSE;
  222. BOOL bImport = FALSE;
  223. BOOL bExport = FALSE;
  224. BOOL bFile = FALSE;
  225. BOOL bUsage = FALSE;
  226. BOOL bNumerical = FALSE;
  227. BOOL bGlobalFlag = FALSE;
  228. BOOL bGetSupporedSStates = FALSE;
  229. BOOL bBatteryAlarm = FALSE;
  230. // error status
  231. BOOL bFail = FALSE;
  232. // dummy
  233. INT iDummy = 1;
  234. // DLL handle
  235. HINSTANCE hLib = NULL;
  236. // parse result value vars
  237. LPTSTR lpszName = NULL;
  238. LPTSTR lpszBoolStr = NULL;
  239. LPTSTR lpszFile = NULL;
  240. LPTSTR lpszThrottleAcStr = NULL;
  241. LPTSTR lpszThrottleDcStr = NULL;
  242. LPTSTR lpszGlobalFlagOption = NULL;
  243. DWORD dwAlarmLevel = 0xffffffff;
  244. LPTSTR lpszAlarmTextBoolStr = NULL;
  245. LPTSTR lpszAlarmSoundBoolStr = NULL;
  246. LPTSTR lpszAlarmActionStr = NULL;
  247. LPTSTR lpszAlarmForceBoolStr = NULL;
  248. LPTSTR lpszAlarmProgramBoolStr = NULL;
  249. CHANGE_PARAM tChangeParam;
  250. // parser info struct
  251. TCMDPARSER cmdOptions[NUM_CMDS];
  252. // system power caps struct
  253. SYSTEM_POWER_CAPABILITIES SysPwrCapabilities;
  254. // determine upper bound on input string length
  255. UINT uiMaxInLen = 0;
  256. DWORD dwIdx;
  257. for(dwIdx=1; dwIdx<argc; dwIdx++)
  258. {
  259. UINT uiCurLen = lstrlen(argv[dwIdx]);
  260. if (uiCurLen > uiMaxInLen)
  261. {
  262. uiMaxInLen = uiCurLen;
  263. }
  264. }
  265. // load POWRPROF.DLL
  266. hLib = LoadLibrary(_T("POWRPROF.DLL"));
  267. if(!hLib) {
  268. DISPLAY_MESSAGE(stderr,GetResString(IDS_DLL_LOAD_ERROR));
  269. return EXIT_FAILURE;
  270. }
  271. fWritePwrScheme = (PWRITEPWRSCHEME_PROC)GetProcAddress(hLib,"WritePwrScheme");
  272. fWriteProcessorPwrScheme = (PWRITEPROCESSORPWRSCHEME_PROC)GetProcAddress(hLib,"WriteProcessorPwrScheme");
  273. fReadProcessorPwrScheme = (PREADPROCESSORPWRSCHEME_PROC)GetProcAddress(hLib,"ReadProcessorPwrScheme");
  274. fEnumPwrSchemes = (PENUMPWRSCHEMES_PROC)GetProcAddress(hLib,"EnumPwrSchemes");
  275. fDeletePwrScheme = (PDELETEPWRSCHEME_PROC)GetProcAddress(hLib,"DeletePwrScheme");
  276. fGetActivePwrScheme = (PGETACTIVEPWRSCHEME_PROC)GetProcAddress(hLib,"GetActivePwrScheme");
  277. fSetActivePwrScheme = (PSETACTIVEPWRSCHEME_PROC)GetProcAddress(hLib,"SetActivePwrScheme");
  278. fGetPwrCapabilities = (PGETPWRCAPABILITIES_PROC)GetProcAddress(hLib,"GetPwrCapabilities");
  279. fGetGlobalPwrPolicy = (PGETGLOBALPWRPOLICY_PROC)GetProcAddress(hLib,"ReadGlobalPwrPolicy");
  280. fWriteGlobalPwrPolicy = (PWRITEGLOBALPWRPOLICY_PROC)GetProcAddress(hLib,"WriteGlobalPwrPolicy");
  281. fCallNtPowerInformation = (PCALLNTPOWERINFORMATION_PROC)GetProcAddress(hLib,"CallNtPowerInformation");
  282. fGetCurrentPowerPolicies = (PGETCURRENTPOWERPOLICIES_PROC)GetProcAddress(hLib,"GetCurrentPowerPolicies");
  283. if((!fWritePwrScheme) ||
  284. (!fEnumPwrSchemes) ||
  285. (!fDeletePwrScheme) ||
  286. (!fGetActivePwrScheme) ||
  287. (!fSetActivePwrScheme) ||
  288. (!fGetGlobalPwrPolicy) ||
  289. (!fWriteGlobalPwrPolicy) ||
  290. (!fGetPwrCapabilities) ||
  291. (!fCallNtPowerInformation) ||
  292. (!fGetCurrentPowerPolicies))
  293. {
  294. DISPLAY_MESSAGE(stderr,GetResString(IDS_DLL_PROC_ERROR));
  295. FreeLibrary(hLib);
  296. return EXIT_FAILURE;
  297. }
  298. g_bProcessorPwrSchemeSupported = fWriteProcessorPwrScheme && fReadProcessorPwrScheme;
  299. // Syncronize the data in the registry with the actual power policy.
  300. SyncRegPPM();
  301. // hook into cmdline.lib to allow Win2k operation
  302. SetOsVersion(5,0,0);
  303. // allocate space for scheme name and boolean string, and others strings
  304. lpszName = (LPTSTR)LocalAlloc(
  305. LPTR,
  306. (uiMaxInLen+1)*sizeof(TCHAR)
  307. );
  308. if (!lpszName)
  309. {
  310. DISPLAY_MESSAGE(stderr,GetResString(IDS_OUT_OF_MEMORY));
  311. FreeLibrary(hLib);
  312. return EXIT_FAILURE;
  313. }
  314. lpszBoolStr = (LPTSTR)LocalAlloc(LPTR,(uiMaxInLen+1)*sizeof(TCHAR));
  315. if (!lpszBoolStr)
  316. {
  317. LocalFree(lpszName);
  318. DISPLAY_MESSAGE(stderr,GetResString(IDS_OUT_OF_MEMORY));
  319. FreeLibrary(hLib);
  320. return EXIT_FAILURE;
  321. }
  322. if (uiMaxInLen < (UINT)lstrlen(GetResString(IDS_DEFAULT_FILENAME)))
  323. {
  324. lpszFile = (LPTSTR)LocalAlloc(
  325. LPTR,
  326. (lstrlen(GetResString(IDS_DEFAULT_FILENAME))+1)*sizeof(TCHAR)
  327. );
  328. }
  329. else
  330. {
  331. lpszFile = (LPTSTR)LocalAlloc(
  332. LPTR,
  333. (uiMaxInLen+1)*sizeof(TCHAR)
  334. );
  335. }
  336. if (!lpszFile)
  337. {
  338. LocalFree(lpszName);
  339. LocalFree(lpszBoolStr);
  340. DISPLAY_MESSAGE(stderr,GetResString(IDS_OUT_OF_MEMORY));
  341. FreeLibrary(hLib);
  342. return EXIT_FAILURE;
  343. }
  344. lpszThrottleAcStr = (LPTSTR)LocalAlloc(LPTR,(uiMaxInLen+1)*sizeof(TCHAR));
  345. if (!lpszThrottleAcStr)
  346. {
  347. LocalFree(lpszName);
  348. LocalFree(lpszBoolStr);
  349. LocalFree(lpszFile);
  350. DISPLAY_MESSAGE(stderr,GetResString(IDS_OUT_OF_MEMORY));
  351. FreeLibrary(hLib);
  352. return EXIT_FAILURE;
  353. }
  354. lpszThrottleDcStr = (LPTSTR)LocalAlloc(LPTR,(uiMaxInLen+1)*sizeof(TCHAR));
  355. if (!lpszThrottleDcStr)
  356. {
  357. LocalFree(lpszThrottleAcStr);
  358. LocalFree(lpszName);
  359. LocalFree(lpszBoolStr);
  360. LocalFree(lpszFile);
  361. DISPLAY_MESSAGE(stderr,GetResString(IDS_OUT_OF_MEMORY));
  362. FreeLibrary(hLib);
  363. return EXIT_FAILURE;
  364. }
  365. lpszGlobalFlagOption = (LPTSTR)LocalAlloc(LPTR,(uiMaxInLen+1)*sizeof(TCHAR));
  366. if (!lpszGlobalFlagOption)
  367. {
  368. LocalFree(lpszThrottleDcStr);
  369. LocalFree(lpszThrottleAcStr);
  370. LocalFree(lpszName);
  371. LocalFree(lpszBoolStr);
  372. LocalFree(lpszFile);
  373. DISPLAY_MESSAGE(stderr,GetResString(IDS_OUT_OF_MEMORY));
  374. FreeLibrary(hLib);
  375. return EXIT_FAILURE;
  376. }
  377. lpszAlarmTextBoolStr = (LPTSTR)LocalAlloc(LPTR,(uiMaxInLen+1)*sizeof(TCHAR));
  378. if (!lpszAlarmTextBoolStr)
  379. {
  380. LocalFree(lpszGlobalFlagOption);
  381. LocalFree(lpszThrottleDcStr);
  382. LocalFree(lpszThrottleAcStr);
  383. LocalFree(lpszName);
  384. LocalFree(lpszBoolStr);
  385. LocalFree(lpszFile);
  386. DISPLAY_MESSAGE(stderr,GetResString(IDS_OUT_OF_MEMORY));
  387. FreeLibrary(hLib);
  388. return EXIT_FAILURE;
  389. }
  390. lpszAlarmSoundBoolStr = (LPTSTR)LocalAlloc(LPTR,(uiMaxInLen+1)*sizeof(TCHAR));
  391. if (!lpszAlarmTextBoolStr)
  392. {
  393. LocalFree(lpszAlarmTextBoolStr);
  394. LocalFree(lpszGlobalFlagOption);
  395. LocalFree(lpszThrottleDcStr);
  396. LocalFree(lpszThrottleAcStr);
  397. LocalFree(lpszName);
  398. LocalFree(lpszBoolStr);
  399. LocalFree(lpszFile);
  400. DISPLAY_MESSAGE(stderr,GetResString(IDS_OUT_OF_MEMORY));
  401. FreeLibrary(hLib);
  402. return EXIT_FAILURE;
  403. }
  404. lpszAlarmActionStr = (LPTSTR)LocalAlloc(LPTR,(uiMaxInLen+1)*sizeof(TCHAR));
  405. if (!lpszAlarmActionStr)
  406. {
  407. LocalFree(lpszAlarmSoundBoolStr);
  408. LocalFree(lpszAlarmTextBoolStr);
  409. LocalFree(lpszGlobalFlagOption);
  410. LocalFree(lpszThrottleDcStr);
  411. LocalFree(lpszThrottleAcStr);
  412. LocalFree(lpszName);
  413. LocalFree(lpszBoolStr);
  414. LocalFree(lpszFile);
  415. DISPLAY_MESSAGE(stderr,GetResString(IDS_OUT_OF_MEMORY));
  416. FreeLibrary(hLib);
  417. return EXIT_FAILURE;
  418. }
  419. lpszAlarmForceBoolStr = (LPTSTR)LocalAlloc(LPTR,(uiMaxInLen+1)*sizeof(TCHAR));
  420. if (!lpszAlarmForceBoolStr)
  421. {
  422. LocalFree(lpszAlarmActionStr);
  423. LocalFree(lpszAlarmSoundBoolStr);
  424. LocalFree(lpszAlarmTextBoolStr);
  425. LocalFree(lpszGlobalFlagOption);
  426. LocalFree(lpszThrottleDcStr);
  427. LocalFree(lpszThrottleAcStr);
  428. LocalFree(lpszName);
  429. LocalFree(lpszBoolStr);
  430. LocalFree(lpszFile);
  431. DISPLAY_MESSAGE(stderr,GetResString(IDS_OUT_OF_MEMORY));
  432. FreeLibrary(hLib);
  433. return EXIT_FAILURE;
  434. }
  435. lpszAlarmProgramBoolStr = (LPTSTR)LocalAlloc(LPTR,(uiMaxInLen+1)*sizeof(TCHAR));
  436. if (!lpszAlarmProgramBoolStr)
  437. {
  438. LocalFree(lpszAlarmForceBoolStr);
  439. LocalFree(lpszAlarmActionStr);
  440. LocalFree(lpszAlarmSoundBoolStr);
  441. LocalFree(lpszAlarmTextBoolStr);
  442. LocalFree(lpszGlobalFlagOption);
  443. LocalFree(lpszThrottleDcStr);
  444. LocalFree(lpszThrottleAcStr);
  445. LocalFree(lpszName);
  446. LocalFree(lpszBoolStr);
  447. LocalFree(lpszFile);
  448. DISPLAY_MESSAGE(stderr,GetResString(IDS_OUT_OF_MEMORY));
  449. FreeLibrary(hLib);
  450. return EXIT_FAILURE;
  451. }
  452. // initialize the allocated strings
  453. lstrcpy(lpszName,NULL_STRING);
  454. lstrcpy(lpszFile,GetResString(IDS_DEFAULT_FILENAME));
  455. lstrcpy(lpszThrottleAcStr,NULL_STRING);
  456. lstrcpy(lpszThrottleAcStr,NULL_STRING);
  457. lstrcpy(lpszThrottleDcStr,NULL_STRING);
  458. lstrcpy(lpszGlobalFlagOption,NULL_STRING);
  459. lstrcpy(lpszAlarmTextBoolStr,NULL_STRING);
  460. lstrcpy(lpszAlarmSoundBoolStr,NULL_STRING);
  461. lstrcpy(lpszAlarmActionStr,NULL_STRING);
  462. lstrcpy(lpszAlarmForceBoolStr,NULL_STRING);
  463. lstrcpy(lpszAlarmProgramBoolStr,NULL_STRING);
  464. // determine system capabilities
  465. if (fGetPwrCapabilities(&SysPwrCapabilities))
  466. {
  467. g_bHiberFileSupported = SysPwrCapabilities.SystemS4;
  468. g_bHiberTimerSupported =
  469. (SysPwrCapabilities.RtcWake >= PowerSystemHibernate);
  470. g_bHiberFilePresent = SysPwrCapabilities.HiberFilePresent;
  471. g_bStandbySupported = SysPwrCapabilities.SystemS1 |
  472. SysPwrCapabilities.SystemS2 |
  473. SysPwrCapabilities.SystemS3;
  474. g_bDiskPowerSupported = SysPwrCapabilities.DiskSpinDown;
  475. g_bThrottleSupported = SysPwrCapabilities.ProcessorThrottle;
  476. g_bMonitorPowerSupported = SystemParametersInfo(
  477. SPI_GETLOWPOWERACTIVE,
  478. 0,
  479. &iDummy,
  480. 0
  481. );
  482. if (!g_bMonitorPowerSupported ) {
  483. g_bMonitorPowerSupported = SystemParametersInfo(
  484. SPI_GETPOWEROFFACTIVE,
  485. 0,
  486. &iDummy,
  487. 0
  488. );
  489. }
  490. }
  491. else
  492. {
  493. g_lpszErr = GetResString(IDS_UNEXPECTED_ERROR);
  494. LocalFree(lpszAlarmProgramBoolStr);
  495. LocalFree(lpszAlarmForceBoolStr);
  496. LocalFree(lpszAlarmActionStr);
  497. LocalFree(lpszAlarmSoundBoolStr);
  498. LocalFree(lpszAlarmTextBoolStr);
  499. LocalFree(lpszGlobalFlagOption);
  500. LocalFree(lpszThrottleDcStr);
  501. LocalFree(lpszThrottleAcStr);
  502. LocalFree(lpszName);
  503. LocalFree(lpszBoolStr);
  504. LocalFree(lpszFile);
  505. FreeLibrary(hLib);
  506. return EXIT_FAILURE;
  507. }
  508. //fill in the TCMDPARSER array
  509. // option 'list'
  510. cmdOptions[CMDINDEX_LIST].dwFlags = CP_MAIN_OPTION;
  511. cmdOptions[CMDINDEX_LIST].dwCount = 1;
  512. cmdOptions[CMDINDEX_LIST].dwActuals = 0;
  513. cmdOptions[CMDINDEX_LIST].pValue = &bList;
  514. cmdOptions[CMDINDEX_LIST].pFunction = NULL;
  515. cmdOptions[CMDINDEX_LIST].pFunctionData = NULL;
  516. lstrcpy(
  517. cmdOptions[CMDINDEX_LIST].szOption,
  518. CMDOPTION_LIST
  519. );
  520. lstrcpy(
  521. cmdOptions[CMDINDEX_LIST].szValues,
  522. NULL_STRING
  523. );
  524. // option 'query'
  525. cmdOptions[CMDINDEX_QUERY].dwFlags = CP_TYPE_TEXT |
  526. CP_VALUE_OPTIONAL |
  527. CP_MAIN_OPTION;
  528. cmdOptions[CMDINDEX_QUERY].dwCount = 1;
  529. cmdOptions[CMDINDEX_QUERY].dwActuals = 0;
  530. cmdOptions[CMDINDEX_QUERY].pValue = lpszName;
  531. cmdOptions[CMDINDEX_QUERY].pFunction = NULL;
  532. cmdOptions[CMDINDEX_QUERY].pFunctionData = NULL;
  533. lstrcpy(
  534. cmdOptions[CMDINDEX_QUERY].szOption,
  535. CMDOPTION_QUERY
  536. );
  537. lstrcpy(
  538. cmdOptions[CMDINDEX_QUERY].szValues,
  539. NULL_STRING
  540. );
  541. // option 'create'
  542. cmdOptions[CMDINDEX_CREATE].dwFlags = CP_TYPE_TEXT |
  543. CP_VALUE_MANDATORY |
  544. CP_MAIN_OPTION;
  545. cmdOptions[CMDINDEX_CREATE].dwCount = 1;
  546. cmdOptions[CMDINDEX_CREATE].dwActuals = 0;
  547. cmdOptions[CMDINDEX_CREATE].pValue = lpszName;
  548. cmdOptions[CMDINDEX_CREATE].pFunction = NULL;
  549. cmdOptions[CMDINDEX_CREATE].pFunctionData = NULL;
  550. lstrcpy(
  551. cmdOptions[CMDINDEX_CREATE].szOption,
  552. CMDOPTION_CREATE
  553. );
  554. lstrcpy(
  555. cmdOptions[CMDINDEX_CREATE].szValues,
  556. NULL_STRING
  557. );
  558. // option 'delete'
  559. cmdOptions[CMDINDEX_DELETE].dwFlags = CP_TYPE_TEXT |
  560. CP_VALUE_MANDATORY |
  561. CP_MAIN_OPTION;
  562. cmdOptions[CMDINDEX_DELETE].dwCount = 1;
  563. cmdOptions[CMDINDEX_DELETE].dwActuals = 0;
  564. cmdOptions[CMDINDEX_DELETE].pValue = lpszName;
  565. cmdOptions[CMDINDEX_DELETE].pFunction = NULL;
  566. cmdOptions[CMDINDEX_DELETE].pFunctionData = NULL;
  567. lstrcpy(
  568. cmdOptions[CMDINDEX_DELETE].szOption,
  569. CMDOPTION_DELETE
  570. );
  571. lstrcpy(
  572. cmdOptions[CMDINDEX_DELETE].szValues,
  573. NULL_STRING
  574. );
  575. // option 'setactive'
  576. cmdOptions[CMDINDEX_SETACTIVE].dwFlags = CP_TYPE_TEXT |
  577. CP_VALUE_MANDATORY |
  578. CP_MAIN_OPTION;
  579. cmdOptions[CMDINDEX_SETACTIVE].dwCount = 1;
  580. cmdOptions[CMDINDEX_SETACTIVE].dwActuals = 0;
  581. cmdOptions[CMDINDEX_SETACTIVE].pValue = lpszName;
  582. cmdOptions[CMDINDEX_SETACTIVE].pFunction = NULL;
  583. cmdOptions[CMDINDEX_SETACTIVE].pFunctionData = NULL;
  584. lstrcpy(
  585. cmdOptions[CMDINDEX_SETACTIVE].szOption,
  586. CMDOPTION_SETACTIVE
  587. );
  588. lstrcpy(
  589. cmdOptions[CMDINDEX_SETACTIVE].szValues,
  590. NULL_STRING
  591. );
  592. // option 'change'
  593. cmdOptions[CMDINDEX_CHANGE].dwFlags = CP_TYPE_TEXT |
  594. CP_VALUE_MANDATORY |
  595. CP_MAIN_OPTION;
  596. cmdOptions[CMDINDEX_CHANGE].dwCount = 1;
  597. cmdOptions[CMDINDEX_CHANGE].dwActuals = 0;
  598. cmdOptions[CMDINDEX_CHANGE].pValue = lpszName;
  599. cmdOptions[CMDINDEX_CHANGE].pFunction = NULL;
  600. cmdOptions[CMDINDEX_CHANGE].pFunctionData = NULL;
  601. lstrcpy(
  602. cmdOptions[CMDINDEX_CHANGE].szOption,
  603. CMDOPTION_CHANGE
  604. );
  605. lstrcpy(
  606. cmdOptions[CMDINDEX_CHANGE].szValues,
  607. NULL_STRING
  608. );
  609. // option 'hibernate'
  610. cmdOptions[CMDINDEX_HIBERNATE].dwFlags = CP_TYPE_TEXT |
  611. CP_VALUE_MANDATORY |
  612. CP_MAIN_OPTION;
  613. cmdOptions[CMDINDEX_HIBERNATE].dwCount = 1;
  614. cmdOptions[CMDINDEX_HIBERNATE].dwActuals = 0;
  615. cmdOptions[CMDINDEX_HIBERNATE].pValue = lpszBoolStr;
  616. cmdOptions[CMDINDEX_HIBERNATE].pFunction = NULL;
  617. cmdOptions[CMDINDEX_HIBERNATE].pFunctionData = NULL;
  618. lstrcpy(
  619. cmdOptions[CMDINDEX_HIBERNATE].szOption,
  620. CMDOPTION_HIBERNATE
  621. );
  622. lstrcpy(
  623. cmdOptions[CMDINDEX_HIBERNATE].szValues,
  624. NULL_STRING
  625. );
  626. // option 'getsstates'
  627. cmdOptions[CMDINDEX_SSTATES].dwFlags = CP_MAIN_OPTION;
  628. cmdOptions[CMDINDEX_SSTATES].dwCount = 1;
  629. cmdOptions[CMDINDEX_SSTATES].dwActuals = 0;
  630. cmdOptions[CMDINDEX_SSTATES].pValue = &bGetSupporedSStates;
  631. cmdOptions[CMDINDEX_SSTATES].pFunction = NULL;
  632. cmdOptions[CMDINDEX_SSTATES].pFunctionData = NULL;
  633. lstrcpy(
  634. cmdOptions[CMDINDEX_SSTATES].szOption,
  635. CMDOPTION_SSTATES
  636. );
  637. lstrcpy(
  638. cmdOptions[CMDINDEX_SSTATES].szValues,
  639. NULL_STRING
  640. );
  641. // option 'export'
  642. cmdOptions[CMDINDEX_EXPORT].dwFlags = CP_TYPE_TEXT |
  643. CP_VALUE_MANDATORY |
  644. CP_MAIN_OPTION;
  645. cmdOptions[CMDINDEX_EXPORT].dwCount = 1;
  646. cmdOptions[CMDINDEX_EXPORT].dwActuals = 0;
  647. cmdOptions[CMDINDEX_EXPORT].pValue = lpszName;
  648. cmdOptions[CMDINDEX_EXPORT].pFunction = NULL;
  649. cmdOptions[CMDINDEX_EXPORT].pFunctionData = NULL;
  650. lstrcpy(
  651. cmdOptions[CMDINDEX_EXPORT].szOption,
  652. CMDOPTION_EXPORT
  653. );
  654. lstrcpy(
  655. cmdOptions[CMDINDEX_EXPORT].szValues,
  656. NULL_STRING
  657. );
  658. // option 'import'
  659. cmdOptions[CMDINDEX_IMPORT].dwFlags = CP_TYPE_TEXT |
  660. CP_VALUE_MANDATORY |
  661. CP_MAIN_OPTION;
  662. cmdOptions[CMDINDEX_IMPORT].dwCount = 1;
  663. cmdOptions[CMDINDEX_IMPORT].dwActuals = 0;
  664. cmdOptions[CMDINDEX_IMPORT].pValue = lpszName;
  665. cmdOptions[CMDINDEX_IMPORT].pFunction = NULL;
  666. cmdOptions[CMDINDEX_IMPORT].pFunctionData = NULL;
  667. lstrcpy(
  668. cmdOptions[CMDINDEX_IMPORT].szOption,
  669. CMDOPTION_IMPORT
  670. );
  671. lstrcpy(
  672. cmdOptions[CMDINDEX_IMPORT].szValues,
  673. NULL_STRING
  674. );
  675. // option 'usage'
  676. cmdOptions[CMDINDEX_USAGE].dwFlags = CP_USAGE |
  677. CP_MAIN_OPTION;
  678. cmdOptions[CMDINDEX_USAGE].dwCount = 1;
  679. cmdOptions[CMDINDEX_USAGE].dwActuals = 0;
  680. cmdOptions[CMDINDEX_USAGE].pValue = &bUsage;
  681. cmdOptions[CMDINDEX_USAGE].pFunction = NULL;
  682. cmdOptions[CMDINDEX_USAGE].pFunctionData = NULL;
  683. lstrcpy(
  684. cmdOptions[CMDINDEX_USAGE].szOption,
  685. CMDOPTION_USAGE
  686. );
  687. lstrcpy(
  688. cmdOptions[CMDINDEX_USAGE].szValues,
  689. NULL_STRING
  690. );
  691. // sub-option 'numerical'
  692. cmdOptions[CMDINDEX_NUMERICAL].dwFlags = 0;
  693. cmdOptions[CMDINDEX_NUMERICAL].dwCount = 1;
  694. cmdOptions[CMDINDEX_NUMERICAL].dwActuals = 0;
  695. cmdOptions[CMDINDEX_NUMERICAL].pValue = &bNumerical;
  696. cmdOptions[CMDINDEX_NUMERICAL].pFunction = NULL;
  697. cmdOptions[CMDINDEX_NUMERICAL].pFunctionData = NULL;
  698. lstrcpy(
  699. cmdOptions[CMDINDEX_NUMERICAL].szOption,
  700. CMDOPTION_NUMERICAL
  701. );
  702. lstrcpy(
  703. cmdOptions[CMDINDEX_NUMERICAL].szValues,
  704. NULL_STRING
  705. );
  706. // sub-option 'monitor-timeout-ac'
  707. cmdOptions[CMDINDEX_MONITOR_OFF_AC].dwFlags = CP_TYPE_UNUMERIC |
  708. CP_VALUE_MANDATORY;
  709. cmdOptions[CMDINDEX_MONITOR_OFF_AC].dwCount = 1;
  710. cmdOptions[CMDINDEX_MONITOR_OFF_AC].dwActuals = 0;
  711. cmdOptions[CMDINDEX_MONITOR_OFF_AC].pValue =
  712. &tChangeParam.ulVideoTimeoutAc;
  713. cmdOptions[CMDINDEX_MONITOR_OFF_AC].pFunction = NULL;
  714. cmdOptions[CMDINDEX_MONITOR_OFF_AC].pFunctionData = NULL;
  715. lstrcpy(
  716. cmdOptions[CMDINDEX_MONITOR_OFF_AC].szOption,
  717. CMDOPTION_MONITOR_OFF_AC
  718. );
  719. lstrcpy(
  720. cmdOptions[CMDINDEX_MONITOR_OFF_AC].szValues,
  721. NULL_STRING
  722. );
  723. // sub-option 'monitor-timeout-dc'
  724. cmdOptions[CMDINDEX_MONITOR_OFF_DC].dwFlags = CP_TYPE_UNUMERIC |
  725. CP_VALUE_MANDATORY;
  726. cmdOptions[CMDINDEX_MONITOR_OFF_DC].dwCount = 1;
  727. cmdOptions[CMDINDEX_MONITOR_OFF_DC].dwActuals = 0;
  728. cmdOptions[CMDINDEX_MONITOR_OFF_DC].pValue =
  729. &tChangeParam.ulVideoTimeoutDc;
  730. cmdOptions[CMDINDEX_MONITOR_OFF_DC].pFunction = NULL;
  731. cmdOptions[CMDINDEX_MONITOR_OFF_DC].pFunctionData = NULL;
  732. lstrcpy(
  733. cmdOptions[CMDINDEX_MONITOR_OFF_DC].szOption,
  734. CMDOPTION_MONITOR_OFF_DC
  735. );
  736. lstrcpy(
  737. cmdOptions[CMDINDEX_MONITOR_OFF_DC].szValues,
  738. NULL_STRING
  739. );
  740. // sub-option 'disk-timeout-ac'
  741. cmdOptions[CMDINDEX_DISK_OFF_AC].dwFlags = CP_TYPE_UNUMERIC |
  742. CP_VALUE_MANDATORY;
  743. cmdOptions[CMDINDEX_DISK_OFF_AC].dwCount = 1;
  744. cmdOptions[CMDINDEX_DISK_OFF_AC].dwActuals = 0;
  745. cmdOptions[CMDINDEX_DISK_OFF_AC].pValue =
  746. &tChangeParam.ulSpindownTimeoutAc;
  747. cmdOptions[CMDINDEX_DISK_OFF_AC].pFunction = NULL;
  748. cmdOptions[CMDINDEX_DISK_OFF_AC].pFunctionData = NULL;
  749. lstrcpy(
  750. cmdOptions[CMDINDEX_DISK_OFF_AC].szOption,
  751. CMDOPTION_DISK_OFF_AC
  752. );
  753. lstrcpy(
  754. cmdOptions[CMDINDEX_DISK_OFF_AC].szValues,
  755. NULL_STRING
  756. );
  757. // sub-option 'disk-timeout-dc'
  758. cmdOptions[CMDINDEX_DISK_OFF_DC].dwFlags = CP_TYPE_UNUMERIC |
  759. CP_VALUE_MANDATORY;
  760. cmdOptions[CMDINDEX_DISK_OFF_DC].dwCount = 1;
  761. cmdOptions[CMDINDEX_DISK_OFF_DC].dwActuals = 0;
  762. cmdOptions[CMDINDEX_DISK_OFF_DC].pValue =
  763. &tChangeParam.ulSpindownTimeoutDc;
  764. cmdOptions[CMDINDEX_DISK_OFF_DC].pFunction = NULL;
  765. cmdOptions[CMDINDEX_DISK_OFF_DC].pFunctionData = NULL;
  766. lstrcpy(
  767. cmdOptions[CMDINDEX_DISK_OFF_DC].szOption,
  768. CMDOPTION_DISK_OFF_DC
  769. );
  770. lstrcpy(
  771. cmdOptions[CMDINDEX_DISK_OFF_DC].szValues,
  772. NULL_STRING
  773. );
  774. // sub-option 'standby-timeout-ac'
  775. cmdOptions[CMDINDEX_STANDBY_AC].dwFlags = CP_TYPE_UNUMERIC |
  776. CP_VALUE_MANDATORY;
  777. cmdOptions[CMDINDEX_STANDBY_AC].dwCount = 1;
  778. cmdOptions[CMDINDEX_STANDBY_AC].dwActuals = 0;
  779. cmdOptions[CMDINDEX_STANDBY_AC].pValue =
  780. &tChangeParam.ulIdleTimeoutAc;
  781. cmdOptions[CMDINDEX_STANDBY_AC].pFunction = NULL;
  782. cmdOptions[CMDINDEX_STANDBY_AC].pFunctionData = NULL;
  783. lstrcpy(
  784. cmdOptions[CMDINDEX_STANDBY_AC].szOption,
  785. CMDOPTION_STANDBY_AC
  786. );
  787. lstrcpy(
  788. cmdOptions[CMDINDEX_STANDBY_AC].szValues,
  789. NULL_STRING
  790. );
  791. // sub-option 'standby-timeout-dc'
  792. cmdOptions[CMDINDEX_STANDBY_DC].dwFlags = CP_TYPE_UNUMERIC |
  793. CP_VALUE_MANDATORY;
  794. cmdOptions[CMDINDEX_STANDBY_DC].dwCount = 1;
  795. cmdOptions[CMDINDEX_STANDBY_DC].dwActuals = 0;
  796. cmdOptions[CMDINDEX_STANDBY_DC].pValue =
  797. &tChangeParam.ulIdleTimeoutDc;
  798. cmdOptions[CMDINDEX_STANDBY_DC].pFunction = NULL;
  799. cmdOptions[CMDINDEX_STANDBY_DC].pFunctionData = NULL;
  800. lstrcpy(
  801. cmdOptions[CMDINDEX_STANDBY_DC].szOption,
  802. CMDOPTION_STANDBY_DC
  803. );
  804. lstrcpy(
  805. cmdOptions[CMDINDEX_STANDBY_DC].szValues,
  806. NULL_STRING
  807. );
  808. // sub-option 'hibernate-timeout-ac'
  809. cmdOptions[CMDINDEX_HIBER_AC].dwFlags = CP_TYPE_UNUMERIC |
  810. CP_VALUE_MANDATORY;
  811. cmdOptions[CMDINDEX_HIBER_AC].dwCount = 1;
  812. cmdOptions[CMDINDEX_HIBER_AC].dwActuals = 0;
  813. cmdOptions[CMDINDEX_HIBER_AC].pValue =
  814. &tChangeParam.ulDozeS4TimeoutAc;
  815. cmdOptions[CMDINDEX_HIBER_AC].pFunction = NULL;
  816. cmdOptions[CMDINDEX_HIBER_AC].pFunctionData = NULL;
  817. lstrcpy(
  818. cmdOptions[CMDINDEX_HIBER_AC].szOption,
  819. CMDOPTION_HIBER_AC
  820. );
  821. lstrcpy(
  822. cmdOptions[CMDINDEX_HIBER_AC].szValues,
  823. NULL_STRING
  824. );
  825. // sub-option 'hibernate-timeout-dc'
  826. cmdOptions[CMDINDEX_HIBER_DC].dwFlags = CP_TYPE_UNUMERIC |
  827. CP_VALUE_MANDATORY;
  828. cmdOptions[CMDINDEX_HIBER_DC].dwCount = 1;
  829. cmdOptions[CMDINDEX_HIBER_DC].dwActuals = 0;
  830. cmdOptions[CMDINDEX_HIBER_DC].pValue =
  831. &tChangeParam.ulDozeS4TimeoutDc;
  832. cmdOptions[CMDINDEX_HIBER_DC].pFunction = NULL;
  833. cmdOptions[CMDINDEX_HIBER_DC].pFunctionData = NULL;
  834. lstrcpy(
  835. cmdOptions[CMDINDEX_HIBER_DC].szOption,
  836. CMDOPTION_HIBER_DC
  837. );
  838. lstrcpy(
  839. cmdOptions[CMDINDEX_HIBER_DC].szValues,
  840. NULL_STRING
  841. );
  842. // sub-option 'processor-throttle-ac'
  843. cmdOptions[CMDINDEX_THROTTLE_AC].dwFlags = CP_TYPE_TEXT |
  844. CP_VALUE_MANDATORY;
  845. cmdOptions[CMDINDEX_THROTTLE_AC].dwCount = 1;
  846. cmdOptions[CMDINDEX_THROTTLE_AC].dwActuals = 0;
  847. cmdOptions[CMDINDEX_THROTTLE_AC].pValue = lpszThrottleAcStr;
  848. cmdOptions[CMDINDEX_THROTTLE_AC].pFunction = NULL;
  849. cmdOptions[CMDINDEX_THROTTLE_AC].pFunctionData = NULL;
  850. lstrcpy(
  851. cmdOptions[CMDINDEX_THROTTLE_AC].szOption,
  852. CMDOPTION_THROTTLE_AC
  853. );
  854. lstrcpy(
  855. cmdOptions[CMDINDEX_THROTTLE_AC].szValues,
  856. NULL_STRING
  857. );
  858. // sub-option 'processor-throttle-dc'
  859. cmdOptions[CMDINDEX_THROTTLE_DC].dwFlags = CP_TYPE_TEXT |
  860. CP_VALUE_MANDATORY;
  861. cmdOptions[CMDINDEX_THROTTLE_DC].dwCount = 1;
  862. cmdOptions[CMDINDEX_THROTTLE_DC].dwActuals = 0;
  863. cmdOptions[CMDINDEX_THROTTLE_DC].pValue = lpszThrottleDcStr;
  864. cmdOptions[CMDINDEX_THROTTLE_DC].pFunction = NULL;
  865. cmdOptions[CMDINDEX_THROTTLE_DC].pFunctionData = NULL;
  866. lstrcpy(
  867. cmdOptions[CMDINDEX_THROTTLE_DC].szOption,
  868. CMDOPTION_THROTTLE_DC
  869. );
  870. lstrcpy(
  871. cmdOptions[CMDINDEX_THROTTLE_DC].szValues,
  872. NULL_STRING
  873. );
  874. // sub-option 'file'
  875. cmdOptions[CMDINDEX_FILE].dwFlags = CP_TYPE_TEXT |
  876. CP_VALUE_MANDATORY;
  877. cmdOptions[CMDINDEX_FILE].dwCount = 1;
  878. cmdOptions[CMDINDEX_FILE].dwActuals = 0;
  879. cmdOptions[CMDINDEX_FILE].pValue = lpszFile;
  880. cmdOptions[CMDINDEX_FILE].pFunction = NULL;
  881. cmdOptions[CMDINDEX_FILE].pFunctionData = NULL;
  882. lstrcpy(
  883. cmdOptions[CMDINDEX_FILE].szOption,
  884. CMDOPTION_FILE
  885. );
  886. lstrcpy(
  887. cmdOptions[CMDINDEX_FILE].szValues,
  888. NULL_STRING
  889. );
  890. // option 'globalpowerflag'
  891. cmdOptions[CMDINDEX_GLOBALFLAG].dwFlags = CP_TYPE_TEXT |
  892. CP_VALUE_MANDATORY |
  893. CP_MAIN_OPTION;
  894. cmdOptions[CMDINDEX_GLOBALFLAG].dwCount = 1;
  895. cmdOptions[CMDINDEX_GLOBALFLAG].dwActuals = 0;
  896. cmdOptions[CMDINDEX_GLOBALFLAG].pValue = lpszBoolStr;
  897. cmdOptions[CMDINDEX_GLOBALFLAG].pFunction = NULL;
  898. cmdOptions[CMDINDEX_GLOBALFLAG].pFunctionData = NULL;
  899. lstrcpy(
  900. cmdOptions[CMDINDEX_GLOBALFLAG].szOption,
  901. CMDOPTION_GLOBALFLAG
  902. );
  903. lstrcpy(
  904. cmdOptions[CMDINDEX_GLOBALFLAG].szValues,
  905. NULL_STRING
  906. );
  907. // globalflag sub-option 'OPTION'
  908. cmdOptions[CMDINDEX_POWEROPTION].dwFlags = CP_TYPE_TEXT |
  909. CP_VALUE_MANDATORY;
  910. cmdOptions[CMDINDEX_POWEROPTION].dwCount = 1;
  911. cmdOptions[CMDINDEX_POWEROPTION].dwActuals = 0;
  912. cmdOptions[CMDINDEX_POWEROPTION].pValue = lpszGlobalFlagOption;
  913. cmdOptions[CMDINDEX_POWEROPTION].pFunction = NULL;
  914. cmdOptions[CMDINDEX_POWEROPTION].pFunctionData = NULL;
  915. lstrcpy(
  916. cmdOptions[CMDINDEX_POWEROPTION].szOption,
  917. CMDOPTION_POWEROPTION
  918. );
  919. lstrcpy(
  920. cmdOptions[CMDINDEX_POWEROPTION].szValues,
  921. NULL_STRING
  922. );
  923. // option 'batteryalarm'
  924. cmdOptions[CMDINDEX_BATTERYALARM].dwFlags = CP_TYPE_TEXT |
  925. CP_VALUE_MANDATORY |
  926. CP_MAIN_OPTION;
  927. cmdOptions[CMDINDEX_BATTERYALARM].dwCount = 1;
  928. cmdOptions[CMDINDEX_BATTERYALARM].dwActuals = 0;
  929. cmdOptions[CMDINDEX_BATTERYALARM].pValue = lpszName;
  930. cmdOptions[CMDINDEX_BATTERYALARM].pFunction = NULL;
  931. cmdOptions[CMDINDEX_BATTERYALARM].pFunctionData = NULL;
  932. lstrcpy(
  933. cmdOptions[CMDINDEX_BATTERYALARM].szOption,
  934. CMDOPTION_BATTERYALARM
  935. );
  936. lstrcpy(
  937. cmdOptions[CMDINDEX_BATTERYALARM].szValues,
  938. NULL_STRING
  939. );
  940. // batteryalarm sub-option 'ACTIVATE'
  941. cmdOptions[CMDINDEX_ALARMACTIVE].dwFlags = CP_TYPE_TEXT |
  942. CP_VALUE_MANDATORY;
  943. cmdOptions[CMDINDEX_ALARMACTIVE].dwCount = 1;
  944. cmdOptions[CMDINDEX_ALARMACTIVE].dwActuals = 0;
  945. cmdOptions[CMDINDEX_ALARMACTIVE].pValue = lpszBoolStr;
  946. cmdOptions[CMDINDEX_ALARMACTIVE].pFunction = NULL;
  947. cmdOptions[CMDINDEX_ALARMACTIVE].pFunctionData = NULL;
  948. lstrcpy(
  949. cmdOptions[CMDINDEX_ALARMACTIVE].szOption,
  950. CMDOPTION_ALARMACTIVE
  951. );
  952. lstrcpy(
  953. cmdOptions[CMDINDEX_ALARMACTIVE].szValues,
  954. NULL_STRING
  955. );
  956. // batteryalarm sub-option 'LEVEL'
  957. cmdOptions[CMDINDEX_ALARMLEVEL].dwFlags = CP_TYPE_UNUMERIC |
  958. CP_VALUE_MANDATORY;
  959. cmdOptions[CMDINDEX_ALARMLEVEL].dwCount = 1;
  960. cmdOptions[CMDINDEX_ALARMLEVEL].dwActuals = 0;
  961. cmdOptions[CMDINDEX_ALARMLEVEL].pValue = &dwAlarmLevel;
  962. cmdOptions[CMDINDEX_ALARMLEVEL].pFunction = NULL;
  963. cmdOptions[CMDINDEX_ALARMLEVEL].pFunctionData = NULL;
  964. lstrcpy(
  965. cmdOptions[CMDINDEX_ALARMLEVEL].szOption,
  966. CMDOPTION_ALARMLEVEL
  967. );
  968. lstrcpy(
  969. cmdOptions[CMDINDEX_ALARMLEVEL].szValues,
  970. NULL_STRING
  971. );
  972. // batteryalarm sub-option 'TEXT'
  973. cmdOptions[CMDINDEX_ALARMTEXT].dwFlags = CP_TYPE_TEXT |
  974. CP_VALUE_MANDATORY;
  975. cmdOptions[CMDINDEX_ALARMTEXT].dwCount = 1;
  976. cmdOptions[CMDINDEX_ALARMTEXT].dwActuals = 0;
  977. cmdOptions[CMDINDEX_ALARMTEXT].pValue = lpszAlarmTextBoolStr;
  978. cmdOptions[CMDINDEX_ALARMTEXT].pFunction = NULL;
  979. cmdOptions[CMDINDEX_ALARMTEXT].pFunctionData = NULL;
  980. lstrcpy(
  981. cmdOptions[CMDINDEX_ALARMTEXT].szOption,
  982. CMDOPTION_ALARMTEXT
  983. );
  984. lstrcpy(
  985. cmdOptions[CMDINDEX_ALARMTEXT].szValues,
  986. NULL_STRING
  987. );
  988. // batteryalarm sub-option 'SOUND'
  989. cmdOptions[CMDINDEX_ALARMSOUND].dwFlags = CP_TYPE_TEXT |
  990. CP_VALUE_MANDATORY;
  991. cmdOptions[CMDINDEX_ALARMSOUND].dwCount = 1;
  992. cmdOptions[CMDINDEX_ALARMSOUND].dwActuals = 0;
  993. cmdOptions[CMDINDEX_ALARMSOUND].pValue = lpszAlarmSoundBoolStr;
  994. cmdOptions[CMDINDEX_ALARMSOUND].pFunction = NULL;
  995. cmdOptions[CMDINDEX_ALARMSOUND].pFunctionData = NULL;
  996. lstrcpy(
  997. cmdOptions[CMDINDEX_ALARMSOUND].szOption,
  998. CMDOPTION_ALARMSOUND
  999. );
  1000. lstrcpy(
  1001. cmdOptions[CMDINDEX_ALARMSOUND].szValues,
  1002. NULL_STRING
  1003. );
  1004. // batteryalarm sub-option 'ACTION'
  1005. cmdOptions[CMDINDEX_ALARMACTION].dwFlags = CP_TYPE_TEXT |
  1006. CP_VALUE_MANDATORY;
  1007. cmdOptions[CMDINDEX_ALARMACTION].dwCount = 1;
  1008. cmdOptions[CMDINDEX_ALARMACTION].dwActuals = 0;
  1009. cmdOptions[CMDINDEX_ALARMACTION].pValue = lpszAlarmActionStr;
  1010. cmdOptions[CMDINDEX_ALARMACTION].pFunction = NULL;
  1011. cmdOptions[CMDINDEX_ALARMACTION].pFunctionData = NULL;
  1012. lstrcpy(
  1013. cmdOptions[CMDINDEX_ALARMACTION].szOption,
  1014. CMDOPTION_ALARMACTION
  1015. );
  1016. lstrcpy(
  1017. cmdOptions[CMDINDEX_ALARMACTION].szValues,
  1018. NULL_STRING
  1019. );
  1020. // batteryalarm sub-option 'FORCE'
  1021. cmdOptions[CMDINDEX_ALARMFORCE].dwFlags = CP_TYPE_TEXT |
  1022. CP_VALUE_MANDATORY;
  1023. cmdOptions[CMDINDEX_ALARMFORCE].dwCount = 1;
  1024. cmdOptions[CMDINDEX_ALARMFORCE].dwActuals = 0;
  1025. cmdOptions[CMDINDEX_ALARMFORCE].pValue = lpszAlarmForceBoolStr;
  1026. cmdOptions[CMDINDEX_ALARMFORCE].pFunction = NULL;
  1027. cmdOptions[CMDINDEX_ALARMFORCE].pFunctionData = NULL;
  1028. lstrcpy(
  1029. cmdOptions[CMDINDEX_ALARMFORCE].szOption,
  1030. CMDOPTION_ALARMFORCE
  1031. );
  1032. lstrcpy(
  1033. cmdOptions[CMDINDEX_ALARMFORCE].szValues,
  1034. NULL_STRING
  1035. );
  1036. // batteryalarm sub-option 'PROGRAM'
  1037. cmdOptions[CMDINDEX_ALARMPROGRAM].dwFlags = CP_TYPE_TEXT |
  1038. CP_VALUE_MANDATORY;
  1039. cmdOptions[CMDINDEX_ALARMPROGRAM].dwCount = 1;
  1040. cmdOptions[CMDINDEX_ALARMPROGRAM].dwActuals = 0;
  1041. cmdOptions[CMDINDEX_ALARMPROGRAM].pValue = lpszAlarmProgramBoolStr;
  1042. cmdOptions[CMDINDEX_ALARMPROGRAM].pFunction = NULL;
  1043. cmdOptions[CMDINDEX_ALARMPROGRAM].pFunctionData = NULL;
  1044. lstrcpy(
  1045. cmdOptions[CMDINDEX_ALARMPROGRAM].szOption,
  1046. CMDOPTION_ALARMPROGRAM
  1047. );
  1048. lstrcpy(
  1049. cmdOptions[CMDINDEX_ALARMPROGRAM].szValues,
  1050. NULL_STRING
  1051. );
  1052. // parse parameters, take appropriate action
  1053. if(DoParseParam(argc,argv,NUM_CMDS,cmdOptions))
  1054. {
  1055. // make sure only one command issued
  1056. DWORD dwCmdCount = 0;
  1057. DWORD dwParamCount = 0;
  1058. for(dwIdx=0;dwIdx<NUM_CMDS;dwIdx++)
  1059. {
  1060. if (dwIdx < NUM_MAIN_CMDS)
  1061. {
  1062. dwCmdCount += cmdOptions[dwIdx].dwActuals;
  1063. }
  1064. else if (dwIdx != CMDINDEX_NUMERICAL)
  1065. {
  1066. dwParamCount += cmdOptions[dwIdx].dwActuals;
  1067. }
  1068. }
  1069. // determine other flags
  1070. bQuery = (cmdOptions[CMDINDEX_QUERY].dwActuals != 0);
  1071. bCreate = (cmdOptions[CMDINDEX_CREATE].dwActuals != 0);
  1072. bDelete = (cmdOptions[CMDINDEX_DELETE].dwActuals != 0);
  1073. bSetActive = (cmdOptions[CMDINDEX_SETACTIVE].dwActuals != 0);
  1074. bChange = (cmdOptions[CMDINDEX_CHANGE].dwActuals != 0);
  1075. bHibernate = (cmdOptions[CMDINDEX_HIBERNATE].dwActuals != 0);
  1076. bGlobalFlag = (cmdOptions[CMDINDEX_GLOBALFLAG].dwActuals != 0);
  1077. bGetSupporedSStates = (cmdOptions[CMDINDEX_SSTATES].dwActuals != 0);
  1078. bExport = (cmdOptions[CMDINDEX_EXPORT].dwActuals != 0);
  1079. bImport = (cmdOptions[CMDINDEX_IMPORT].dwActuals != 0);
  1080. bFile = (cmdOptions[CMDINDEX_FILE].dwActuals != 0);
  1081. tChangeParam.bVideoTimeoutAc =
  1082. (cmdOptions[CMDINDEX_MONITOR_OFF_AC].dwActuals != 0);
  1083. tChangeParam.bVideoTimeoutDc =
  1084. (cmdOptions[CMDINDEX_MONITOR_OFF_DC].dwActuals != 0);
  1085. tChangeParam.bSpindownTimeoutAc =
  1086. (cmdOptions[CMDINDEX_DISK_OFF_AC].dwActuals != 0);
  1087. tChangeParam.bSpindownTimeoutDc =
  1088. (cmdOptions[CMDINDEX_DISK_OFF_DC].dwActuals != 0);
  1089. tChangeParam.bIdleTimeoutAc =
  1090. (cmdOptions[CMDINDEX_STANDBY_AC].dwActuals != 0);
  1091. tChangeParam.bIdleTimeoutDc =
  1092. (cmdOptions[CMDINDEX_STANDBY_DC].dwActuals != 0);
  1093. tChangeParam.bDozeS4TimeoutAc =
  1094. (cmdOptions[CMDINDEX_HIBER_AC].dwActuals != 0);
  1095. tChangeParam.bDozeS4TimeoutDc =
  1096. (cmdOptions[CMDINDEX_HIBER_DC].dwActuals != 0);
  1097. tChangeParam.bDynamicThrottleAc =
  1098. (cmdOptions[CMDINDEX_THROTTLE_AC].dwActuals != 0);
  1099. tChangeParam.bDynamicThrottleDc =
  1100. (cmdOptions[CMDINDEX_THROTTLE_DC].dwActuals != 0);
  1101. tChangeParam.lpszDynamicThrottleAc = lpszThrottleAcStr;
  1102. tChangeParam.lpszDynamicThrottleDc = lpszThrottleDcStr;
  1103. bBatteryAlarm = (cmdOptions[CMDINDEX_BATTERYALARM].dwActuals != 0);
  1104. // verify number
  1105. if(bNumerical)
  1106. {
  1107. for(dwIdx=0; lpszName[dwIdx] != 0; dwIdx++)
  1108. {
  1109. if((lpszName[dwIdx] < _T('0')) ||
  1110. (lpszName[dwIdx] > _T('9')))
  1111. {
  1112. bFail = TRUE;
  1113. g_lpszErr = GetResString(IDS_INVALID_CMDLINE_PARAM);
  1114. break;
  1115. }
  1116. }
  1117. }
  1118. //
  1119. // parameter count validation
  1120. //
  1121. if ((dwCmdCount == 1) &&
  1122. ((dwParamCount == 0) ||
  1123. (bChange && (dwParamCount > 0) && (!bFile)) ||
  1124. ((bImport || bExport) && bFile && (dwParamCount == 1)) ||
  1125. (bGlobalFlag && (dwParamCount == 1)) ||
  1126. ((bBatteryAlarm) && (dwParamCount <= 7))) &&
  1127. ((!bNumerical) || ((lstrlen(lpszName) != 0) && (!bCreate))) &&
  1128. (!bFail))
  1129. {
  1130. // check flags, take appropriate action
  1131. if(bList)
  1132. {
  1133. DoList();
  1134. }
  1135. else if (bQuery)
  1136. {
  1137. bFail = !DoQuery(
  1138. lpszName,
  1139. (lstrlen(lpszName) != 0),
  1140. bNumerical
  1141. );
  1142. }
  1143. else if (bCreate)
  1144. {
  1145. bFail = !DoCreate(
  1146. lpszName
  1147. );
  1148. }
  1149. else if (bDelete)
  1150. {
  1151. bFail = !DoDelete(
  1152. lpszName,
  1153. bNumerical
  1154. );
  1155. }
  1156. else if (bSetActive)
  1157. {
  1158. bFail = !DoSetActive(
  1159. lpszName,
  1160. bNumerical
  1161. );
  1162. }
  1163. else if (bChange)
  1164. {
  1165. bFail = !DoChange(
  1166. lpszName,
  1167. bNumerical,
  1168. &tChangeParam
  1169. );
  1170. }
  1171. else if (bHibernate)
  1172. {
  1173. bFail = !DoHibernate(lpszBoolStr);
  1174. }
  1175. else if (bGlobalFlag)
  1176. {
  1177. bFail = !DoGlobalFlag(lpszBoolStr,lpszGlobalFlagOption);
  1178. }
  1179. else if (bGetSupporedSStates)
  1180. {
  1181. bFail = !DoGetSupportedSStates();
  1182. }
  1183. else if (bExport)
  1184. {
  1185. bFail = !DoExport(
  1186. lpszName,
  1187. bNumerical,
  1188. lpszFile
  1189. );
  1190. }
  1191. else if (bImport)
  1192. {
  1193. bFail = !DoImport(
  1194. lpszName,
  1195. bNumerical,
  1196. lpszFile
  1197. );
  1198. }
  1199. else if (bBatteryAlarm)
  1200. {
  1201. bFail = !DoBatteryAlarm(
  1202. lpszName,
  1203. (cmdOptions[CMDINDEX_ALARMACTIVE].dwActuals!=0) ?
  1204. lpszBoolStr : NULL,
  1205. dwAlarmLevel,
  1206. (cmdOptions[CMDINDEX_ALARMTEXT].dwActuals!=0) ?
  1207. lpszAlarmTextBoolStr : NULL,
  1208. (cmdOptions[CMDINDEX_ALARMSOUND].dwActuals!=0) ?
  1209. lpszAlarmSoundBoolStr : NULL,
  1210. (cmdOptions[CMDINDEX_ALARMACTION].dwActuals!=0) ?
  1211. lpszAlarmActionStr : NULL,
  1212. (cmdOptions[CMDINDEX_ALARMFORCE].dwActuals!=0) ?
  1213. lpszAlarmForceBoolStr : NULL,
  1214. (cmdOptions[CMDINDEX_ALARMPROGRAM].dwActuals!=0) ?
  1215. lpszAlarmProgramBoolStr : NULL
  1216. );
  1217. }
  1218. else if (bUsage)
  1219. {
  1220. DoUsage();
  1221. }
  1222. else
  1223. {
  1224. if(lstrlen(g_lpszErr) == 0)
  1225. {
  1226. g_lpszErr = GetResString(IDS_INVALID_CMDLINE_PARAM);
  1227. }
  1228. bFail = TRUE;
  1229. }
  1230. }
  1231. else
  1232. {
  1233. // handle error conditions
  1234. if(lstrlen(g_lpszErr) == 0)
  1235. {
  1236. g_lpszErr = GetResString(IDS_INVALID_CMDLINE_PARAM);
  1237. }
  1238. bFail = TRUE;
  1239. }
  1240. }
  1241. else
  1242. {
  1243. g_lpszErr = GetResString(IDS_INVALID_CMDLINE_PARAM);
  1244. bFail = TRUE;
  1245. }
  1246. // check error status, display msg if needed
  1247. if(bFail)
  1248. {
  1249. if(g_lpszErr2)
  1250. {
  1251. DISPLAY_MESSAGE(stderr,g_lpszErr2);
  1252. }
  1253. else
  1254. {
  1255. DISPLAY_MESSAGE(stderr,g_lpszErr);
  1256. }
  1257. }
  1258. // clean up allocs
  1259. LocalFree(lpszBoolStr);
  1260. LocalFree(lpszName);
  1261. LocalFree(lpszFile);
  1262. LocalFree(lpszThrottleAcStr);
  1263. LocalFree(lpszThrottleDcStr);
  1264. LocalFree(lpszGlobalFlagOption);
  1265. LocalFree(lpszAlarmTextBoolStr);
  1266. LocalFree(lpszAlarmSoundBoolStr);
  1267. LocalFree(lpszAlarmActionStr);
  1268. LocalFree(lpszAlarmForceBoolStr);
  1269. LocalFree(lpszAlarmProgramBoolStr);
  1270. if (g_lpszErr2)
  1271. {
  1272. LocalFree(g_lpszErr2);
  1273. }
  1274. FreeLibrary(hLib);
  1275. // return appropriate result code
  1276. if(bFail)
  1277. {
  1278. return EXIT_FAILURE;
  1279. }
  1280. else
  1281. {
  1282. return EXIT_SUCCESS;
  1283. }
  1284. }
  1285. BOOL
  1286. FreeScheme(
  1287. PSCHEME_LIST psl
  1288. )
  1289. /*++
  1290. Routine Description:
  1291. Frees the memory associated with a scheme list entry.
  1292. Arguments:
  1293. psl - the PSCHEME_LIST to be freed
  1294. Return Value:
  1295. Always returns TRUE, indicating success.
  1296. --*/
  1297. {
  1298. LocalFree(psl->lpszName);
  1299. LocalFree(psl->lpszDesc);
  1300. LocalFree(psl->ppp);
  1301. LocalFree(psl->pmppp);
  1302. LocalFree(psl);
  1303. return TRUE;
  1304. }
  1305. BOOL
  1306. FreeSchemeList(
  1307. PSCHEME_LIST psl,
  1308. PSCHEME_LIST pslExcept
  1309. )
  1310. /*++
  1311. Routine Description:
  1312. Deallocates all power schemes in a linked-list of power schemes, except
  1313. for the one pointed to by pslExcept
  1314. Arguments:
  1315. psl - the power scheme list to deallocate
  1316. pslExcept - a scheme not to deallocate (null to deallocate all)
  1317. Return Value:
  1318. Always returns TRUE, indicating success.
  1319. --*/
  1320. {
  1321. PSCHEME_LIST cur = psl;
  1322. PSCHEME_LIST next;
  1323. while (cur != NULL)
  1324. {
  1325. next = CONTAINING_RECORD(
  1326. cur->le.Flink,
  1327. SCHEME_LIST,
  1328. le
  1329. );
  1330. if (cur != pslExcept)
  1331. {
  1332. FreeScheme(cur);
  1333. }
  1334. else
  1335. {
  1336. cur->le.Flink = NULL;
  1337. cur->le.Blink = NULL;
  1338. }
  1339. cur = next;
  1340. }
  1341. return TRUE;
  1342. }
  1343. PSCHEME_LIST
  1344. CreateScheme(
  1345. UINT uiID,
  1346. DWORD dwNameSize,
  1347. LPCTSTR lpszName,
  1348. DWORD dwDescSize,
  1349. LPCTSTR lpszDesc,
  1350. PPOWER_POLICY ppp
  1351. )
  1352. /*++
  1353. Routine Description:
  1354. Builds a policy list entry. Note that the scheme is allocated and must
  1355. be freed when done.
  1356. Arguments:
  1357. uiID - the numerical ID of the scheme
  1358. dwNameSize - the number of bytes needed to store lpszName
  1359. lpszName - the name of the scheme
  1360. dwDescSize - the number of bytes needed to store lpszDesc
  1361. lpszDesc - the description of the scheme
  1362. ppp - the power policy for this scheme, may be NULL
  1363. Return Value:
  1364. A PSCHEME_LIST entry containing the specified values, with the next
  1365. entry field set to NULL
  1366. --*/
  1367. {
  1368. PSCHEME_LIST psl = (PSCHEME_LIST)LocalAlloc(LPTR,sizeof(SCHEME_LIST));
  1369. if (psl)
  1370. {
  1371. // deal with potentially null input strings
  1372. if(lpszName == NULL)
  1373. {
  1374. lpszName = NULL_STRING;
  1375. }
  1376. if(lpszDesc == NULL)
  1377. {
  1378. lpszDesc = NULL_STRING;
  1379. }
  1380. // allocate fields
  1381. psl->ppp = (PPOWER_POLICY)LocalAlloc(LPTR,sizeof(POWER_POLICY));
  1382. if (!psl->ppp)
  1383. {
  1384. g_lpszErr = GetResString(IDS_OUT_OF_MEMORY);
  1385. return NULL;
  1386. }
  1387. psl->pmppp = (PMACHINE_PROCESSOR_POWER_POLICY)LocalAlloc(
  1388. LPTR,
  1389. sizeof(MACHINE_PROCESSOR_POWER_POLICY)
  1390. );
  1391. if (!psl->pmppp)
  1392. {
  1393. LocalFree(psl->ppp);
  1394. g_lpszErr = GetResString(IDS_OUT_OF_MEMORY);
  1395. return NULL;
  1396. }
  1397. psl->lpszName = (LPTSTR)LocalAlloc(LPTR,dwNameSize);
  1398. if (!psl->lpszName)
  1399. {
  1400. LocalFree(psl->ppp);
  1401. LocalFree(psl->pmppp);
  1402. g_lpszErr = GetResString(IDS_OUT_OF_MEMORY);
  1403. return NULL;
  1404. }
  1405. psl->lpszDesc = (LPTSTR)LocalAlloc(LPTR,dwDescSize);
  1406. if (!psl->lpszDesc)
  1407. {
  1408. LocalFree(psl->ppp);
  1409. LocalFree(psl->pmppp);
  1410. LocalFree(psl->lpszName);
  1411. g_lpszErr = GetResString(IDS_OUT_OF_MEMORY);
  1412. return NULL;
  1413. }
  1414. // initialize structure
  1415. psl->uiID = uiID;
  1416. memcpy(psl->lpszName,lpszName,dwNameSize);
  1417. memcpy(psl->lpszDesc,lpszDesc,dwDescSize);
  1418. if (ppp)
  1419. {
  1420. memcpy(psl->ppp,ppp,sizeof(POWER_POLICY));
  1421. }
  1422. psl->le.Flink = NULL;
  1423. psl->le.Blink = NULL;
  1424. }
  1425. else
  1426. {
  1427. g_lpszErr = GetResString(IDS_OUT_OF_MEMORY);
  1428. }
  1429. return psl;
  1430. }
  1431. BOOLEAN CALLBACK
  1432. PowerSchemeEnumProc(
  1433. UINT uiID,
  1434. DWORD dwNameSize,
  1435. LPTSTR lpszName,
  1436. DWORD dwDescSize,
  1437. LPTSTR lpszDesc,
  1438. PPOWER_POLICY ppp,
  1439. LPARAM lParam
  1440. )
  1441. /*++
  1442. Routine Description:
  1443. This is a callback used in retrieving the policy list.
  1444. Arguments:
  1445. uiID - the numerical ID of the scheme
  1446. dwNameSize - the number of bytes needed to store lpszName
  1447. lpszName - the name of the scheme
  1448. dwDescSize - the number of bytes needed to store lpszDesc
  1449. lpszDesc - the description of the scheme
  1450. ppp - the power policy for this scheme
  1451. lParam - used to hold a pointer to the head-of-list pointer, allowing
  1452. for insertions at the head of the list
  1453. Return Value:
  1454. TRUE to continue enumeration
  1455. FALSE to abort enumeration
  1456. --*/
  1457. {
  1458. PSCHEME_LIST psl;
  1459. // Allocate and initalize a policies element.
  1460. if ((psl = CreateScheme(
  1461. uiID,
  1462. dwNameSize,
  1463. lpszName,
  1464. dwDescSize,
  1465. lpszDesc,
  1466. ppp
  1467. )) != NULL)
  1468. {
  1469. // add the element to the head of the linked list
  1470. psl->le.Flink = *((PLIST_ENTRY *)lParam);
  1471. if(*((PLIST_ENTRY *)lParam))
  1472. {
  1473. (*((PLIST_ENTRY *)lParam))->Blink = &(psl->le);
  1474. }
  1475. (*(PLIST_ENTRY *)lParam) = &(psl->le);
  1476. return TRUE;
  1477. }
  1478. return FALSE;
  1479. }
  1480. PSCHEME_LIST
  1481. CreateSchemeList()
  1482. /*++
  1483. Routine Description:
  1484. Creates a linked list of existing power schemes.
  1485. Arguments:
  1486. None
  1487. Return Value:
  1488. A pointer to the head of the list.
  1489. NULL would correspond to an empty list.
  1490. --*/
  1491. {
  1492. PLIST_ENTRY ple = NULL;
  1493. PSCHEME_LIST psl;
  1494. fEnumPwrSchemes(PowerSchemeEnumProc, (LPARAM)(&ple));
  1495. if(ple)
  1496. {
  1497. PSCHEME_LIST res = CONTAINING_RECORD(
  1498. ple,
  1499. SCHEME_LIST,
  1500. le
  1501. );
  1502. psl = res;
  1503. if(g_bProcessorPwrSchemeSupported) {
  1504. while(psl != NULL)
  1505. {
  1506. if(!fReadProcessorPwrScheme(psl->uiID,psl->pmppp))
  1507. {
  1508. FreeSchemeList(res,NULL);
  1509. g_lpszErr = GetResString(IDS_UNEXPECTED_ERROR);
  1510. return NULL;
  1511. }
  1512. psl = CONTAINING_RECORD(
  1513. psl->le.Flink,
  1514. SCHEME_LIST,
  1515. le
  1516. );
  1517. }
  1518. }
  1519. return res;
  1520. }
  1521. else
  1522. {
  1523. return NULL;
  1524. }
  1525. }
  1526. PSCHEME_LIST
  1527. FindScheme(
  1528. LPCTSTR lpszName,
  1529. UINT uiID,
  1530. BOOL bNumerical
  1531. )
  1532. /*++
  1533. Routine Description:
  1534. Finds the policy with the matching name. If lpszName is NULL,
  1535. the scheme is found by uiID instead. If bNumerical is TRUE,
  1536. lpszName will be interpreted as a numerical identifier instead.
  1537. Arguments:
  1538. lpszName - the name of the scheme to find
  1539. uiID - the numerical identifier of the scheme
  1540. bNumerical - causes lpszName to be interpreted as a numerical identifier
  1541. Return Value:
  1542. the matching scheme list entry, null if none
  1543. --*/
  1544. {
  1545. PSCHEME_LIST psl = CreateSchemeList();
  1546. PSCHEME_LIST pslRes = NULL;
  1547. // process bNumerical option
  1548. if(bNumerical && lpszName) {
  1549. uiID = _ttoi(lpszName);
  1550. lpszName = NULL;
  1551. }
  1552. // find scheme entry
  1553. while(psl != NULL)
  1554. {
  1555. // check for match
  1556. if (((lpszName != NULL) && (!lstrcmpi(lpszName, psl->lpszName))) ||
  1557. ((lpszName == NULL) && (uiID == psl->uiID)))
  1558. {
  1559. pslRes = psl;
  1560. break;
  1561. }
  1562. // traverse list
  1563. psl = CONTAINING_RECORD(
  1564. psl->le.Flink,
  1565. SCHEME_LIST,
  1566. le
  1567. );
  1568. }
  1569. FreeSchemeList(psl,pslRes); // all except for pslRes
  1570. if (pslRes == NULL)
  1571. g_lpszErr = GetResString(IDS_SCHEME_NOT_FOUND);
  1572. return pslRes;
  1573. }
  1574. BOOL
  1575. MyWriteScheme(
  1576. PSCHEME_LIST psl
  1577. )
  1578. /*++
  1579. Routine Description:
  1580. Writes a power scheme -- both user/machine power policies and
  1581. processor power policy. The underlying powrprof.dll does not
  1582. treat the processor power policy as part of the power policy
  1583. because the processor power policies were added at a later
  1584. date and backwards compatibility must be maintained.
  1585. Arguments:
  1586. psl - The scheme list entry to write
  1587. Return Value:
  1588. TRUE if successful, otherwise FALSE
  1589. --*/
  1590. {
  1591. g_lpszErr = GetResString(IDS_UNEXPECTED_ERROR);
  1592. if(fWritePwrScheme(
  1593. &psl->uiID,
  1594. psl->lpszName,
  1595. psl->lpszDesc,
  1596. psl->ppp))
  1597. {
  1598. if(g_bProcessorPwrSchemeSupported) {
  1599. return fWriteProcessorPwrScheme(
  1600. psl->uiID,
  1601. psl->pmppp
  1602. );
  1603. }
  1604. else
  1605. {
  1606. return TRUE;
  1607. }
  1608. }
  1609. else
  1610. {
  1611. return FALSE;
  1612. }
  1613. }
  1614. BOOL
  1615. MapIdleValue(
  1616. ULONG ulVal,
  1617. PULONG pulIdle,
  1618. PULONG pulHiber,
  1619. PPOWER_ACTION ppapIdle
  1620. )
  1621. /*++
  1622. Routine Description:
  1623. Modifies Idle and Hibernation settings to reflect the desired idle
  1624. timeout. See GUI tool's PWRSCHEM.C MapHiberTimer for logic.
  1625. Arguments:
  1626. ulVal - the new idle timeout
  1627. pulIdle - the idle timeout variable to be updated
  1628. pulHiber - the hiber timeout variable to be updated
  1629. Return Value:
  1630. TRUE if successful
  1631. FALSE if failed
  1632. --*/
  1633. {
  1634. // if previously, hiber was enabled and standby wasn't, standby timer
  1635. // takes over the hibernation timer's role
  1636. if (*ppapIdle == PowerActionHibernate)
  1637. {
  1638. if (ulVal > 0)
  1639. { // enable standby
  1640. *pulHiber = *pulIdle + ulVal;
  1641. *pulIdle = ulVal;
  1642. *ppapIdle = PowerActionSleep;
  1643. }
  1644. else { // standby already disabled, no change
  1645. }
  1646. }
  1647. else // standby timer actually being used for standby (not hiber)
  1648. {
  1649. if (ulVal > 0)
  1650. { // enable standby
  1651. if ((*pulHiber) != 0)
  1652. {
  1653. *pulHiber = *pulHiber + ulVal - *pulIdle;
  1654. }
  1655. *pulIdle = ulVal;
  1656. if (ulVal > 0)
  1657. {
  1658. *ppapIdle = PowerActionSleep;
  1659. }
  1660. else
  1661. {
  1662. *ppapIdle = PowerActionNone;
  1663. }
  1664. }
  1665. else
  1666. { // disable standby
  1667. if ((*pulHiber) != 0)
  1668. {
  1669. *pulIdle = *pulHiber;
  1670. *pulHiber = 0;
  1671. *ppapIdle = PowerActionHibernate;
  1672. }
  1673. else
  1674. {
  1675. *pulIdle = 0;
  1676. *ppapIdle = PowerActionNone;
  1677. }
  1678. }
  1679. }
  1680. return TRUE;
  1681. }
  1682. BOOL
  1683. MapHiberValue(
  1684. ULONG NewHibernateTimeout,
  1685. PULONG pExistingStandbyTimeout,
  1686. PULONG pHIbernateTimeoutVariable,
  1687. PPOWER_ACTION pIdlePowerAction
  1688. )
  1689. /*++
  1690. Routine Description:
  1691. Modifies Idle and Hibernation settings to reflect the desired hibernation
  1692. timeout. See GUI tool's PWRSCHEM.C MapHiberTimer for logic.
  1693. Arguments:
  1694. NewHibernateTimeout - the new hibernation timeout the user is
  1695. asking us to apply.
  1696. pExistingStandbyTimeout - existing standby timeout.
  1697. pHIbernateTimeoutVariable - existing hibernate timeout variable which will
  1698. be updated with the new value being sent in.
  1699. pIdlePowerAction - existing power action to take after specified idle timeout.
  1700. Return Value:
  1701. TRUE if successful
  1702. FALSE if failed
  1703. --*/
  1704. {
  1705. //
  1706. // check valid input
  1707. //
  1708. if( (NewHibernateTimeout != 0) &&
  1709. (NewHibernateTimeout < *pExistingStandbyTimeout) ) {
  1710. //
  1711. // He's asking us to set the hibernate timeout
  1712. // to be less than the standby timer. We disallow this.
  1713. //
  1714. g_lpszErr = GetResString(IDS_HIBER_OUT_OF_RANGE);
  1715. return FALSE;
  1716. }
  1717. //
  1718. // check to see if we can even enable hibernation.
  1719. //
  1720. if( (NewHibernateTimeout != 0) &&
  1721. (!g_bHiberFileSupported) ) {
  1722. g_lpszErr = GetResString(IDS_HIBER_UNSUPPORTED);
  1723. return FALSE;
  1724. }
  1725. //
  1726. // We're ready to update our timeout value.
  1727. //
  1728. if( NewHibernateTimeout == 0 ) {
  1729. //
  1730. // He's asking us to set the timeout to zero, which
  1731. // is synonymous with simply disabling hibernate.
  1732. //
  1733. *pHIbernateTimeoutVariable = NewHibernateTimeout;
  1734. //
  1735. // Now fix up our idle PowerAction. It can no longer
  1736. // be set to hibernate, so our choices are either sleep
  1737. // or nothing. Set it according to whether sleep is even
  1738. // supported on this machine.
  1739. //
  1740. *pIdlePowerAction = g_bStandbySupported ? PowerActionSleep : PowerActionNone;
  1741. //
  1742. // Care here. if we just set our idle PowerAction to do nothing,
  1743. // make sure our standby idle timeout is set to zero.
  1744. //
  1745. *pExistingStandbyTimeout = 0;
  1746. } else {
  1747. //
  1748. // He wants to set some timeout. But the standby and
  1749. // hibernate timeouts are somewhat related. If he
  1750. // wants the system to hibernate after 60 minutes of
  1751. // idle time, but the standby is set to 20, then what
  1752. // he's really asking is for us to set the hibernate to
  1753. // 40. This means that after 20 minutes of idle, the system
  1754. // will go to standby and we'll set a 40 minute timer to
  1755. // tell the system to go to hibernate. If we set that timer
  1756. // to 60 minutes, then the system wouldn't actually hibernate
  1757. // until after 20+60=80 minutes. Therefore, set the timeout
  1758. // to what he's asking for, minus the existing standby timeout.
  1759. //
  1760. *pHIbernateTimeoutVariable = NewHibernateTimeout - *pExistingStandbyTimeout;
  1761. //
  1762. // Now fix up our idle PowerAction. If we don't support sleep on this
  1763. // machine, then we need to set the idle PowerAction to hibernate.
  1764. //
  1765. *pIdlePowerAction = g_bStandbySupported ? PowerActionSleep : PowerActionHibernate;
  1766. }
  1767. return TRUE;
  1768. }
  1769. BOOL
  1770. DoList()
  1771. /*++
  1772. Routine Description:
  1773. Lists the existing power schemes on stdout
  1774. Arguments:
  1775. none
  1776. Return Value:
  1777. TRUE if successful
  1778. FALSE if failed
  1779. --*/
  1780. {
  1781. PSCHEME_LIST psl = CreateSchemeList();
  1782. if (psl != NULL)
  1783. {
  1784. DISPLAY_MESSAGE(stdout,GetResString(IDS_LIST_HEADER1));
  1785. DISPLAY_MESSAGE(stdout,GetResString(IDS_LIST_HEADER2));
  1786. }
  1787. else
  1788. {
  1789. return FALSE;
  1790. }
  1791. while(psl != NULL)
  1792. {
  1793. DISPLAY_MESSAGE(stdout, psl->lpszName);
  1794. DISPLAY_MESSAGE(stdout, L"\n");
  1795. psl = CONTAINING_RECORD(
  1796. psl->le.Flink,
  1797. SCHEME_LIST,
  1798. le
  1799. );
  1800. }
  1801. FreeSchemeList(psl,NULL); // free all entries
  1802. return TRUE;
  1803. }
  1804. BOOL
  1805. DoQuery(
  1806. LPCTSTR lpszName,
  1807. BOOL bNameSpecified,
  1808. BOOL bNumerical
  1809. )
  1810. /*++
  1811. Routine Description:
  1812. Show details of an existing scheme
  1813. Arguments:
  1814. lpszName - the name of the scheme
  1815. bNameSpecified - if TRUE, lpszName ignored and shows details
  1816. of active power scheme instead
  1817. bNumerical - if TRUE, lpszName interpreted as numerical identifier
  1818. Return Value:
  1819. TRUE if successful
  1820. FALSE if failed
  1821. --*/
  1822. {
  1823. PSCHEME_LIST psl;
  1824. // check if querying specific scheme or active scheme and deal w/it
  1825. if (bNameSpecified)
  1826. {
  1827. psl = FindScheme(
  1828. lpszName,
  1829. 0,
  1830. bNumerical
  1831. );
  1832. }
  1833. else // fetch the active scheme
  1834. {
  1835. UINT uiID;
  1836. if (fGetActivePwrScheme(&uiID))
  1837. {
  1838. psl = FindScheme(NULL,uiID,FALSE);
  1839. }
  1840. else
  1841. {
  1842. g_lpszErr = GetResString(IDS_ACTIVE_SCHEME_INVALID);
  1843. return FALSE;
  1844. }
  1845. }
  1846. // display info
  1847. if (psl)
  1848. {
  1849. // header
  1850. DISPLAY_MESSAGE(stdout, GetResString(IDS_QUERY_HEADER1));
  1851. DISPLAY_MESSAGE(stdout, GetResString(IDS_QUERY_HEADER2));
  1852. // name
  1853. DISPLAY_MESSAGE1(
  1854. stdout,
  1855. g_lpszBuf,
  1856. GetResString(IDS_SCHEME_NAME),
  1857. psl->lpszName
  1858. );
  1859. // id
  1860. DISPLAY_MESSAGE1(
  1861. stdout,
  1862. g_lpszBuf,
  1863. GetResString(IDS_SCHEME_ID),
  1864. psl->uiID
  1865. );
  1866. // monitor timeout AC
  1867. DISPLAY_MESSAGE(stdout, GetResString(IDS_MONITOR_TIMEOUT_AC));
  1868. if (!g_bMonitorPowerSupported)
  1869. {
  1870. DISPLAY_MESSAGE(stdout, GetResString(IDS_UNSUPPORTED));
  1871. }
  1872. else if (psl->ppp->user.VideoTimeoutAc == 0) {
  1873. DISPLAY_MESSAGE(stdout, GetResString(IDS_DISABLED));
  1874. }
  1875. else
  1876. {
  1877. DISPLAY_MESSAGE1(
  1878. stdout,
  1879. g_lpszBuf,
  1880. GetResString(IDS_MINUTES),
  1881. psl->ppp->user.VideoTimeoutAc/60
  1882. );
  1883. }
  1884. // monitor timeout DC
  1885. DISPLAY_MESSAGE(stdout, GetResString(IDS_MONITOR_TIMEOUT_DC));
  1886. if (!g_bMonitorPowerSupported)
  1887. {
  1888. DISPLAY_MESSAGE(stdout, GetResString(IDS_UNSUPPORTED));
  1889. }
  1890. else if (psl->ppp->user.VideoTimeoutDc == 0)
  1891. {
  1892. DISPLAY_MESSAGE(stdout, GetResString(IDS_DISABLED));
  1893. }
  1894. else
  1895. {
  1896. DISPLAY_MESSAGE1(
  1897. stdout,
  1898. g_lpszBuf,
  1899. GetResString(IDS_MINUTES),
  1900. psl->ppp->user.VideoTimeoutDc/60
  1901. );
  1902. }
  1903. // disk timeout AC
  1904. DISPLAY_MESSAGE(stdout, GetResString(IDS_DISK_TIMEOUT_AC));
  1905. if (!g_bDiskPowerSupported)
  1906. {
  1907. DISPLAY_MESSAGE(stdout, GetResString(IDS_UNSUPPORTED));
  1908. }
  1909. else if (psl->ppp->user.SpindownTimeoutAc == 0)
  1910. {
  1911. DISPLAY_MESSAGE(stdout, GetResString(IDS_DISABLED));
  1912. }
  1913. else
  1914. {
  1915. DISPLAY_MESSAGE1(
  1916. stdout,
  1917. g_lpszBuf,
  1918. GetResString(IDS_MINUTES),
  1919. psl->ppp->user.SpindownTimeoutAc/60
  1920. );
  1921. }
  1922. // disk timeout DC
  1923. DISPLAY_MESSAGE(stdout, GetResString(IDS_DISK_TIMEOUT_DC));
  1924. if (!g_bDiskPowerSupported)
  1925. {
  1926. DISPLAY_MESSAGE(stdout, GetResString(IDS_UNSUPPORTED));
  1927. }
  1928. else if (psl->ppp->user.SpindownTimeoutDc == 0)
  1929. {
  1930. DISPLAY_MESSAGE(stdout, GetResString(IDS_DISABLED));
  1931. }
  1932. else
  1933. {
  1934. DISPLAY_MESSAGE1(
  1935. stdout,
  1936. g_lpszBuf,
  1937. GetResString(IDS_MINUTES),
  1938. psl->ppp->user.SpindownTimeoutDc/60
  1939. );
  1940. }
  1941. // standby timeout AC
  1942. DISPLAY_MESSAGE(stdout, GetResString(IDS_STANDBY_TIMEOUT_AC));
  1943. if (!g_bStandbySupported)
  1944. {
  1945. DISPLAY_MESSAGE(stdout, GetResString(IDS_UNSUPPORTED));
  1946. }
  1947. else if ((psl->ppp->user.IdleAc.Action != PowerActionSleep) ||
  1948. (psl->ppp->user.IdleTimeoutAc == 0))
  1949. {
  1950. DISPLAY_MESSAGE(stdout, GetResString(IDS_DISABLED));
  1951. }
  1952. else
  1953. {
  1954. DISPLAY_MESSAGE1(
  1955. stdout,
  1956. g_lpszBuf,
  1957. GetResString(IDS_MINUTES),
  1958. psl->ppp->user.IdleTimeoutAc/60
  1959. );
  1960. }
  1961. // standby timeout DC
  1962. DISPLAY_MESSAGE(stdout, GetResString(IDS_STANDBY_TIMEOUT_DC));
  1963. if (!g_bStandbySupported)
  1964. {
  1965. DISPLAY_MESSAGE(stdout, GetResString(IDS_UNSUPPORTED));
  1966. }
  1967. else if ((psl->ppp->user.IdleDc.Action != PowerActionSleep) ||
  1968. (psl->ppp->user.IdleTimeoutDc == 0))
  1969. {
  1970. DISPLAY_MESSAGE(stdout, GetResString(IDS_DISABLED));
  1971. }
  1972. else
  1973. {
  1974. DISPLAY_MESSAGE1(
  1975. stdout,
  1976. g_lpszBuf,
  1977. GetResString(IDS_MINUTES),
  1978. psl->ppp->user.IdleTimeoutDc/60
  1979. );
  1980. }
  1981. // hibernate timeout AC
  1982. DISPLAY_MESSAGE(stdout, GetResString(IDS_HIBER_TIMEOUT_AC));
  1983. if (!g_bHiberFileSupported)
  1984. {
  1985. DISPLAY_MESSAGE(stdout, GetResString(IDS_UNSUPPORTED));
  1986. }
  1987. else if (psl->ppp->mach.DozeS4TimeoutAc == 0)
  1988. {
  1989. DISPLAY_MESSAGE(stdout, GetResString(IDS_DISABLED));
  1990. }
  1991. else
  1992. {
  1993. DISPLAY_MESSAGE1(
  1994. stdout,
  1995. g_lpszBuf,
  1996. GetResString(IDS_MINUTES),
  1997. (psl->ppp->mach.DozeS4TimeoutAc +
  1998. psl->ppp->user.IdleTimeoutAc)/60
  1999. );
  2000. }
  2001. // hibernate timeout DC
  2002. DISPLAY_MESSAGE(stdout, GetResString(IDS_HIBER_TIMEOUT_DC));
  2003. if (!g_bHiberFileSupported)
  2004. {
  2005. DISPLAY_MESSAGE(stdout, GetResString(IDS_UNSUPPORTED));
  2006. }
  2007. else if (psl->ppp->mach.DozeS4TimeoutDc == 0)
  2008. {
  2009. DISPLAY_MESSAGE(stdout, GetResString(IDS_DISABLED));
  2010. }
  2011. else
  2012. {
  2013. DISPLAY_MESSAGE1(
  2014. stdout,
  2015. g_lpszBuf,
  2016. GetResString(IDS_MINUTES),
  2017. (psl->ppp->mach.DozeS4TimeoutDc +
  2018. psl->ppp->user.IdleTimeoutDc)/60
  2019. );
  2020. }
  2021. // throttle policy AC
  2022. DISPLAY_MESSAGE(stdout, GetResString(IDS_THROTTLE_AC));
  2023. if (!g_bThrottleSupported)
  2024. {
  2025. DISPLAY_MESSAGE(stdout, GetResString(IDS_UNSUPPORTED));
  2026. }
  2027. else
  2028. {
  2029. switch(psl->pmppp->ProcessorPolicyAc.DynamicThrottle)
  2030. {
  2031. case PO_THROTTLE_NONE:
  2032. DISPLAY_MESSAGE(stdout, GetResString(IDS_THROTTLE_NONE));
  2033. break;
  2034. case PO_THROTTLE_CONSTANT:
  2035. DISPLAY_MESSAGE(stdout, GetResString(IDS_THROTTLE_CONSTANT));
  2036. break;
  2037. case PO_THROTTLE_DEGRADE:
  2038. DISPLAY_MESSAGE(stdout, GetResString(IDS_THROTTLE_DEGRADE));
  2039. break;
  2040. case PO_THROTTLE_ADAPTIVE:
  2041. DISPLAY_MESSAGE(stdout, GetResString(IDS_THROTTLE_ADAPTIVE));
  2042. break;
  2043. default:
  2044. DISPLAY_MESSAGE(stdout, GetResString(IDS_THROTTLE_UNKNOWN));
  2045. break;
  2046. }
  2047. }
  2048. // throttle policy DC
  2049. DISPLAY_MESSAGE(stdout, GetResString(IDS_THROTTLE_DC));
  2050. if (!g_bThrottleSupported)
  2051. {
  2052. DISPLAY_MESSAGE(stdout, GetResString(IDS_UNSUPPORTED));
  2053. }
  2054. else
  2055. {
  2056. switch(psl->pmppp->ProcessorPolicyDc.DynamicThrottle) {
  2057. case PO_THROTTLE_NONE:
  2058. DISPLAY_MESSAGE(stdout, GetResString(IDS_THROTTLE_NONE));
  2059. break;
  2060. case PO_THROTTLE_CONSTANT:
  2061. DISPLAY_MESSAGE(stdout, GetResString(IDS_THROTTLE_CONSTANT));
  2062. break;
  2063. case PO_THROTTLE_DEGRADE:
  2064. DISPLAY_MESSAGE(stdout, GetResString(IDS_THROTTLE_DEGRADE));
  2065. break;
  2066. case PO_THROTTLE_ADAPTIVE:
  2067. DISPLAY_MESSAGE(stdout, GetResString(IDS_THROTTLE_ADAPTIVE));
  2068. break;
  2069. default:
  2070. DISPLAY_MESSAGE(stdout, GetResString(IDS_THROTTLE_UNKNOWN));
  2071. break;
  2072. }
  2073. }
  2074. FreeScheme(psl);
  2075. return TRUE;
  2076. }
  2077. else
  2078. {
  2079. return FALSE;
  2080. }
  2081. }
  2082. BOOL DoCreate(
  2083. LPTSTR lpszName
  2084. )
  2085. /*++
  2086. Routine Description:
  2087. Adds a new power scheme
  2088. The description will match the name
  2089. All other details are copied from the active power scheme
  2090. Fails if scheme already exists
  2091. Arguments:
  2092. lpszName - the name of the scheme
  2093. Return Value:
  2094. TRUE if successful
  2095. FALSE if failed
  2096. --*/
  2097. {
  2098. PSCHEME_LIST psl = FindScheme(
  2099. lpszName,
  2100. 0,
  2101. FALSE
  2102. );
  2103. UINT uiID;
  2104. BOOL bRes;
  2105. LPTSTR lpszNewName;
  2106. LPTSTR lpszNewDesc;
  2107. if(psl) // already existed -> fail
  2108. {
  2109. FreeScheme(psl);
  2110. g_lpszErr = GetResString(IDS_SCHEME_ALREADY_EXISTS);
  2111. return FALSE;
  2112. }
  2113. // create a new scheme
  2114. if(fGetActivePwrScheme(&uiID))
  2115. {
  2116. psl = FindScheme(NULL,uiID,FALSE);
  2117. if(!psl)
  2118. {
  2119. g_lpszErr = GetResString(IDS_SCHEME_CREATE_FAIL);
  2120. return FALSE;
  2121. }
  2122. lpszNewName = (LPTSTR)LocalAlloc(LPTR,(lstrlen(lpszName)+1)*sizeof(TCHAR));
  2123. if(!lpszNewName)
  2124. {
  2125. FreeScheme(psl);
  2126. g_lpszErr = GetResString(IDS_OUT_OF_MEMORY);
  2127. return FALSE;
  2128. }
  2129. lpszNewDesc = (LPTSTR)LocalAlloc(LPTR,(lstrlen(lpszName)+1)*sizeof(TCHAR));
  2130. if(!lpszNewDesc)
  2131. {
  2132. LocalFree(lpszNewName);
  2133. FreeScheme(psl);
  2134. g_lpszErr = GetResString(IDS_OUT_OF_MEMORY);
  2135. return FALSE;
  2136. }
  2137. lstrcpy(lpszNewName,lpszName);
  2138. lstrcpy(lpszNewDesc,lpszName);
  2139. LocalFree(psl->lpszName);
  2140. LocalFree(psl->lpszDesc);
  2141. psl->lpszName = lpszNewName;
  2142. psl->lpszDesc = lpszNewDesc;
  2143. psl->uiID = NEWSCHEME;
  2144. g_lpszErr = GetResString(IDS_UNEXPECTED_ERROR);
  2145. bRes = MyWriteScheme(psl);
  2146. FreeScheme(psl);
  2147. return bRes;
  2148. }
  2149. g_lpszErr = GetResString(IDS_SCHEME_CREATE_FAIL);
  2150. return FALSE;
  2151. }
  2152. BOOL DoDelete(
  2153. LPCTSTR lpszName,
  2154. BOOL bNumerical
  2155. )
  2156. /*++
  2157. Routine Description:
  2158. Deletes an existing scheme
  2159. Arguments:
  2160. lpszName - the name of the scheme
  2161. bNumerical - if TRUE, lpszName interpreted as numerical identifier
  2162. Return Value:
  2163. TRUE if successful
  2164. FALSE if failed
  2165. --*/
  2166. {
  2167. PSCHEME_LIST psl = FindScheme(
  2168. lpszName,
  2169. 0,
  2170. bNumerical
  2171. );
  2172. if (psl)
  2173. {
  2174. BOOL bRes = fDeletePwrScheme(psl->uiID);
  2175. FreeScheme(psl);
  2176. g_lpszErr = GetResString(IDS_UNEXPECTED_ERROR);
  2177. return bRes;
  2178. }
  2179. else
  2180. {
  2181. return FALSE;
  2182. }
  2183. }
  2184. BOOL DoSetActive(
  2185. LPCTSTR lpszName,
  2186. BOOL bNumerical
  2187. )
  2188. /*++
  2189. Routine Description:
  2190. Sets the active scheme
  2191. Arguments:
  2192. lpszName - the name of the scheme
  2193. bNumerical - if TRUE, lpszName interpreted as numerical identifier
  2194. Return Value:
  2195. TRUE if successful
  2196. FALSE if failed
  2197. --*/
  2198. {
  2199. PSCHEME_LIST psl = FindScheme(
  2200. lpszName,
  2201. 0,
  2202. bNumerical
  2203. );
  2204. if (psl)
  2205. {
  2206. BOOL bRes = fSetActivePwrScheme(
  2207. psl->uiID,
  2208. NULL,
  2209. NULL
  2210. );
  2211. FreeScheme(psl);
  2212. g_lpszErr = GetResString(IDS_UNEXPECTED_ERROR);
  2213. return bRes;
  2214. }
  2215. else
  2216. {
  2217. return FALSE;
  2218. }
  2219. }
  2220. BOOL
  2221. DoChange(
  2222. LPCTSTR lpszName,
  2223. BOOL bNumerical,
  2224. PCHANGE_PARAM pcp
  2225. )
  2226. /*++
  2227. Routine Description:
  2228. Modifies an existing scheme
  2229. Arguments:
  2230. lpszName - the name of the scheme
  2231. bNumerical - if TRUE, lpszName interpreted as numerical identifier
  2232. pcp - PCHANGE_PARAM pointing to the parameter structure,
  2233. indicates which variable(s) to change
  2234. Return Value:
  2235. TRUE if successful
  2236. FALSE if failed
  2237. --*/
  2238. {
  2239. BOOL bRes = TRUE;
  2240. PSCHEME_LIST psl = FindScheme(
  2241. lpszName,
  2242. 0,
  2243. bNumerical
  2244. );
  2245. if (psl)
  2246. {
  2247. // check for feature support
  2248. if ((pcp->bIdleTimeoutAc ||
  2249. pcp->bIdleTimeoutDc) &&
  2250. !g_bStandbySupported)
  2251. {
  2252. DISPLAY_MESSAGE(stderr, GetResString(IDS_STANDBY_WARNING));
  2253. }
  2254. if ((pcp->bDozeS4TimeoutAc ||
  2255. pcp->bDozeS4TimeoutDc) &&
  2256. g_bStandbySupported &&
  2257. !g_bHiberTimerSupported)
  2258. {
  2259. //
  2260. // The wake from realtime clock in order to hibernate
  2261. // the system may not work. Warn the user.
  2262. //
  2263. DISPLAY_MESSAGE(stderr, GetResString(IDS_HIBER_WARNING));
  2264. }
  2265. if ((pcp->bVideoTimeoutAc ||
  2266. pcp->bVideoTimeoutDc) &&
  2267. !g_bMonitorPowerSupported)
  2268. {
  2269. DISPLAY_MESSAGE(stderr, GetResString(IDS_MONITOR_WARNING));
  2270. }
  2271. if ((pcp->bSpindownTimeoutAc ||
  2272. pcp->bSpindownTimeoutDc) &&
  2273. !g_bDiskPowerSupported)
  2274. {
  2275. DISPLAY_MESSAGE(stderr, GetResString(IDS_DISK_WARNING));
  2276. }
  2277. // change params
  2278. if (pcp->bVideoTimeoutAc)
  2279. {
  2280. psl->ppp->user.VideoTimeoutAc = pcp->ulVideoTimeoutAc*60;
  2281. }
  2282. if (pcp->bVideoTimeoutDc)
  2283. {
  2284. psl->ppp->user.VideoTimeoutDc = pcp->ulVideoTimeoutDc*60;
  2285. }
  2286. if (pcp->bSpindownTimeoutAc)
  2287. {
  2288. psl->ppp->user.SpindownTimeoutAc = pcp->ulSpindownTimeoutAc*60;
  2289. }
  2290. if (pcp->bSpindownTimeoutDc)
  2291. {
  2292. psl->ppp->user.SpindownTimeoutDc = pcp->ulSpindownTimeoutDc*60;
  2293. }
  2294. if (pcp->bIdleTimeoutAc)
  2295. {
  2296. bRes = bRes & MapIdleValue(
  2297. pcp->ulIdleTimeoutAc*60,
  2298. &psl->ppp->user.IdleTimeoutAc,
  2299. &psl->ppp->mach.DozeS4TimeoutAc,
  2300. &psl->ppp->user.IdleAc.Action
  2301. );
  2302. }
  2303. if (pcp->bIdleTimeoutDc)
  2304. {
  2305. bRes = bRes & MapIdleValue(
  2306. pcp->ulIdleTimeoutDc*60,
  2307. &psl->ppp->user.IdleTimeoutDc,
  2308. &psl->ppp->mach.DozeS4TimeoutDc,
  2309. &psl->ppp->user.IdleDc.Action
  2310. );
  2311. }
  2312. if (pcp->bDozeS4TimeoutAc)
  2313. {
  2314. bRes = bRes & MapHiberValue(
  2315. pcp->ulDozeS4TimeoutAc*60,
  2316. &psl->ppp->user.IdleTimeoutAc,
  2317. &psl->ppp->mach.DozeS4TimeoutAc,
  2318. &psl->ppp->user.IdleAc.Action
  2319. );
  2320. }
  2321. if (pcp->bDozeS4TimeoutDc)
  2322. {
  2323. bRes = bRes & MapHiberValue(
  2324. pcp->ulDozeS4TimeoutDc*60,
  2325. &psl->ppp->user.IdleTimeoutDc,
  2326. &psl->ppp->mach.DozeS4TimeoutDc,
  2327. &psl->ppp->user.IdleDc.Action
  2328. );
  2329. }
  2330. if (pcp->bDynamicThrottleAc)
  2331. {
  2332. if(lstrcmpi(
  2333. pcp->lpszDynamicThrottleAc,
  2334. _T("NONE")
  2335. ) == 0)
  2336. {
  2337. psl->pmppp->ProcessorPolicyAc.DynamicThrottle =
  2338. PO_THROTTLE_NONE;
  2339. }
  2340. else if(lstrcmpi(
  2341. pcp->lpszDynamicThrottleAc,
  2342. _T("CONSTANT")
  2343. ) == 0)
  2344. {
  2345. psl->pmppp->ProcessorPolicyAc.DynamicThrottle =
  2346. PO_THROTTLE_CONSTANT;
  2347. }
  2348. else if(lstrcmpi(
  2349. pcp->lpszDynamicThrottleAc,
  2350. _T("DEGRADE")
  2351. ) == 0)
  2352. {
  2353. psl->pmppp->ProcessorPolicyAc.DynamicThrottle =
  2354. PO_THROTTLE_DEGRADE;
  2355. }
  2356. else if(lstrcmpi(
  2357. pcp->lpszDynamicThrottleAc,
  2358. _T("ADAPTIVE")
  2359. ) == 0)
  2360. {
  2361. psl->pmppp->ProcessorPolicyAc.DynamicThrottle =
  2362. PO_THROTTLE_ADAPTIVE;
  2363. }
  2364. else
  2365. {
  2366. g_lpszErr = GetResString(IDS_INVALID_CMDLINE_PARAM);
  2367. bRes = FALSE;
  2368. }
  2369. }
  2370. if (pcp->bDynamicThrottleDc)
  2371. {
  2372. if(lstrcmpi(
  2373. pcp->lpszDynamicThrottleDc,
  2374. _T("NONE")
  2375. ) == 0)
  2376. {
  2377. psl->pmppp->ProcessorPolicyDc.DynamicThrottle =
  2378. PO_THROTTLE_NONE;
  2379. }
  2380. else if(lstrcmpi(
  2381. pcp->lpszDynamicThrottleDc,
  2382. _T("CONSTANT")
  2383. ) == 0)
  2384. {
  2385. psl->pmppp->ProcessorPolicyDc.DynamicThrottle =
  2386. PO_THROTTLE_CONSTANT;
  2387. }
  2388. else if(lstrcmpi(
  2389. pcp->lpszDynamicThrottleDc,
  2390. _T("DEGRADE")
  2391. ) == 0)
  2392. {
  2393. psl->pmppp->ProcessorPolicyDc.DynamicThrottle =
  2394. PO_THROTTLE_DEGRADE;
  2395. }
  2396. else if(lstrcmpi(
  2397. pcp->lpszDynamicThrottleDc,
  2398. _T("ADAPTIVE")
  2399. ) == 0)
  2400. {
  2401. psl->pmppp->ProcessorPolicyDc.DynamicThrottle =
  2402. PO_THROTTLE_ADAPTIVE;
  2403. }
  2404. else
  2405. {
  2406. g_lpszErr = GetResString(IDS_INVALID_CMDLINE_PARAM);
  2407. bRes = FALSE;
  2408. }
  2409. }
  2410. if (bRes)
  2411. {
  2412. // attempt to update power scheme
  2413. g_lpszErr = GetResString(IDS_UNEXPECTED_ERROR);
  2414. bRes = MyWriteScheme(psl);
  2415. // keep active power scheme consistent
  2416. if (bRes)
  2417. {
  2418. UINT uiIDactive;
  2419. if (fGetActivePwrScheme(&uiIDactive) &&
  2420. (psl->uiID == uiIDactive))
  2421. {
  2422. bRes = fSetActivePwrScheme(psl->uiID,NULL,NULL);
  2423. }
  2424. }
  2425. FreeScheme(psl);
  2426. return bRes;
  2427. }
  2428. else
  2429. {
  2430. return FALSE;
  2431. }
  2432. }
  2433. else
  2434. {
  2435. return FALSE;
  2436. }
  2437. }
  2438. BOOL
  2439. DoExport(
  2440. LPCTSTR lpszName,
  2441. BOOL bNumerical,
  2442. LPCTSTR lpszFile
  2443. )
  2444. /*++
  2445. Routine Description:
  2446. Exports a power scheme
  2447. Arguments:
  2448. lpszName - the name of the scheme
  2449. bNumerical - if TRUE, lpszName interpreted as numerical identifier
  2450. lpszFile - the file to hold the scheme
  2451. Return Value:
  2452. TRUE if successful
  2453. FALSE if failed
  2454. --*/
  2455. {
  2456. DWORD res; // write result value
  2457. HANDLE f; // file handle
  2458. // find scheme
  2459. PSCHEME_LIST psl = FindScheme(
  2460. lpszName,
  2461. 0,
  2462. bNumerical
  2463. );
  2464. if(!psl) {
  2465. return FALSE;
  2466. }
  2467. // write to file
  2468. f = CreateFile(
  2469. lpszFile,
  2470. GENERIC_WRITE,
  2471. 0,
  2472. NULL,
  2473. CREATE_ALWAYS,
  2474. FILE_ATTRIBUTE_NORMAL,
  2475. NULL
  2476. );
  2477. if (f == INVALID_HANDLE_VALUE)
  2478. {
  2479. FormatMessage(
  2480. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  2481. FORMAT_MESSAGE_FROM_SYSTEM |
  2482. FORMAT_MESSAGE_IGNORE_INSERTS,
  2483. NULL,
  2484. GetLastError(),
  2485. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  2486. (LPTSTR)&g_lpszErr2,
  2487. 0,
  2488. NULL
  2489. );
  2490. FreeScheme(psl);
  2491. return FALSE;
  2492. }
  2493. if (!WriteFile(
  2494. f,
  2495. psl->ppp,
  2496. sizeof(POWER_POLICY),
  2497. &res,
  2498. NULL
  2499. ))
  2500. {
  2501. FormatMessage(
  2502. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  2503. FORMAT_MESSAGE_FROM_SYSTEM |
  2504. FORMAT_MESSAGE_IGNORE_INSERTS,
  2505. NULL,
  2506. GetLastError(),
  2507. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  2508. (LPTSTR)&g_lpszErr2,
  2509. 0,
  2510. NULL
  2511. );
  2512. CloseHandle(f);
  2513. FreeScheme(psl);
  2514. return FALSE;
  2515. }
  2516. if (g_bProcessorPwrSchemeSupported)
  2517. {
  2518. if (!WriteFile(
  2519. f,
  2520. psl->pmppp,
  2521. sizeof(MACHINE_PROCESSOR_POWER_POLICY),
  2522. &res,
  2523. NULL
  2524. ))
  2525. {
  2526. FormatMessage(
  2527. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  2528. FORMAT_MESSAGE_FROM_SYSTEM |
  2529. FORMAT_MESSAGE_IGNORE_INSERTS,
  2530. NULL,
  2531. GetLastError(),
  2532. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  2533. (LPTSTR)&g_lpszErr2,
  2534. 0,
  2535. NULL
  2536. );
  2537. CloseHandle(f);
  2538. FreeScheme(psl);
  2539. return FALSE;
  2540. }
  2541. }
  2542. CloseHandle(f);
  2543. FreeScheme(psl);
  2544. return TRUE;
  2545. }
  2546. BOOL
  2547. DoImport(
  2548. LPCTSTR lpszName,
  2549. BOOL bNumerical,
  2550. LPCTSTR lpszFile
  2551. )
  2552. /*++
  2553. Routine Description:
  2554. Imports a power scheme
  2555. If the scheme already exists, overwrites it
  2556. Arguments:
  2557. lpszName - the name of the scheme
  2558. bNumerical - if TRUE, lpszName interpreted as numerical identifier
  2559. lpszFile - the file that holds the scheme
  2560. Return Value:
  2561. TRUE if successful
  2562. FALSE if failed
  2563. --*/
  2564. {
  2565. DWORD res; // write result value
  2566. HANDLE f; // file handle
  2567. UINT uiIDactive; // active ID
  2568. PSCHEME_LIST psl;
  2569. // check for pre-existing scheme
  2570. psl = FindScheme(
  2571. lpszName,
  2572. 0,
  2573. bNumerical
  2574. );
  2575. // if didn't exist, create it (if actual name given)
  2576. if (!psl)
  2577. {
  2578. if (!bNumerical)
  2579. {
  2580. psl = CreateScheme(
  2581. NEWSCHEME,
  2582. (lstrlen(lpszName)+1)*sizeof(TCHAR),
  2583. lpszName,
  2584. (lstrlen(lpszName)+1)*sizeof(TCHAR),
  2585. lpszName,
  2586. NULL // psl->ppp will be allocated but uninitialized
  2587. );
  2588. // check for successful alloc
  2589. if(!psl)
  2590. {
  2591. return FALSE;
  2592. }
  2593. }
  2594. else
  2595. {
  2596. g_lpszErr = GetResString(IDS_INVALID_NUMERICAL_IMPORT);
  2597. return FALSE;
  2598. }
  2599. }
  2600. // open file
  2601. f = CreateFile(
  2602. lpszFile,
  2603. GENERIC_READ,
  2604. FILE_SHARE_READ,
  2605. NULL,
  2606. OPEN_EXISTING,
  2607. FILE_ATTRIBUTE_NORMAL,
  2608. NULL
  2609. );
  2610. if (f == INVALID_HANDLE_VALUE)
  2611. {
  2612. FormatMessage(
  2613. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  2614. FORMAT_MESSAGE_FROM_SYSTEM |
  2615. FORMAT_MESSAGE_IGNORE_INSERTS,
  2616. NULL,
  2617. GetLastError(),
  2618. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  2619. (LPTSTR)&g_lpszErr2,
  2620. 0,
  2621. NULL
  2622. );
  2623. FreeScheme(psl);
  2624. return FALSE;
  2625. }
  2626. // read scheme
  2627. if (!ReadFile(
  2628. f,
  2629. psl->ppp,
  2630. sizeof(POWER_POLICY),
  2631. &res,
  2632. NULL
  2633. ))
  2634. {
  2635. FormatMessage(
  2636. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  2637. FORMAT_MESSAGE_FROM_SYSTEM |
  2638. FORMAT_MESSAGE_IGNORE_INSERTS,
  2639. NULL,
  2640. GetLastError(),
  2641. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  2642. (LPTSTR)&g_lpszErr2,
  2643. 0,
  2644. NULL
  2645. );
  2646. CloseHandle(f);
  2647. FreeScheme(psl);
  2648. return FALSE;
  2649. }
  2650. if (g_bProcessorPwrSchemeSupported)
  2651. {
  2652. if (!ReadFile(
  2653. f,
  2654. psl->pmppp,
  2655. sizeof(MACHINE_PROCESSOR_POWER_POLICY),
  2656. &res,
  2657. NULL
  2658. ))
  2659. {
  2660. // copy processor profile from the active scheme,
  2661. // thus supporting Win2k->WinXP imports
  2662. if(fGetActivePwrScheme(&uiIDactive))
  2663. {
  2664. PSCHEME_LIST pslActive = FindScheme(
  2665. NULL,
  2666. uiIDactive,
  2667. FALSE
  2668. );
  2669. if(!pslActive)
  2670. {
  2671. g_lpszErr = GetResString(IDS_UNEXPECTED_ERROR);
  2672. CloseHandle(f);
  2673. FreeScheme(psl);
  2674. return FALSE;
  2675. }
  2676. memcpy(
  2677. psl->pmppp,
  2678. pslActive->pmppp,
  2679. sizeof(MACHINE_PROCESSOR_POWER_POLICY)
  2680. );
  2681. FreeScheme(pslActive);
  2682. }
  2683. else
  2684. {
  2685. g_lpszErr = GetResString(IDS_UNEXPECTED_ERROR);
  2686. CloseHandle(f);
  2687. FreeScheme(psl);
  2688. return FALSE;
  2689. }
  2690. }
  2691. }
  2692. CloseHandle(f);
  2693. g_lpszErr = GetResString(IDS_UNEXPECTED_ERROR);
  2694. // save scheme
  2695. if (!MyWriteScheme(psl))
  2696. {
  2697. FreeScheme(psl);
  2698. return FALSE;
  2699. }
  2700. // check against active scheme
  2701. if (!fGetActivePwrScheme(&uiIDactive))
  2702. {
  2703. return FALSE;
  2704. }
  2705. if (uiIDactive == psl->uiID)
  2706. {
  2707. if (!fSetActivePwrScheme(psl->uiID,NULL,NULL))
  2708. {
  2709. return FALSE;
  2710. }
  2711. }
  2712. FreeScheme(psl);
  2713. return TRUE;
  2714. }
  2715. PowerLoggingMessage*
  2716. GetLoggingMessage(
  2717. PSYSTEM_POWER_STATE_DISABLE_REASON LoggingInfo,
  2718. DWORD BaseMessage,
  2719. HINSTANCE hInst
  2720. )
  2721. /*++
  2722. Routine Description:
  2723. Wrapper to instantiate the appropriate PowerLoggingMessage based on
  2724. the passed in LoggingInfo data.
  2725. Arguments:
  2726. LoggingInfo - reason code structure.
  2727. BaseMessage - base resource ID for this power failure. used to lookup
  2728. the correct resource.
  2729. hInst - module handle for looking up resource.
  2730. Return Value:
  2731. returns a newly instantiated PowerLoggingMessage object or NULL if this
  2732. fails.
  2733. --*/
  2734. {
  2735. PowerLoggingMessage *LoggingMessage = NULL;
  2736. //
  2737. // these classes can throw if they hit an allocation error.
  2738. // catch it.
  2739. //
  2740. try {
  2741. switch (LoggingInfo->PowerReasonCode) {
  2742. case SPSD_REASON_LEGACYDRIVER:
  2743. LoggingMessage = new SubstituteMultiSzPowerLoggingMessage(
  2744. LoggingInfo,
  2745. BaseMessage,
  2746. hInst);
  2747. break;
  2748. case SPSD_REASON_HIBERFILE:
  2749. case SPSD_REASON_POINTERNAL:
  2750. LoggingMessage = new SubstituteNtStatusPowerLoggingMessage(
  2751. LoggingInfo,
  2752. BaseMessage,
  2753. hInst);
  2754. break;
  2755. #ifdef IA64
  2756. //
  2757. // on IA64 we want a slightly different message for this
  2758. // reason -- IA64 OS doesn't support these standby states
  2759. // today, but on IA32 this means you are not in ACPI mode.
  2760. //
  2761. // So we have this IA64 message arbitrarily offset by 50.
  2762. case SPSD_REASON_NOOSPM:
  2763. LoggingMessage = new SubstituteNtStatusPowerLoggingMessage(
  2764. LoggingInfo,
  2765. BaseMessage+50,
  2766. hInst);
  2767. break;
  2768. #endif
  2769. case SPSD_REASON_DRIVERDOWNGRADE:
  2770. default:
  2771. LoggingMessage = new NoSubstitutionPowerLoggingMessage(
  2772. LoggingInfo,
  2773. BaseMessage,
  2774. hInst);
  2775. break;
  2776. }
  2777. ASSERT(LoggingMessage!= NULL);
  2778. } catch (...) {
  2779. }
  2780. return(LoggingMessage);
  2781. }
  2782. BOOL
  2783. GetAndAppendDescriptiveLoggingText(
  2784. NTSTATUS HiberStatus,
  2785. LPTSTR *CurrentErrorText,
  2786. PSYSTEM_POWER_STATE_DISABLE_REASON LoggingInfo)
  2787. /*++
  2788. Routine Description:
  2789. given a failed hibernation, this routine retrieves some descriptive text
  2790. for why hibernate isn't available.
  2791. Arguments:
  2792. HiberStatus - status code from enabling hibernate.
  2793. CurrentErrorText - pointer to the current error code text.
  2794. LoggingInfo - pointer to logging code with one reason one for failed
  2795. Return Value:
  2796. TRUE if successful
  2797. FALSE if failed
  2798. --*/
  2799. {
  2800. PWSTR ReasonString = NULL;
  2801. PCWSTR pRootString;
  2802. PCWSTR pRootHiberFailedString = NULL;
  2803. DWORD Length = 0;
  2804. PowerLoggingMessage *LoggingMessage = NULL;
  2805. PWSTR FinalString;
  2806. BOOL RetVal = FALSE;
  2807. //
  2808. // if we don't have any error text yet, then we need to look up
  2809. // a header error message as the base of the message. otherwise
  2810. // we just append.
  2811. //
  2812. if (!*CurrentErrorText) {
  2813. pRootString = GetResString(IDS_HIBER_FAILED_DESCRIPTION_HEADER);
  2814. if (!pRootString) {
  2815. return(FALSE);
  2816. }
  2817. FormatMessage(
  2818. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  2819. FORMAT_MESSAGE_FROM_SYSTEM |
  2820. FORMAT_MESSAGE_IGNORE_INSERTS,
  2821. NULL,
  2822. RtlNtStatusToDosError(HiberStatus),
  2823. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  2824. (LPTSTR)&pRootHiberFailedString,
  2825. 0,
  2826. NULL
  2827. );
  2828. Length += wcslen(pRootString);
  2829. if (pRootHiberFailedString) {
  2830. Length += wcslen(pRootHiberFailedString);
  2831. }
  2832. } else {
  2833. Length += wcslen(*CurrentErrorText);
  2834. }
  2835. //
  2836. // get the logging reason text.
  2837. //
  2838. LoggingMessage = GetLoggingMessage(LoggingInfo,
  2839. IDS_BASE_HIBER_REASON_CODE,
  2840. GetModuleHandle(NULL));
  2841. ASSERT(LoggingMessage!= NULL);
  2842. if (!LoggingMessage->GetString(&ReasonString)) {
  2843. RetVal = FALSE;
  2844. goto exit;
  2845. }
  2846. Length += wcslen(ReasonString);
  2847. //
  2848. // now that we have the length for everything, allocate space,
  2849. // and fill it in with our text, either the prior text, or the
  2850. // header.
  2851. //
  2852. FinalString = (LPTSTR)LocalAlloc(LPTR,(Length+1)*sizeof(WCHAR));
  2853. if (!FinalString) {
  2854. RetVal = FALSE;
  2855. goto exit;
  2856. }
  2857. if (!*CurrentErrorText) {
  2858. wsprintf(FinalString,pRootString,pRootHiberFailedString);
  2859. } else {
  2860. wcscpy(FinalString,*CurrentErrorText);
  2861. }
  2862. wcscat(FinalString,ReasonString);
  2863. //
  2864. // if we appended onto existing text, we can free the old text
  2865. // and replace it with our new string.
  2866. //
  2867. if (*CurrentErrorText) {
  2868. LocalFree(*CurrentErrorText);
  2869. }
  2870. *CurrentErrorText = FinalString;
  2871. RetVal = TRUE;
  2872. exit:
  2873. if (pRootHiberFailedString) {
  2874. LocalFree((PWSTR)pRootHiberFailedString);
  2875. }
  2876. if (ReasonString) {
  2877. LocalFree(ReasonString);
  2878. }
  2879. if (LoggingMessage) {
  2880. delete LoggingMessage;
  2881. }
  2882. return (RetVal);
  2883. }
  2884. BOOL
  2885. DoHibernate(
  2886. LPCTSTR lpszBoolStr
  2887. )
  2888. /*++
  2889. Routine Description:
  2890. Enables/Disables hibernation
  2891. NOTE: this functionality pretty much taken verbatim from the test program
  2892. "base\ntos\po\tests\ehib\ehib.c"
  2893. Arguments:
  2894. lpszBoolStr - "on" or "off"
  2895. Return Value:
  2896. TRUE if successful
  2897. FALSE if failed
  2898. --*/
  2899. {
  2900. GLOBAL_POWER_POLICY PowerPolicy;
  2901. DWORD uiIDactive;
  2902. BOOL bChangePolicy = FALSE;
  2903. BOOLEAN bEnable; // doesn't work with a BOOL, apparently
  2904. NTSTATUS Status;
  2905. // parse enable/disable state
  2906. if (!lstrcmpi(lpszBoolStr,GetResString(IDS_ON)))
  2907. {
  2908. bEnable = TRUE;
  2909. }
  2910. else if (!lstrcmpi(lpszBoolStr,GetResString(IDS_OFF)))
  2911. {
  2912. bEnable = FALSE;
  2913. if (fGetGlobalPwrPolicy(&PowerPolicy)) {
  2914. if (PowerPolicy.user.DischargePolicy[DISCHARGE_POLICY_LOW].
  2915. PowerPolicy.Action == PowerActionHibernate) {
  2916. PowerPolicy.user.DischargePolicy[DISCHARGE_POLICY_LOW].
  2917. PowerPolicy.Action = PowerActionNone;
  2918. bChangePolicy = TRUE;
  2919. }
  2920. if (PowerPolicy.user.DischargePolicy[DISCHARGE_POLICY_CRITICAL].
  2921. PowerPolicy.Action == PowerActionHibernate) {
  2922. PowerPolicy.user.DischargePolicy[DISCHARGE_POLICY_CRITICAL].
  2923. PowerPolicy.Action = PowerActionNone;
  2924. bChangePolicy = TRUE;
  2925. }
  2926. if (bChangePolicy) {
  2927. if (fWriteGlobalPwrPolicy(&PowerPolicy) &&
  2928. fGetActivePwrScheme((PUINT)&uiIDactive) &&
  2929. fSetActivePwrScheme(uiIDactive,&PowerPolicy,NULL)){
  2930. DISPLAY_MESSAGE(stderr, GetResString(IDS_HIBERNATE_ALARM_DISABLED));
  2931. }
  2932. else
  2933. {
  2934. DISPLAY_MESSAGE(stderr, GetResString(IDS_HIBERNATE_ALARM_DISABLE_FAILED));
  2935. }
  2936. }
  2937. }
  2938. }
  2939. else
  2940. {
  2941. g_lpszErr = GetResString(IDS_HIBER_INVALID_STATE);
  2942. return FALSE;
  2943. }
  2944. // enable/disable hibernation
  2945. if (!g_bHiberFileSupported)
  2946. {
  2947. g_lpszErr = GetResString(IDS_HIBER_UNSUPPORTED);
  2948. Status = STATUS_NOT_SUPPORTED;
  2949. }
  2950. else {
  2951. //
  2952. // do the actual hibernate enable/disable operation.
  2953. //
  2954. Status = fCallNtPowerInformation(
  2955. SystemReserveHiberFile,
  2956. &bEnable,
  2957. sizeof(bEnable),
  2958. NULL,
  2959. 0
  2960. );
  2961. }
  2962. //
  2963. // print out an error message. if we can, we use the verbose error
  2964. // message, otherwise we just fall back on the error code coming back
  2965. // from NtPowerInformation.
  2966. //
  2967. if (!NT_SUCCESS(Status)) {
  2968. //
  2969. // remember the specific error message
  2970. //
  2971. PVOID LoggingInfoBuffer = NULL;
  2972. PSYSTEM_POWER_STATE_DISABLE_REASON LoggingInfo;
  2973. ULONG size,LoggingInfoSize;
  2974. NTSTATUS HiberStatus = Status;
  2975. FormatMessage(
  2976. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  2977. FORMAT_MESSAGE_FROM_SYSTEM |
  2978. FORMAT_MESSAGE_IGNORE_INSERTS,
  2979. NULL,
  2980. RtlNtStatusToDosError(Status),
  2981. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  2982. (LPTSTR)&g_lpszErr2,
  2983. 0,
  2984. NULL
  2985. );
  2986. //
  2987. // try to get the verbose reason why hibernate fails.
  2988. //
  2989. Status = STATUS_INSUFFICIENT_RESOURCES;
  2990. size = 1024;
  2991. LoggingInfoBuffer = LocalAlloc(LPTR,size);
  2992. if (!LoggingInfoBuffer) {
  2993. return(FALSE);
  2994. }
  2995. while (Status != STATUS_SUCCESS) {
  2996. Status = fCallNtPowerInformation(
  2997. SystemPowerStateLogging,
  2998. NULL,
  2999. 0,
  3000. LoggingInfoBuffer,
  3001. size);
  3002. if (!NT_SUCCESS(Status)) {
  3003. if (Status != STATUS_INSUFFICIENT_RESOURCES) {
  3004. LocalFree(LoggingInfoBuffer);
  3005. return(FALSE);
  3006. } else {
  3007. size += 1024;
  3008. LocalFree(LoggingInfoBuffer);
  3009. LoggingInfoBuffer = LocalAlloc(LPTR,size);
  3010. if (!LoggingInfoBuffer) {
  3011. return(FALSE);
  3012. }
  3013. }
  3014. }
  3015. }
  3016. ASSERT(Status == STATUS_SUCCESS);
  3017. LoggingInfoSize = (ULONG)*(PULONG)LoggingInfoBuffer;
  3018. LoggingInfo = (PSYSTEM_POWER_STATE_DISABLE_REASON)(PCHAR)((PCHAR)LoggingInfoBuffer+sizeof(ULONG));
  3019. //
  3020. // we have a more verbose error available so let's use that. don't need
  3021. // the less verbose error.
  3022. //
  3023. if (g_lpszErr2) {
  3024. LocalFree(g_lpszErr2);
  3025. g_lpszErr2 = NULL;
  3026. }
  3027. //
  3028. // walk through the list of reasons and print out the ones related to
  3029. // hibernate.
  3030. //
  3031. while((PCHAR)LoggingInfo <= (PCHAR)((PCHAR)LoggingInfoBuffer + LoggingInfoSize)) {
  3032. if (LoggingInfo->AffectedState[PowerStateSleeping4] == TRUE) {
  3033. //
  3034. // need to remember the reason
  3035. //
  3036. GetAndAppendDescriptiveLoggingText(
  3037. HiberStatus,
  3038. &g_lpszErr2,
  3039. LoggingInfo);
  3040. }
  3041. LoggingInfo = (PSYSTEM_POWER_STATE_DISABLE_REASON)(PCHAR)((PCHAR)LoggingInfo+sizeof(SYSTEM_POWER_STATE_DISABLE_REASON)+LoggingInfo->PowerReasonLength);
  3042. }
  3043. return FALSE;
  3044. }
  3045. return TRUE;
  3046. }
  3047. BOOL
  3048. DoGetSupportedSStates(
  3049. VOID
  3050. )
  3051. /*++
  3052. Routine Description:
  3053. Lists the available S-States on a machine.
  3054. Arguments:
  3055. None.
  3056. Return Value:
  3057. TRUE if successful
  3058. FALSE if failed
  3059. --*/
  3060. {
  3061. NTSTATUS Status;
  3062. SYSTEM_POWER_CAPABILITIES Capabilities;
  3063. BOOL StandbyAvailable = FALSE;
  3064. BOOL HibernateAvailable = FALSE;
  3065. PVOID LoggingInfoBuffer = NULL;
  3066. PSYSTEM_POWER_STATE_DISABLE_REASON LoggingInfo;
  3067. ULONG size,LoggingInfoSize;
  3068. PowerLoggingMessage *LoggingMessage;
  3069. PWSTR ReasonString;
  3070. DWORD i;
  3071. BOOL ExitLoop;
  3072. BOOL LoggingApiAvailable;
  3073. //
  3074. // call the power state logging API if it's available. on older systems
  3075. // this API isn't avaialable and that should not be a problem.
  3076. //
  3077. Status = STATUS_INSUFFICIENT_RESOURCES;
  3078. size = 1024;
  3079. LoggingInfoBuffer = LocalAlloc(LPTR,size);
  3080. if (!LoggingInfoBuffer) {
  3081. LoggingApiAvailable = FALSE;
  3082. goto GetStaticStates;
  3083. }
  3084. while (Status != STATUS_SUCCESS) {
  3085. Status = fCallNtPowerInformation(
  3086. SystemPowerStateLogging,
  3087. NULL,
  3088. 0,
  3089. LoggingInfoBuffer,
  3090. size);
  3091. if (!NT_SUCCESS(Status)) {
  3092. if (Status != STATUS_INSUFFICIENT_RESOURCES) {
  3093. LocalFree(LoggingInfoBuffer);
  3094. LoggingInfoBuffer = NULL;
  3095. LoggingApiAvailable = FALSE;
  3096. goto GetStaticStates;
  3097. } else {
  3098. size += 1024;
  3099. LocalFree(LoggingInfoBuffer);
  3100. LoggingInfoBuffer = LocalAlloc(LPTR,size);
  3101. if (!LoggingInfoBuffer) {
  3102. LoggingApiAvailable = FALSE;
  3103. goto GetStaticStates;
  3104. }
  3105. }
  3106. }
  3107. }
  3108. //
  3109. // we have the verbose logging structure. remember that for later on.
  3110. //
  3111. LoggingApiAvailable = TRUE;
  3112. LoggingInfoSize = (ULONG)*(PULONG)LoggingInfoBuffer;
  3113. GetStaticStates:
  3114. //
  3115. // get the current power capabilities of the system.
  3116. //
  3117. Status = fCallNtPowerInformation(
  3118. SystemPowerCapabilities,
  3119. NULL,
  3120. 0,
  3121. &Capabilities,
  3122. sizeof(SYSTEM_POWER_CAPABILITIES)
  3123. );
  3124. if (!NT_SUCCESS(Status)) {
  3125. //
  3126. // print out failure message
  3127. //
  3128. g_lpszErr = GetResString(IDS_CANTGETSLEEPSTATES);
  3129. return(FALSE);
  3130. }
  3131. //
  3132. // if the logging API is available, it may tell us that
  3133. // one of the S states isn't really available. Process
  3134. // that "override" data here so that we're sure to print
  3135. // out the correct list of supported states on this system.
  3136. //
  3137. if (LoggingApiAvailable) {
  3138. LoggingInfo = (PSYSTEM_POWER_STATE_DISABLE_REASON)(PCHAR)((PCHAR)LoggingInfoBuffer+sizeof(ULONG));
  3139. while((PCHAR)LoggingInfo <= (PCHAR)((PCHAR)LoggingInfoBuffer + LoggingInfoSize)) {
  3140. if (LoggingInfo->PowerReasonCode != SPSD_REASON_NONE) {
  3141. if (LoggingInfo->AffectedState[PowerStateSleeping1] == TRUE) {
  3142. Capabilities.SystemS1 = FALSE;
  3143. }
  3144. if (LoggingInfo->AffectedState[PowerStateSleeping2] == TRUE) {
  3145. Capabilities.SystemS2 = FALSE;
  3146. }
  3147. if (LoggingInfo->AffectedState[PowerStateSleeping3] == TRUE) {
  3148. Capabilities.SystemS3 = FALSE;
  3149. }
  3150. if (LoggingInfo->AffectedState[PowerStateSleeping4] == TRUE) {
  3151. Capabilities.SystemS4 = FALSE;
  3152. }
  3153. }
  3154. LoggingInfo = (PSYSTEM_POWER_STATE_DISABLE_REASON)(PCHAR)((PCHAR)LoggingInfo+sizeof(SYSTEM_POWER_STATE_DISABLE_REASON)+LoggingInfo->PowerReasonLength);
  3155. }
  3156. }
  3157. //
  3158. // print out the list of supported s states.
  3159. //
  3160. if (Capabilities.SystemS1 ||
  3161. Capabilities.SystemS2 ||
  3162. Capabilities.SystemS3) {
  3163. StandbyAvailable = TRUE;
  3164. }
  3165. if (Capabilities.SystemS4) {
  3166. HibernateAvailable = TRUE;
  3167. }
  3168. if (StandbyAvailable || HibernateAvailable) {
  3169. //
  3170. // "the following sleep states are available on this machine: "
  3171. //
  3172. DISPLAY_MESSAGE(stdout,GetResString(IDS_SLEEPSTATES_AVAILABLE));
  3173. DISPLAY_MESSAGE(stdout,L" ");
  3174. if (StandbyAvailable) {
  3175. //" Standby ("
  3176. // IDS_STANDBY " " IDS_LEFTPAREN
  3177. DISPLAY_MESSAGE(stdout,GetResString(IDS_STANDBY));
  3178. DISPLAY_MESSAGE(stdout,L" ");
  3179. DISPLAY_MESSAGE(stdout,GetResString(IDS_LEFTPAREN));
  3180. DISPLAY_MESSAGE(stdout,L" ");
  3181. if (Capabilities.SystemS1) {
  3182. //"S1 "
  3183. //IDS_S1
  3184. DISPLAY_MESSAGE(stdout,GetResString(IDS_S1));
  3185. DISPLAY_MESSAGE(stdout,L" ");
  3186. }
  3187. if (Capabilities.SystemS2) {
  3188. //"S2 "
  3189. //IDS_S2
  3190. DISPLAY_MESSAGE(stdout,GetResString(IDS_S2));
  3191. DISPLAY_MESSAGE(stdout,L" ");
  3192. }
  3193. if (Capabilities.SystemS3) {
  3194. //"S3"
  3195. //IDS_S3
  3196. DISPLAY_MESSAGE(stdout,GetResString(IDS_S3));
  3197. DISPLAY_MESSAGE(stdout,L" ");
  3198. }
  3199. //")"
  3200. //IDS_RIGHTPAREN
  3201. DISPLAY_MESSAGE(stdout,GetResString(IDS_RIGHTPAREN));
  3202. DISPLAY_MESSAGE(stdout,L" ");
  3203. }
  3204. if (HibernateAvailable) {
  3205. //" Hibernate"
  3206. //IDS_HIBERNATE
  3207. DISPLAY_MESSAGE(stdout,GetResString(IDS_HIBERNATE));
  3208. DISPLAY_MESSAGE(stdout,L" ");
  3209. }
  3210. DISPLAY_MESSAGE(stdout,L"\n");
  3211. }
  3212. //
  3213. // if one or more capabilities are missing then find out why and
  3214. // print it out.
  3215. //
  3216. if (!Capabilities.SystemS1 ||
  3217. !Capabilities.SystemS2 ||
  3218. !Capabilities.SystemS3 ||
  3219. !Capabilities.SystemS4) {
  3220. //
  3221. // "the following sleep states are not available on this machine:"
  3222. //
  3223. //IDS_SLEEPSTATES_UNAVAILABLE
  3224. DISPLAY_MESSAGE(stdout,GetResString(IDS_SLEEPSTATES_UNAVAILABLE));
  3225. DISPLAY_MESSAGE(stdout,L"\n");
  3226. i = 0;
  3227. ExitLoop = FALSE;
  3228. while (1) {
  3229. BOOL NotSupported;
  3230. DWORD BaseMessage;
  3231. DWORD HeaderMessage;
  3232. POWER_STATE_HANDLER_TYPE SystemPowerState;
  3233. //
  3234. // remember some resource ids for the S state we're
  3235. // currently considering
  3236. //
  3237. switch (i) {
  3238. case 0:
  3239. BaseMessage = IDS_BASE_SX_REASON_CODE;
  3240. HeaderMessage = IDS_BASE_S1_HEADER;
  3241. SystemPowerState = PowerStateSleeping1;
  3242. NotSupported = !Capabilities.SystemS1;
  3243. break;
  3244. case 1:
  3245. BaseMessage = IDS_BASE_SX_REASON_CODE;
  3246. HeaderMessage = IDS_BASE_S2_HEADER;
  3247. SystemPowerState = PowerStateSleeping2;
  3248. NotSupported = !Capabilities.SystemS2;
  3249. break;
  3250. case 2:
  3251. BaseMessage = IDS_BASE_SX_REASON_CODE;
  3252. HeaderMessage = IDS_BASE_S3_HEADER;
  3253. SystemPowerState = PowerStateSleeping3;
  3254. NotSupported = !Capabilities.SystemS3;
  3255. break;
  3256. case 3:
  3257. BaseMessage = IDS_BASE_HIBER_REASON_CODE;
  3258. HeaderMessage = IDS_HIBERNATE;
  3259. SystemPowerState = PowerStateSleeping4;
  3260. NotSupported = !Capabilities.SystemS4;
  3261. break;
  3262. default:
  3263. ExitLoop = TRUE;
  3264. }
  3265. if (ExitLoop) {
  3266. break;
  3267. }
  3268. if (NotSupported) {
  3269. //"Standby (S1)" BaseMessage...
  3270. DISPLAY_MESSAGE(stdout,GetResString(HeaderMessage));
  3271. DISPLAY_MESSAGE(stdout,L"\n");
  3272. if (LoggingApiAvailable) {
  3273. LoggingInfo = (PSYSTEM_POWER_STATE_DISABLE_REASON)(PCHAR)((PCHAR)LoggingInfoBuffer+sizeof(ULONG));
  3274. while((PCHAR)LoggingInfo <= (PCHAR)((PCHAR)LoggingInfoBuffer + LoggingInfoSize)) {
  3275. if (LoggingInfo->AffectedState[SystemPowerState]) {
  3276. //
  3277. // get reason, print it out.
  3278. //
  3279. LoggingMessage = GetLoggingMessage(
  3280. LoggingInfo,
  3281. BaseMessage,
  3282. GetModuleHandle(NULL));
  3283. if (!LoggingMessage ||
  3284. !LoggingMessage->GetString(&ReasonString)) {
  3285. // oops
  3286. // IDS_CANTGETSSTATEREASONS
  3287. g_lpszErr = GetResString(IDS_CANTGETSSTATEREASONS);
  3288. LocalFree(LoggingInfoBuffer);
  3289. return(FALSE);
  3290. }
  3291. DISPLAY_MESSAGE(stdout,ReasonString);
  3292. LocalFree(ReasonString);
  3293. delete LoggingMessage;
  3294. }
  3295. LoggingInfo = (PSYSTEM_POWER_STATE_DISABLE_REASON)(PCHAR)((PCHAR)LoggingInfo+sizeof(SYSTEM_POWER_STATE_DISABLE_REASON)+LoggingInfo->PowerReasonLength);
  3296. }
  3297. }
  3298. }
  3299. i += 1;
  3300. }
  3301. if (LoggingInfoBuffer) {
  3302. LocalFree(LoggingInfoBuffer);
  3303. }
  3304. }
  3305. return(TRUE);
  3306. }
  3307. BOOL
  3308. DoGlobalFlag(
  3309. LPCTSTR lpszBoolStr,
  3310. LPCTSTR lpszGlobalFlagOption
  3311. )
  3312. /*++
  3313. Routine Description:
  3314. Enables/Disables a global flag
  3315. Arguments:
  3316. lpszBoolStr - "on" or "off"
  3317. lpszGlobalFlagOption - one of several flags.
  3318. Return Value:
  3319. TRUE if successful
  3320. FALSE if failed
  3321. --*/
  3322. {
  3323. BOOLEAN bEnable; // doesn't work with a BOOL, apparently
  3324. BOOL RetVal;
  3325. GLOBAL_POWER_POLICY PowerPolicy;
  3326. DWORD GlobalFlag = 0;
  3327. DWORD uiIDactive;
  3328. // parse enable/disable state
  3329. if (!lstrcmpi(lpszBoolStr,GetResString(IDS_ON)))
  3330. {
  3331. bEnable = TRUE;
  3332. }
  3333. else if (!lstrcmpi(lpszBoolStr,GetResString(IDS_OFF)))
  3334. {
  3335. bEnable = FALSE;
  3336. }
  3337. else
  3338. {
  3339. g_lpszErr = GetResString(IDS_GLOBAL_FLAG_INVALID_STATE);
  3340. RetVal = FALSE;
  3341. goto exit;
  3342. }
  3343. // parse which global flag we are changing
  3344. if (!lstrcmpi(lpszGlobalFlagOption,CMDOPTION_BATTERYICON)) {
  3345. GlobalFlag |= EnableSysTrayBatteryMeter;
  3346. } else if (!lstrcmpi(lpszGlobalFlagOption,CMDOPTION_MULTIBATTERY)) {
  3347. GlobalFlag |= EnableMultiBatteryDisplay;
  3348. } else if (!lstrcmpi(lpszGlobalFlagOption,CMDOPTION_RESUMEPASSWORD)) {
  3349. GlobalFlag |= EnablePasswordLogon;
  3350. } else if (!lstrcmpi(lpszGlobalFlagOption,CMDOPTION_WAKEONRING)) {
  3351. GlobalFlag |= EnableWakeOnRing;
  3352. } else if (!lstrcmpi(lpszGlobalFlagOption,CMDOPTION_VIDEODIM)) {
  3353. GlobalFlag |= EnableVideoDimDisplay;
  3354. } else {
  3355. g_lpszErr = GetResString(IDS_GLOBAL_FLAG_INVALID_FLAG);
  3356. RetVal = FALSE;
  3357. goto exit;
  3358. }
  3359. //
  3360. // now get the current state, set or clear the flags, and then save the
  3361. // changed settings back.
  3362. //
  3363. RetVal = FALSE;
  3364. if (fGetGlobalPwrPolicy(&PowerPolicy)) {
  3365. if (bEnable) {
  3366. PowerPolicy.user.GlobalFlags |= GlobalFlag;
  3367. } else {
  3368. PowerPolicy.user.GlobalFlags &= ~(GlobalFlag);
  3369. }
  3370. if (fWriteGlobalPwrPolicy(&PowerPolicy) &&
  3371. fGetActivePwrScheme((PUINT)&uiIDactive) &&
  3372. fSetActivePwrScheme(uiIDactive,&PowerPolicy,NULL)){
  3373. RetVal = TRUE;
  3374. }
  3375. }
  3376. //
  3377. // save off the error if we had issues
  3378. //
  3379. if (!RetVal) {
  3380. FormatMessage(
  3381. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  3382. FORMAT_MESSAGE_FROM_SYSTEM |
  3383. FORMAT_MESSAGE_IGNORE_INSERTS,
  3384. NULL,
  3385. GetLastError(),
  3386. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  3387. (LPTSTR)&g_lpszErr2,
  3388. 0,
  3389. NULL
  3390. );
  3391. return FALSE;
  3392. }
  3393. exit:
  3394. return(RetVal);
  3395. }
  3396. LPTSTR
  3397. FileNameOnly(
  3398. LPTSTR sz
  3399. )
  3400. /*++
  3401. Routine Description:
  3402. Returns a pointer to the first character after the last backslash in a string
  3403. Arguments:
  3404. sz - full file name.
  3405. Return Value:
  3406. pointer ot file name without path.
  3407. --*/
  3408. {
  3409. LPTSTR lpszFileName = NULL;
  3410. if ( sz )
  3411. {
  3412. lpszFileName = wcsrchr( sz, L'\\' );
  3413. if ( lpszFileName ) {
  3414. lpszFileName++;
  3415. }
  3416. else {
  3417. lpszFileName = sz;
  3418. }
  3419. }
  3420. return lpszFileName;
  3421. }
  3422. BOOL
  3423. DoBatteryAlarm(
  3424. LPTSTR lpszName,
  3425. LPTSTR lpszBoolStr,
  3426. DWORD dwLevel,
  3427. LPTSTR lpszAlarmTextBoolStr,
  3428. LPTSTR lpszAlarmSoundBoolStr,
  3429. LPTSTR lpszAlarmActionStr,
  3430. LPTSTR lpszAlarmForceBoolStr,
  3431. LPTSTR lpszAlarmProgramBoolStr
  3432. )
  3433. /*++
  3434. Routine Description:
  3435. Configures battery alarms
  3436. Arguments:
  3437. lpszName - "Low" or "Critical" on checked builds: ("0", "1", "2", "3")
  3438. lpszBoolStr - "on", or "off"
  3439. dwLevel - alarm level (0-100)
  3440. lpszAlarmTextBoolStr - NULL, "on", or "off".
  3441. lpszAlarmSoundBoolStr - NULL, "on", or "off".
  3442. lpszAlarmActionStr - NULL, "none", "standby", "hibernate", "shutdown"
  3443. lpszAlarmForceBoolStr - NULL, "on", or "off".
  3444. lpszAlarmProgramBoolStr - NULL, "on", or "off".
  3445. Return Value:
  3446. TRUE if successful
  3447. FALSE if failed
  3448. --*/
  3449. {
  3450. BOOL bShowSetting = TRUE;
  3451. BOOL RetVal;
  3452. GLOBAL_POWER_POLICY PowerPolicy;
  3453. DWORD GlobalFlag = 0;
  3454. DWORD uiIDactive;
  3455. DWORD uiDefaultAlert1;
  3456. DWORD uiAlarmIndex;
  3457. PSYSTEM_POWER_LEVEL lpDischargePolicy;
  3458. ITaskScheduler *pISchedAgent = NULL;
  3459. ITask *pITask;
  3460. IPersistFile *pIPersistFile;
  3461. LPTSTR lpszRunProg = NULL;
  3462. SYSTEM_BATTERY_STATE sbsBatteryState;
  3463. HRESULT hr;
  3464. LPTSTR lpszProgramName;
  3465. //
  3466. // now get the current state, set or clear the flags, and then save the
  3467. // changed settings back.
  3468. //
  3469. RetVal = FALSE;
  3470. if (fGetGlobalPwrPolicy(&PowerPolicy)) {
  3471. // parse name
  3472. if (!lstrcmpi(lpszName,GetResString(IDS_CRITICAL))) {
  3473. lstrcpy((LPWSTR) lpszName, GetResString(IDS_CRITICAL));
  3474. uiAlarmIndex = DISCHARGE_POLICY_CRITICAL;
  3475. }
  3476. else if (!lstrcmpi(lpszName,GetResString(IDS_LOW)))
  3477. {
  3478. lstrcpy((LPWSTR) lpszName,GetResString(IDS_LOW));
  3479. uiAlarmIndex = DISCHARGE_POLICY_LOW;
  3480. }
  3481. else
  3482. {
  3483. g_lpszErr = GetResString(IDS_ALARM_INVALID_ALARM);
  3484. RetVal = FALSE;
  3485. goto exit;
  3486. }
  3487. lpDischargePolicy = &PowerPolicy.user.DischargePolicy[uiAlarmIndex];
  3488. // parse activate state
  3489. if (lpszBoolStr) {
  3490. bShowSetting = FALSE;
  3491. if (!lstrcmpi(lpszBoolStr,GetResString(IDS_ON)))
  3492. {
  3493. lpDischargePolicy->Enable = TRUE;
  3494. }
  3495. else if (!lstrcmpi(lpszBoolStr,GetResString(IDS_OFF)))
  3496. {
  3497. lpDischargePolicy->Enable = FALSE;
  3498. }
  3499. else
  3500. {
  3501. g_lpszErr = GetResString(IDS_ALARM_INVALID_ACTIVATE);
  3502. RetVal = FALSE;
  3503. goto exit;
  3504. }
  3505. }
  3506. // Set Level
  3507. if (dwLevel != 0xffffffff) {
  3508. bShowSetting = FALSE;
  3509. if (dwLevel <= 100) {
  3510. // Read DefaultAlert1 from composite battery
  3511. NtPowerInformation (SystemBatteryState, NULL, 0, &sbsBatteryState, sizeof(sbsBatteryState));
  3512. if (sbsBatteryState.MaxCapacity == 0) {
  3513. uiDefaultAlert1 = 0;
  3514. } else {
  3515. uiDefaultAlert1 = (100 * sbsBatteryState.DefaultAlert1)/sbsBatteryState.MaxCapacity;
  3516. }
  3517. if (dwLevel < uiDefaultAlert1) {
  3518. dwLevel = uiDefaultAlert1;
  3519. DISPLAY_MESSAGE1(stderr, g_lpszBuf, GetResString(IDS_ALARM_LEVEL_MINIMUM), dwLevel);
  3520. }
  3521. lpDischargePolicy->BatteryLevel = dwLevel;
  3522. if (PowerPolicy.user.DischargePolicy[DISCHARGE_POLICY_LOW].BatteryLevel <
  3523. PowerPolicy.user.DischargePolicy[DISCHARGE_POLICY_CRITICAL].BatteryLevel) {
  3524. PowerPolicy.user.DischargePolicy[DISCHARGE_POLICY_LOW].BatteryLevel = dwLevel;
  3525. PowerPolicy.user.DischargePolicy[DISCHARGE_POLICY_CRITICAL].BatteryLevel = dwLevel;
  3526. DISPLAY_MESSAGE1(stderr, g_lpszBuf, GetResString(IDS_ALARM_LEVEL_EQUAL), dwLevel);
  3527. }
  3528. } else {
  3529. g_lpszErr = GetResString(IDS_ALARM_INVALID_LEVEL);
  3530. RetVal = FALSE;
  3531. goto exit;
  3532. }
  3533. }
  3534. // parse and set "text" on/off
  3535. if (lpszAlarmTextBoolStr) { // NULL indicates this option wasn't specified
  3536. bShowSetting = FALSE;
  3537. if (!lstrcmpi(lpszAlarmTextBoolStr,GetResString(IDS_ON)))
  3538. {
  3539. lpDischargePolicy->PowerPolicy.EventCode |= POWER_LEVEL_USER_NOTIFY_TEXT;
  3540. }
  3541. else if (!lstrcmpi(lpszAlarmTextBoolStr,GetResString(IDS_OFF)))
  3542. {
  3543. lpDischargePolicy->PowerPolicy.EventCode &= ~POWER_LEVEL_USER_NOTIFY_TEXT;
  3544. }
  3545. else
  3546. {
  3547. g_lpszErr = GetResString(IDS_ALARM_INVALID_TEXT);
  3548. RetVal = FALSE;
  3549. goto exit;
  3550. }
  3551. }
  3552. // parse and set "sound" on/off
  3553. if (lpszAlarmSoundBoolStr) { // NULL indicates this option wasn't specified
  3554. bShowSetting = FALSE;
  3555. if (!lstrcmpi(lpszAlarmSoundBoolStr,GetResString(IDS_ON)))
  3556. {
  3557. lpDischargePolicy->PowerPolicy.EventCode |= POWER_LEVEL_USER_NOTIFY_SOUND;
  3558. }
  3559. else if (!lstrcmpi(lpszAlarmSoundBoolStr,GetResString(IDS_OFF)))
  3560. {
  3561. lpDischargePolicy->PowerPolicy.EventCode &= ~POWER_LEVEL_USER_NOTIFY_SOUND;
  3562. }
  3563. else
  3564. {
  3565. g_lpszErr = GetResString(IDS_ALARM_INVALID_SOUND);
  3566. RetVal = FALSE;
  3567. goto exit;
  3568. }
  3569. }
  3570. // parse and set "action" none/shutdown/hibernate/standby
  3571. if (lpszAlarmActionStr) { // NULL indicates this option wasn't specified
  3572. bShowSetting = FALSE;
  3573. if (!lstrcmpi(lpszAlarmActionStr,GetResString(IDS_NONE)))
  3574. {
  3575. lpDischargePolicy->PowerPolicy.Action = PowerActionNone;
  3576. }
  3577. else if (!lstrcmpi(lpszAlarmActionStr,GetResString(IDS_STANDBY)))
  3578. {
  3579. if (g_bStandbySupported) {
  3580. lpDischargePolicy->PowerPolicy.Action = PowerActionSleep;
  3581. }
  3582. else
  3583. {
  3584. g_lpszErr = GetResString(IDS_ALARM_STANDBY_UNSUPPORTED);
  3585. RetVal = FALSE;
  3586. goto exit;
  3587. }
  3588. }
  3589. else if (!lstrcmpi(lpszAlarmActionStr,GetResString(IDS_HIBERNATE)))
  3590. {
  3591. if (g_bHiberFilePresent) {
  3592. lpDischargePolicy->PowerPolicy.Action = PowerActionHibernate;
  3593. }
  3594. else
  3595. {
  3596. g_lpszErr = GetResString(IDS_ALARM_HIBERNATE_DISABLED);
  3597. RetVal = FALSE;
  3598. goto exit;
  3599. }
  3600. }
  3601. else if (!lstrcmpi(lpszAlarmActionStr,GetResString(IDS_SHUTDOWN)))
  3602. {
  3603. lpDischargePolicy->PowerPolicy.Action = PowerActionShutdownOff;
  3604. }
  3605. else
  3606. {
  3607. g_lpszErr = GetResString(IDS_ALARM_INVALID_ACTION);
  3608. RetVal = FALSE;
  3609. goto exit;
  3610. }
  3611. }
  3612. // parse and set "forceaction" on/off
  3613. if (lpszAlarmForceBoolStr) { // NULL indicates this option wasn't specified
  3614. bShowSetting = FALSE;
  3615. if (!lstrcmpi(lpszAlarmForceBoolStr,GetResString(IDS_ON)))
  3616. {
  3617. lpDischargePolicy->PowerPolicy.Flags |= POWER_ACTION_OVERRIDE_APPS;
  3618. }
  3619. else if (!lstrcmpi(lpszAlarmForceBoolStr,GetResString(IDS_OFF)))
  3620. {
  3621. if (uiAlarmIndex == DISCHARGE_POLICY_CRITICAL) {
  3622. DISPLAY_MESSAGE(stderr, GetResString(IDS_ALARM_FORCE_CRITICAL));
  3623. }
  3624. lpDischargePolicy->PowerPolicy.Flags &= ~POWER_ACTION_OVERRIDE_APPS;
  3625. }
  3626. else
  3627. {
  3628. g_lpszErr = GetResString(IDS_ALARM_INVALID_FORCE);
  3629. RetVal = FALSE;
  3630. goto exit;
  3631. }
  3632. }
  3633. // parse and set "program" on/off
  3634. if (lpszAlarmProgramBoolStr) { // NULL indicates this option wasn't specified
  3635. bShowSetting = FALSE;
  3636. if (!lstrcmpi(lpszAlarmProgramBoolStr,GetResString(IDS_ON)))
  3637. {
  3638. hr = CoInitialize(NULL);
  3639. if (SUCCEEDED(hr)) {
  3640. hr = CoCreateInstance( CLSID_CTaskScheduler, NULL, CLSCTX_INPROC_SERVER,
  3641. IID_ITaskScheduler,(LPVOID*) &pISchedAgent );
  3642. if (SUCCEEDED(hr)) {
  3643. hr = pISchedAgent->Activate(g_szAlarmTaskName[uiAlarmIndex],
  3644. IID_ITask,
  3645. (IUnknown **) &pITask);
  3646. if (SUCCEEDED(hr)) {
  3647. //
  3648. // It already exists. No work needed.
  3649. //
  3650. pITask->Release();
  3651. }
  3652. else if (HRESULT_CODE (hr) == ERROR_FILE_NOT_FOUND){
  3653. hr = pISchedAgent->NewWorkItem(
  3654. g_szAlarmTaskName[uiAlarmIndex],
  3655. CLSID_CTask,
  3656. IID_ITask,
  3657. (IUnknown **) &pITask);
  3658. if (SUCCEEDED(hr)) {
  3659. hr = pITask->QueryInterface(IID_IPersistFile,
  3660. (void **)&pIPersistFile);
  3661. if (SUCCEEDED(hr)) {
  3662. hr = pIPersistFile->Save(NULL, TRUE);
  3663. if (SUCCEEDED(hr)) {
  3664. // No work to do. The task has been created and saved and can be edited using schtasks.exe
  3665. //pITask->lpVtbl->EditWorkItem(pITask, hWnd, 0);
  3666. }
  3667. else {
  3668. #if DBG
  3669. DISPLAY_MESSAGE1(stdout, g_lpszBuf, _T("DoBatteryAlarm: Save failed hr = %08x\n"), hr);
  3670. #endif
  3671. }
  3672. pIPersistFile->Release();
  3673. }
  3674. else {
  3675. #if DBG
  3676. DISPLAY_MESSAGE1(stdout, g_lpszBuf, _T("DoBatteryAlarm: QueryInterface for IPersistFile hr = %08x\n"), hr);
  3677. #endif
  3678. }
  3679. pITask->Release();
  3680. }
  3681. else {
  3682. #if DBG
  3683. DISPLAY_MESSAGE1(stdout, g_lpszBuf, _T("DoBatteryAlarm: NewWorkItem returned hr = %08x\n"), hr);
  3684. #endif
  3685. }
  3686. }
  3687. else {
  3688. #if DBG
  3689. DISPLAY_MESSAGE1(stdout, g_lpszBuf, _T("DoBatteryAlarm: Activate returned hr = %08x\n"), hr);
  3690. #endif
  3691. }
  3692. pISchedAgent->Release();
  3693. }
  3694. else {
  3695. #if DBG
  3696. DISPLAY_MESSAGE1(stdout, g_lpszBuf, _T("DoBatteryAlarm: CoCreateInstance returned hr = %08x\n"), hr);
  3697. #endif
  3698. }
  3699. CoUninitialize();
  3700. } else {
  3701. #if DBG
  3702. DISPLAY_MESSAGE1(stdout, g_lpszBuf, _T("DoBatteryAlarm: CoInitialize returned hr = %08x\n"), hr);
  3703. #endif
  3704. }
  3705. if (SUCCEEDED(hr)) {
  3706. DISPLAY_MESSAGE1(stdout, g_lpszBuf, _T("\"%s\""), g_szAlarmTaskName[uiAlarmIndex]);
  3707. } else {
  3708. DISPLAY_MESSAGE1(
  3709. stdout,
  3710. g_lpszBuf,
  3711. GetResString(IDS_ALARM_PROGRAM_FAILED),
  3712. g_szAlarmTaskName[uiAlarmIndex]);
  3713. }
  3714. lpDischargePolicy->PowerPolicy.EventCode |= POWER_LEVEL_USER_NOTIFY_EXEC;
  3715. }
  3716. else if (!lstrcmpi(lpszAlarmProgramBoolStr,GetResString(IDS_OFF)))
  3717. {
  3718. lpDischargePolicy->PowerPolicy.EventCode &= ~POWER_LEVEL_USER_NOTIFY_EXEC;
  3719. }
  3720. else
  3721. {
  3722. g_lpszErr = GetResString(IDS_ALARM_INVALID_PROGRAM);
  3723. RetVal = FALSE;
  3724. goto exit;
  3725. }
  3726. }
  3727. if (bShowSetting) {
  3728. DISPLAY_MESSAGE(stdout, GetResString(IDS_ALARM_HEADER1));
  3729. DISPLAY_MESSAGE(stdout, GetResString(IDS_ALARM_HEADER2));
  3730. // Which alarm
  3731. DISPLAY_MESSAGE1(
  3732. stdout,
  3733. g_lpszBuf,
  3734. GetResString(IDS_ALARM_NAME),
  3735. lpszName
  3736. );
  3737. // Active
  3738. DISPLAY_MESSAGE1(
  3739. stdout,
  3740. g_lpszBuf,
  3741. GetResString(IDS_ALARM_ACTIVE),
  3742. GetResString(lpDischargePolicy->Enable ? IDS_ON : IDS_OFF)
  3743. );
  3744. // Level
  3745. DISPLAY_MESSAGE1(
  3746. stdout,
  3747. g_lpszBuf,
  3748. GetResString(IDS_ALARM_LEVEL),
  3749. lpDischargePolicy->BatteryLevel
  3750. );
  3751. // Text
  3752. DISPLAY_MESSAGE1(
  3753. stdout,
  3754. g_lpszBuf,
  3755. GetResString(IDS_ALARM_TEXT),
  3756. GetResString((lpDischargePolicy->PowerPolicy.EventCode &
  3757. POWER_LEVEL_USER_NOTIFY_TEXT) ? IDS_ON : IDS_OFF)
  3758. );
  3759. // Sound
  3760. DISPLAY_MESSAGE1(
  3761. stdout,
  3762. g_lpszBuf,
  3763. GetResString(IDS_ALARM_SOUND),
  3764. GetResString((lpDischargePolicy->PowerPolicy.EventCode &
  3765. POWER_LEVEL_USER_NOTIFY_SOUND) ? IDS_ON : IDS_OFF)
  3766. );
  3767. // Action
  3768. DISPLAY_MESSAGE1(
  3769. stdout,
  3770. g_lpszBuf,
  3771. GetResString(IDS_ALARM_ACTION),
  3772. GetResString((lpDischargePolicy->PowerPolicy.Action == PowerActionNone) ? IDS_NONE :
  3773. (lpDischargePolicy->PowerPolicy.Action == PowerActionSleep) ? IDS_STANDBY :
  3774. (lpDischargePolicy->PowerPolicy.Action == PowerActionHibernate) ? IDS_HIBERNATE :
  3775. (lpDischargePolicy->PowerPolicy.Action == PowerActionShutdownOff) ? IDS_SHUTDOWN : IDS_INVALID
  3776. )
  3777. );
  3778. // Force
  3779. DISPLAY_MESSAGE1(
  3780. stdout,
  3781. g_lpszBuf,
  3782. GetResString(IDS_ALARM_FORCE),
  3783. GetResString((lpDischargePolicy->PowerPolicy.Flags &
  3784. POWER_ACTION_OVERRIDE_APPS) ? IDS_ON : IDS_OFF)
  3785. );
  3786. // Program
  3787. DISPLAY_MESSAGE1(
  3788. stdout,
  3789. g_lpszBuf,
  3790. GetResString(IDS_ALARM_PROGRAM),
  3791. GetResString((lpDischargePolicy->PowerPolicy.EventCode &
  3792. POWER_LEVEL_USER_NOTIFY_EXEC) ? IDS_ON : IDS_OFF)
  3793. );
  3794. hr = CoInitialize(NULL);
  3795. if (SUCCEEDED(hr)) {
  3796. hr = CoCreateInstance( CLSID_CTaskScheduler, NULL, CLSCTX_INPROC_SERVER,
  3797. IID_ITaskScheduler,(LPVOID*) &pISchedAgent );
  3798. if (SUCCEEDED(hr)) {
  3799. hr = pISchedAgent->Activate(g_szAlarmTaskName[uiAlarmIndex],
  3800. IID_ITask,
  3801. (IUnknown **) &pITask);
  3802. if (SUCCEEDED(hr)) {
  3803. pITask->GetApplicationName(&lpszRunProg);
  3804. pITask->Release();
  3805. } else {
  3806. #if DBG
  3807. DISPLAY_MESSAGE1(stdout, g_lpszBuf, _T("DoBatteryAlarm: Activate returned hr = %08x\n"), hr);
  3808. #endif
  3809. }
  3810. pISchedAgent->Release();
  3811. }
  3812. else {
  3813. #if DBG
  3814. DISPLAY_MESSAGE1(stdout, g_lpszBuf, _T("DoBatteryAlarm: CoCreateInstance returned hr = %08x\n"), hr);
  3815. #endif
  3816. }
  3817. } else {
  3818. #if DBG
  3819. DISPLAY_MESSAGE1(stdout, g_lpszBuf, _T("DoBatteryAlarm: CoInitialize returned hr = %08x\n"), hr);
  3820. #endif
  3821. }
  3822. DISPLAY_MESSAGE1(
  3823. stdout,
  3824. g_lpszBuf,
  3825. GetResString(IDS_ALARM_PROGRAM_NAME),
  3826. lpszRunProg ? FileNameOnly(lpszRunProg) : GetResString(IDS_NONE));
  3827. if (lpszRunProg) {
  3828. CoTaskMemFree (lpszRunProg);
  3829. lpszRunProg = NULL;
  3830. }
  3831. CoUninitialize();
  3832. RetVal = TRUE;
  3833. goto exit;
  3834. }
  3835. if (fWriteGlobalPwrPolicy(&PowerPolicy) &&
  3836. fGetActivePwrScheme((PUINT)&uiIDactive) &&
  3837. fSetActivePwrScheme(uiIDactive,&PowerPolicy,NULL)){
  3838. RetVal = TRUE;
  3839. }
  3840. }
  3841. RetVal = TRUE;
  3842. //
  3843. // save off the error if we had issues
  3844. //
  3845. if (!RetVal) {
  3846. FormatMessage(
  3847. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  3848. FORMAT_MESSAGE_FROM_SYSTEM |
  3849. FORMAT_MESSAGE_IGNORE_INSERTS,
  3850. NULL,
  3851. GetLastError(),
  3852. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  3853. (LPTSTR)&g_lpszErr2,
  3854. 0,
  3855. NULL
  3856. );
  3857. return FALSE;
  3858. }
  3859. exit:
  3860. return(RetVal);
  3861. }
  3862. BOOL
  3863. DoUsage()
  3864. /*++
  3865. Routine Description:
  3866. Displays usage information
  3867. Arguments:
  3868. none
  3869. Return Value:
  3870. TRUE if successful
  3871. FALSE if failed
  3872. --*/
  3873. {
  3874. ULONG ulIdx;
  3875. ULONG ulOrderIndex = 0;
  3876. for(ulIdx=IDS_USAGE_START;ulIdx<=IDS_USAGE_END;ulIdx++)
  3877. {
  3878. DISPLAY_MESSAGE(stdout, GetResString(ulIdx));
  3879. if (ulIdx == gUsageOrder [ulOrderIndex].InsertAfter) {
  3880. for (ulIdx = gUsageOrder [ulOrderIndex].FirstResource;
  3881. ulIdx <= gUsageOrder [ulOrderIndex].LastResource;
  3882. ulIdx++) {
  3883. DISPLAY_MESSAGE(stdout, GetResString(ulIdx));
  3884. }
  3885. ulIdx = gUsageOrder [ulOrderIndex].InsertAfter;
  3886. ulOrderIndex++;
  3887. }
  3888. }
  3889. return TRUE;
  3890. }
  3891. VOID
  3892. SyncRegPPM(VOID)
  3893. /*++
  3894. Routine Description:
  3895. Call down to the PPM to get the current power policies and write them
  3896. to the registry. This is done in case the PPM is out of sync with the
  3897. PowerCfg registry settings. Requested by JVert.
  3898. Arguments:
  3899. Return Value:
  3900. --*/
  3901. {
  3902. GLOBAL_POWER_POLICY gpp;
  3903. POWER_POLICY pp;
  3904. UINT uiID, uiFlags = 0;
  3905. if (fGetGlobalPwrPolicy(&gpp)) {
  3906. uiFlags = gpp.user.GlobalFlags;
  3907. }
  3908. if (fGetActivePwrScheme(&uiID)) {
  3909. // Get the current PPM settings.
  3910. if (fGetCurrentPowerPolicies(&gpp, &pp)) {
  3911. fSetActivePwrScheme(uiID, &gpp, &pp);
  3912. }
  3913. }
  3914. gpp.user.GlobalFlags |= uiFlags;
  3915. }