最近由于项目的需要一直在搞WIN7的电源管理方案,本来想在网上查点类似的代码,可是搜了许久,一点有用的知识也没有,索性自己来研究了,MSDN上面说的到挺详细,不过没有示例,一些参数总是搞不明白,而且WIN7下的电源管理方案的API函数和XP下有了很大的改变,写这篇文章的目的:一是帮助向我这样苦苦搜索类似的资料的同行人,二是做一个笔记避免将来自己忘掉,做一个备份.
要提起WIN7的电源管理方案,首先要说的就是GUID,在WIN7下 每一个设备都给分配了一个GUID,电源管理方案,同样如此,这里主要用到了三种GUID:
1.
SchemeGuid 这个是用来标示电源管理方案的,就是在WIN7下我们看到 什么平衡模式,节能模式,高性能模式.
const GUID FAR GUID_POWER_SAVER ={ 0xa1841308, 0x3541, 0x4fab, { 0xbc, 0x81, 0xf7, 0x15, 0x56, 0xf2, 0x0b, 0x4a } }; //节能
//a1841308-3541-4fab-bc81-f71556f20b4a
const GUID FAR GUID_BALANCED ={ 0x381b4222, 0xf694, 0x41f0, { 0x96, 0x85, 0xff, 0x5b, 0xb2, 0x60, 0xdf, 0x2e } }; //平横
//381b4222-f694-41f0-9685-ff5bb260df2e
const GUID FAR GUID_HIGH_PERFORMANCE ={ 0x8c5e7fda, 0xe8bf, 0x4a96, { 0x9a, 0x85, 0xa6, 0xe2, 0x3a, 0x8c, 0x63, 0x5c } }; //高性能
这是WIN7下的三种电源管理方案的三种模式的GUID,这些东西是不变的.
2.Subgroup GUID 也就是API函数中SubGroupOfPowerSettingsGuid 这个参数 是每个电源管理方案下每个小组的的GUID,这个参数非常重要,控制每组的参数 全指望它了.
Value | Meaning |
- NO_SUBGROUP_GUID
- fea3413e-7e05-4911-9a71-700331f1c294
Settings in this subgroup will be part of the default power scheme.
- GUID_DISK_SUBGROUP
- 0012ee47-9041-4b5d-9b77-535fba8b1442
Settings in this subgroup control power management configuration of the system's hard disk drives.
- GUID_SYSTEM_BUTTON_SUBGROUP
- 4f971e89-eebd-4455-a8de-9e59040e7347
Settings in this subgroup control configuration of the system power buttons.
- GUID_PROCESSOR_SETTINGS_SUBGROUP
- 54533251-82be-4824-96c1-47b60b740d00
Settings in this subgroup control configuration of processor power management features.
- GUID_VIDEO_SUBGROUP
- 7516b95f-f776-4464-8c53-06167f40cc99
Settings in this subgroup control configuration of the video power management features.
- GUID_BATTERY_SUBGROUP
- e73a048d-bf27-4f12-9731-8b2076e8891f
Settings in this subgroup control battery alarm trip points and actions.
- GUID_SLEEP_SUBGROUP
- 238C9FA8-0AAD-41ED-83F4-97BE242C8F20
Settings in this subgroup control system sleep settings.
- GUID_PCIEXPRESS_SETTINGS_SUBGROUP
- 501a4d13-42af-4429-9fd1-a8218c268e20
Settings in this subgroup control PCI Express settings.
上面这些是所有的Subgroup GUID.
3.还有最后一个参数就是Power Setting GUID,刚开始搞时也就是这个参数困忧了我很长时间,一直不知道是做什么用的,后来才知道,他是Subgroup GUID 的子项,也就是组选项下面的小选项.这个里面涉及的参数比较多,API没有给出详细的GUID 不过我都导出来了,供大家使用方便和自己查询.
Current DC Power Setting Index: 0x00000258
Subgroup GUID: 0d7dbae2-4294-402a-ba8e-26777e8488cd (Desktop background setti
ngs)
Power Setting GUID: 309dce9b-bef4-4119-9921-a851fb12f0f4 (Slide show)
Possible Setting Index: 000
Possible Setting Friendly Name: Available
Possible Setting Index: 001
Possible Setting Friendly Name: Paused
Current AC Power Setting Index: 0x00000000
Current DC Power Setting Index: 0x00000001
Subgroup GUID: 19cbb8fa-5279-450e-9fac-8a3d5fedd0c1 (Wireless Adapter Setting
s)
Power Setting GUID: 12bbebe6-58d6-4636-95bb-3217ef867c1a (Power Saving Mode
)
Possible Setting Index: 000
Possible Setting Friendly Name: Maximum Performance
Possible Setting Index: 001
Possible Setting Friendly Name: Low Power Saving
Possible Setting Index: 002
Possible Setting Friendly Name: Medium Power Saving
Possible Setting Index: 003
Possible Setting Friendly Name: Maximum Power Saving
Current AC Power Setting Index: 0x00000000
Current DC Power Setting Index: 0x00000002
Subgroup GUID: 238c9fa8-0aad-41ed-83f4-97be242c8f20 (Sleep)
Power Setting GUID: 29f6c1db-86da-48c5-9fdb-f2b67b1f44da (Sleep after)
Minimum Possible Setting: 0x00000000
Maximum Possible Setting: 0xffffffff
Possible Settings increment: 0x00000001
Possible Settings units: Seconds
Current AC Power Setting Index: 0x00000708
Current DC Power Setting Index: 0x00000384
Power Setting GUID: 94ac6d29-73ce-41a6-809f-6363ba21b47e (Allow hybrid slee
p)
Possible Setting Index: 000
Possible Setting Friendly Name: Off
Possible Setting Index: 001
Possible Setting Friendly Name: On
Current AC Power Setting Index: 0x00000000
Current DC Power Setting Index: 0x00000000
Power Setting GUID: 9d7815a6-7ee4-497e-8888-515a05f02364 (Hibernate after)
Minimum Possible Setting: 0x00000000
Maximum Possible Setting: 0xffffffff
Possible Settings increment: 0x00000001
Possible Settings units: Seconds
Current AC Power Setting Index: 0x00005460
Current DC Power Setting Index: 0x00005460
Power Setting GUID: bd3b718a-0680-4d9d-8ab2-e1d2b4ac806d (Allow wake timers
)
Possible Setting Index: 000
Possible Setting Friendly Name: Disable
Possible Setting Index: 001
Possible Setting Friendly Name: Enable
Current AC Power Setting Index: 0x00000000
Current DC Power Setting Index: 0x00000000
Subgroup GUID: 2a737441-1930-4402-8d77-b2bebba308a3 (USB settings)
Power Setting GUID: 48e6b7a6-50f5-4782-a5d4-53bb8f07e226 (USB selective sus
pend setting)
Possible Setting Index: 000
Possible Setting Friendly Name: Disabled
Possible Setting Index: 001
Possible Setting Friendly Name: Enabled
Current AC Power Setting Index: 0x00000001
Current DC Power Setting Index: 0x00000001
Subgroup GUID: 4f971e89-eebd-4455-a8de-9e59040e7347 (Power buttons and lid)
Power Setting GUID: 5ca83367-6e45-459f-a27b-476b1d01c936 (Lid close action)
Possible Setting Index: 000
Possible Setting Friendly Name: Do nothing
Possible Setting Index: 001
Possible Setting Friendly Name: Sleep
Possible Setting Index: 002
Possible Setting Friendly Name: Hibernate
Possible Setting Index: 003
Possible Setting Friendly Name: Shut down
Current AC Power Setting Index: 0x00000001
Current DC Power Setting Index: 0x00000001
Power Setting GUID: 7648efa3-dd9c-4e3e-b566-50f929386280 (Power button acti
on)
Possible Setting Index: 000
Possible Setting Friendly Name: Do nothing
Possible Setting Index: 001
Possible Setting Friendly Name: Sleep
Possible Setting Index: 002
Possible Setting Friendly Name: Hibernate
Possible Setting Index: 003
Possible Setting Friendly Name: Shut down
Current AC Power Setting Index: 0x00000001
Current DC Power Setting Index: 0x00000001
Power Setting GUID: 96996bc0-ad50-47ec-923b-6f41874dd9eb (Sleep button acti
on)
Possible Setting Index: 000
Possible Setting Friendly Name: Do nothing
Possible Setting Index: 001
Possible Setting Friendly Name: Sleep
Possible Setting Index: 002
Possible Setting Friendly Name: Hibernate
Possible Setting Index: 003
Possible Setting Friendly Name: Shut down
Current AC Power Setting Index: 0x00000001
Current DC Power Setting Index: 0x00000001
Power Setting GUID: a7066653-8d6c-40a8-910e-a1f54b84c7e5 (Start menu power
button)
Possible Setting Index: 000
Possible Setting Friendly Name: Sleep
Possible Setting Index: 001
Possible Setting Friendly Name: Hibernate
Possible Setting Index: 002
Possible Setting Friendly Name: Shut down
Current AC Power Setting Index: 0x00000000
Current DC Power Setting Index: 0x00000000
Subgroup GUID: 501a4d13-42af-4429-9fd1-a8218c268e20 (PCI Express)
Power Setting GUID: ee12f906-d277-404b-b6da-e5fa1a576df5 (Link State Power
Management)
Possible Setting Index: 000
Possible Setting Friendly Name: Off
Possible Setting Index: 001
Possible Setting Friendly Name: Moderate power savings
Possible Setting Index: 002
Possible Setting Friendly Name: Maximum power savings
Current AC Power Setting Index: 0x00000001
Current DC Power Setting Index: 0x00000002
Subgroup GUID: 54533251-82be-4824-96c1-47b60b740d00 (Processor power manageme
nt)
Power Setting GUID: 893dee8e-2bef-41e0-89c6-b55d0929964c (Minimum processor
state)
Minimum Possible Setting: 0x00000000
Maximum Possible Setting: 0x00000064
Possible Settings increment: 0x00000001
Possible Settings units: %
Current AC Power Setting Index: 0x00000005
Current DC Power Setting Index: 0x00000005
Power Setting GUID: 94d3a615-a899-4ac5-ae2b-e4d8f634367f (System cooling po
licy)
Possible Setting Index: 000
Possible Setting Friendly Name: Passive
Possible Setting Index: 001
Possible Setting Friendly Name: Active
Current AC Power Setting Index: 0x00000001
Current DC Power Setting Index: 0x00000000
Power Setting GUID: bc5038f7-23e0-4960-96da-33abaf5935ec (Maximum processor
state)
Minimum Possible Setting: 0x00000000
Maximum Possible Setting: 0x00000064
Possible Settings increment: 0x00000001
Possible Settings units: %
Current AC Power Setting Index: 0x00000064
Current DC Power Setting Index: 0x00000064
Subgroup GUID: 7516b95f-f776-4464-8c53-06167f40cc99 (Display)
Power Setting GUID: 17aaa29b-8b43-4b94-aafe-35f64daaf1ee (Dim display after
)
Minimum Possible Setting: 0x00000000
Maximum Possible Setting: 0xffffffff
Possible Settings increment: 0x00000001
Possible Settings units: Seconds
Current AC Power Setting Index: 0x0000012c
Current DC Power Setting Index: 0x00000078
Power Setting GUID: 3c0bc021-c8a8-4e07-a973-6b14cbcb2b7e (Turn off display
after)
Minimum Possible Setting: 0x00000000
Maximum Possible Setting: 0xffffffff
Possible Settings increment: 0x00000001
Possible Settings units: Seconds
Current AC Power Setting Index: 0x00000258
Current DC Power Setting Index: 0x0000012c
Power Setting GUID: aded5e82-b909-4619-9949-f5d71dac0bcb (Display brightnes
s)
Minimum Possible Setting: 0x00000000
Maximum Possible Setting: 0x00000064
Possible Settings increment: 0x00000001
Possible Settings units: %
Current AC Power Setting Index: 0x00000039
Current DC Power Setting Index: 0x00000000
Power Setting GUID: f1fbfde2-a960-4165-9f88-50667911ce96 (Dimmed display br
ightness)
Minimum Possible Setting: 0x00000000
Maximum Possible Setting: 0x00000064
Possible Settings increment: 0x00000001
Possible Settings units: %
Current AC Power Setting Index: 0x0000001e
Current DC Power Setting Index: 0x0000001e
Subgroup GUID: 9596fb26-9850-41fd-ac3e-f7c3c00afd4b (Multimedia settings)
Power Setting GUID: 03680956-93bc-4294-bba6-4e0f09bb717f (When sharing medi
a)
Possible Setting Index: 000
Possible Setting Friendly Name: Allow the computer to sleep
Possible Setting Index: 001
Possible Setting Friendly Name: Prevent idling to sleep
Possible Setting Index: 002
Possible Setting Friendly Name: Allow the computer to enter Away Mode
Current AC Power Setting Index: 0x00000001
Current DC Power Setting Index: 0x00000000
Power Setting GUID: 34c7b99f-9a6d-4b3c-8dc7-b6693b78cef4 (When playing vide
o)
Possible Setting Index: 000
Possible Setting Friendly Name: Optimize video quality
Possible Setting Index: 001
Possible Setting Friendly Name: Balanced
Possible Setting Index: 002
Possible Setting Friendly Name: Optimize power savings
Current AC Power Setting Index: 0x00000000
Current DC Power Setting Index: 0x00000001
Subgroup GUID: e73a048d-bf27-4f12-9731-8b2076e8891f (Battery)
Power Setting GUID: 637ea02f-bbcb-4015-8e2c-a1c7b9c0b546 (Critical battery
action)
Possible Setting Index: 000
Possible Setting Friendly Name: Do nothing
Possible Setting Index: 001
Possible Setting Friendly Name: Sleep
Possible Setting Index: 002
Possible Setting Friendly Name: Hibernate
Possible Setting Index: 003
Possible Setting Friendly Name: Shut down
Current AC Power Setting Index: 0x00000000
Current DC Power Setting Index: 0x00000002
Power Setting GUID: 8183ba9a-e910-48da-8769-14ae6dc1170a (Low battery level
)
Minimum Possible Setting: 0x00000000
Maximum Possible Setting: 0x00000064
Possible Settings increment: 0x00000001
Possible Settings units: %
Current AC Power Setting Index: 0x0000000a
Current DC Power Setting Index: 0x0000000a
Power Setting GUID: 9a66d8d7-4ff7-4ef9-b5a2-5a326ca2a469 (Critical battery
level)
Minimum Possible Setting: 0x00000000
Maximum Possible Setting: 0x00000064
Possible Settings increment: 0x00000001
Possible Settings units: %
Current AC Power Setting Index: 0x00000005
Current DC Power Setting Index: 0x00000005
Power Setting GUID: bcded951-187b-4d05-bccc-f7e51960c258 (Low battery notif
ication)
Possible Setting Index: 000
Possible Setting Friendly Name: Off
Possible Setting Index: 001
Possible Setting Friendly Name: On
Current AC Power Setting Index: 0x00000001
Current DC Power Setting Index: 0x00000001
Power Setting GUID: d8742dcb-3e6a-4b3c-b3fe-374623cdcf06 (Low battery actio
n)
Possible Setting Index: 000
Possible Setting Friendly Name: Do nothing
Possible Setting Index: 001
Possible Setting Friendly Name: Sleep
Possible Setting Index: 002
Possible Setting Friendly Name: Hibernate
Possible Setting Index: 003
Possible Setting Friendly Name: Shut down
Current AC Power Setting Index: 0x00000000
Current DC Power Setting Index: 0x00000000
Power Setting GUID: f3c5027d-cd16-4930-aa6b-90db844a8f00 (Reserve battery l
evel)
Minimum Possible Setting: 0x00000000
Maximum Possible Setting: 0x00000064
Possible Settings increment: 0x00000001
Possible Settings units: %
Current AC Power Setting Index: 0x00000007
Current DC Power Setting Index: 0x00000007
Subgroup GUID: f693fb01-e858-4f00-b20f-f30e12ac06d6 (ATI Graphics Power Setti
ngs)
Power Setting GUID: 191f65b5-d45c-4a4f-8aae-1ab8bfd980e6 (ATI Powerplay Set
tings)
Possible Setting Index: 000
Possible Setting Friendly Name: Maximize Battery Life
Possible Setting Index: 001
Possible Setting Friendly Name: Maximize Performance
Current AC Power Setting Index: 0x00000001
Current DC Power Setting Index: 0x00000000
上面把参数介绍个差不多了,下面开始创建电源管理方案,WIN7下创建电源管理方案和XP下不一样,需要以下几步:
1.首先复制一个现有的电源管理方案.要用的API函数是:
DWORD WINAPI PowerDuplicateScheme(
__in_opt HKEY RootPowerKey,
__in const GUID *SourceSchemeGuid,
__in GUID **DestinationSchemeGuid
);
下面是示例代码:
WCHAR displayBuffer[_MAX_PATH]=L"超级节能";
DWORD displayBufferSize =_MAX_PATH; //sizeof(displayBuffer);
GUID *GUID_HIGH_POWERSAVE=NULL; //创建新的电源管理方案声成的的GUID
if(ERROR_SUCCESS== PowerDuplicateScheme(NULL,&GUID_POWER_SAVER,&GUID_HIGH_POWERSAVE))//GUID_POWER_SAVER是复制节能模式的方案
{
MessageBox("DuplicateScheme ok");
}
2.复制电源管理方案后,下面是把它定义成我们自己的,首先对它进行改名和自定义描述,用的API是:
DWORD WINAPI PowerWriteFriendlyName(
__in_opt HKEY RootPowerKey,
__in const GUID *SchemeGuid,
__in_opt const GUID *SubGroupOfPowerSettingsGuid,
__in_opt const GUID *PowerSettingGuid,
__in UCHAR *Buffer,
__in DWORD BufferSize
);
DWORD WINAPI PowerWriteDescription(
__in_opt HKEY RootPowerKey,
__in const GUID *SchemeGuid,
__in_opt const GUID *SubGroupOfPowerSettingsGuid,
__in_opt const GUID *PowerSettingGuid,
__in UCHAR *Buffer,
__in DWORD BufferSize
);
示例代码为:
if(ERROR_SUCCESS==
PowerWriteDescription(
NULL,
GUID_HIGH_POWERSAVE,
NULL,//大家可以根据这里的参数来修改给那个参数描述 要是下面这两项都为空,默认是电源管理方案的描述.
NULL,
(UCHAR*)displayBuffer,//这里大家一定要注意,WIN7只认宽字符,要是定义时一定要定义成WCHAR 否则会乱码.
displayBufferSize
))
{
MessageBox("PowerWriteDescription");
}
else
{
CString outString;
DWORD mm=GetLastError();
outString.Format("%d",mm);
}
if(ERROR_SUCCESS==
PowerWriteFriendlyName(
NULL,
GUID_HIGH_POWERSAVE,
NULL,//
NULL,
(UCHAR*)displayBuffer,
displayBufferSize
))
{
MessageBox("PowerWriteFriendlyName");
}
else
{
CString outString;
DWORD mm=GetLastError();
outString.Format("%d",mm);
}
4.这里就是修改我们自己需要的参数设置了:
这里用的函数比较多,主要是分成AC,DC模式的设置,大家根据参数的不同就可以设置自己需要的不同参数了,这里简单介绍一个函数,用的API函数为
DWORD WINAPI PowerWriteDCValueIndex(
__in_opt HKEY RootPowerKey,
__in const GUID *SchemeGuid,
__in_opt const GUID *SubGroupOfPowerSettingsGuid,
__in_opt const GUID *PowerSettingGuid,
__in DWORD DcValueIndex
);
示例代码是在新建电源管理方案下把处理器降频到%50
GUID FAR MySubGroupOfPowerSettingsGuid={0xbc5038f7, 0x23e0, 0x4960, {0x96, 0xda, 0x33, 0xab, 0xaf, 0x59, 0x35, 0xec} };
if(ERROR_SUCCESS==
PowerWriteDCValueIndex(
NULL,
GUID_HIGH_POWERSAVE,
&GUID_PROCESSOR_SETTINGS_SUBGROUP,
&MySubGroupOfPowerSettingsGuid,// &NO_SUBGROUP_GUID,
0x3c
))
{
MessageBox("PowerWriteValueMax");
}
else
{
CString outString;
DWORD mm=GetLastError();
outString.Format("%d",mm);
}
好了,这样一个自己需要的电源管理方案用代码就实现出来了,这只是简单介绍了一下思路和方法,有不少的笔记本节能软件 就是基于这
个原理实现的.
最后在说一句,大家要是转载,请注明出处,毕竟我敲了半个多小时的键盘,没有功劳也有苦劳.