获取系统所有电源计划策略名称及当前电源计划
2023-07-31
22
0
Windows电源管理相关编程
使用C语言获取系统中所有的电源计划策回略。一般会有多个,但只会有一个当前有效。可通过设置来切换选择那个电源策回略。
#include <Windows.h>
#include <PowrProf.h>
#include <stdio.h>
#include <tchar.h>
#include <locale.h>
#pragma comment(lib, "PowrProf.lib")
int main()
{
UINT index = 0;
DWORD bufferSize = sizeof(GUID);
GUID buffer;
setlocale(LC_ALL, "chs");
while (PowerEnumerate(NULL, NULL, NULL, ACCESS_SCHEME, index, (UCHAR*)&buffer, &bufferSize) == ERROR_SUCCESS)
{
TCHAR Name[512] = { 0 };
DWORD len = sizeof(Name);
if (PowerReadFriendlyName(NULL, &buffer, NULL, NULL, (PUCHAR)Name, &len) == ERROR_SUCCESS)
{
_tprintf(TEXT("%s \t"), Name);
}
printf("Power Plan GUID: %08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X \n",
buffer.Data1, buffer.Data2, buffer.Data3,
buffer.Data4[0], buffer.Data4[1], buffer.Data4[2], buffer.Data4[3],
buffer.Data4[4], buffer.Data4[5], buffer.Data4[6], buffer.Data4[7]);
index++;
bufferSize = sizeof(GUID);
}
printf("------------------\n");
GUID* ActivePolicyGuid = NULL;
DWORD status = PowerGetActiveScheme(
NULL,
&ActivePolicyGuid
);
if (status == ERROR_SUCCESS)
{
printf("Active Plan GUID: %08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X\n",
ActivePolicyGuid->Data1, ActivePolicyGuid->Data2, ActivePolicyGuid->Data3,
ActivePolicyGuid->Data4[0], ActivePolicyGuid->Data4[1], ActivePolicyGuid->Data4[2], ActivePolicyGuid->Data4[3],
ActivePolicyGuid->Data4[4], ActivePolicyGuid->Data4[5], ActivePolicyGuid->Data4[6], ActivePolicyGuid->Data4[7]);
}
if (ActivePolicyGuid != NULL)
{
LocalFree(ActivePolicyGuid);
}
system("pause");
return 0;
}
在本人的笔记本中获取其所有电源策略执行如下:
平衡 Power Plan GUID: 381B4222-F694-41F0-9685-FF5BB260DF2E
Dell Power Plan GUID: 49EF8FC0-BB7F-488E-B6A0-F1FC77EC649B
------------------
Active Plan GUID: 49EF8FC0-BB7F-488E-B6A0-F1FC77EC649B
在本人的另一台测试机中如下:
Balanced Power Plan GUID: 381B4222-F694-41F0-9685-FF5BB260DF2E
High performance Power Plan GUID: 8C5E7FDA-E8BF-4A96-9A85-A6E23A8C635C
Power saver Power Plan GUID: A1841308-3541-4FAB-BC81-F71556F20B4A
------------------
Active Plan GUID: 8C5E7FDA-E8BF-4A96-9A85-A6E23A8C635C