Windows XP电源管理及注册表分析
http://www.cnblogs.com/ziwuge/archive/2011/10/04/2199141.html
注册表:
注册表中电源选项的关键字为“PowerCfg”,以为Windows XP SP3的系统为例,注册表中有“PowerCfg”的位置有①HKEY_CURRENT_USER\Control Panel\PowerCfg②HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\ControlsFolder\PowerCfg③HKEY_USERS\.DEFAULT\Control Panel\PowerCfg④HKEY_USERS\S-1-5-18\Control Panel\PowerCfg⑤HKEY_USERS\S-1-5-19\Control Panel\PowerCfg⑥HKEY_USERS\S-1-5-20\Control Panel\PowerCfg⑦HKEY_USERS\S-1-5-21-1292428093-1123561945-682003330-1003\Control Panel\PowerCfg⑧HKEY_USERS\S-1-5-21-1292428093-1123561945-682003330-1003\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit。
最后通过测试分析发现电源选项中注册表①和②最为重要,①中项“PowerPolicies”为电源选项中配置的方案列表,“CurrentPowerPolicy”为当前系统使用的方案。如下:
[HKEY_CURRENT_USER\Control Panel\PowerCfg\PowerPolicies\0]
"Name"="家用/办公桌"
"Description"="此种模式适用于大多数家用和桌面计算机,这些计算机可在任何时候插入使用。"
"Policies"=hex:01,00,00,00,02,00,00,00,01,00,00,00,00,00,00,00,02,00,00,00,05,\
00,00,00,00,00,00,00,[b0,04],00,00,78,00,00,00,32,32,03,02,02,00,00,00,02,00,\
00,00,00,00,18,77,2e,f2,07,00,[3c,00],00,00,3c,00,00,00,[00,00],00,00,b4,00,00,\
00,00,00,64,64,64,64,93,7c
在Policies中存储的REG_BINARY格式(16进制格式?),分析发现第一个[]中(28/29位置,0开始)表示“系统待机”,第二个[]中(56/57位置)表示“关闭监视器”,第三个[](64/65位置)表示“关闭硬盘”。时间是按秒数的16进制保存的,比如[b0,04]表示16进制的[0x04b0],换算成十进制为1200,表示20分钟,"从不"对应的为[00,00]。
②中“PowerPolicies”和“ProcessorPolicies”列举的是出现过的电源选项(包括自定义),这里解释一下,当你通过“控制面板”→“电源选项”新建的方案这里会记录,即使你删除之后这里还是会存在(存储的数值没有分析过什麽含义:)),①的记录则会删除。“LastID”表示历史记录中出现过的方案数目,这个值和①也会有关系的。
API函数:(来源参考资料1)
Windows的电源管理可以指定多种Schemes(配置),并指定当前应用何种Scheme。对于每一种Scheme包含使用电池时的配置和使用电源时的配置。
1、枚举系统所有的Power Schemes
![](/assets/blank.gif)
![](/assets/blank.gif)
BOOLEAN EnumPwrSchemes(PWRSCHEMESENUMPROC lpfnPwrSchemesEnumProc,LPARAM lParam );
自定义的枚举Callback函数类型是:
![](/assets/blank.gif)
![](/assets/blank.gif)
Typedef BOOLEAN (CALLBACK* PWRSCHEMESENUMPROC)(UINT uiIndex, // power scheme index DWORD dwName, // size of the sName string, in bytes LPTSTR sName, // name of the power scheme DWORD dwDesc, // size of the sDesc string, in bytes LPTSTR sDesc, // description string PPOWER_POLICY pp, // receives the power policy LPARAM lParam // user-defined value );
其中:The sName and sDesc parameters are null-terminated strings; they are Unicode strings on Windows 2000/XP and ANSI strings on Windows Me/98/95.
2、得到当前正在使用的Power Scheme:
![](/assets/blank.gif)
![](/assets/blank.gif)
BOOLEAN GetActivePwrScheme(PUINT puiID/*得到Power Scheme索引*/ ); BOOLEAN ReadPwrScheme(UINT uiID,/*根据索引读取Scheme*/PPOWER_POLICY pPowerPolicy );
或:
![](/assets/blank.gif)
![](/assets/blank.gif)
BOOLEAN GetCurrentPowerPolicies(PGLOBAL_POWER_POLICY pGlobalPowerPolicy,PPOWER_POLICY pPowerPolicy );
3、判断当前计算机是连接在电源上还是电池上:
![](/assets/blank.gif)
![](/assets/blank.gif)
SYSTEM_POWER_STATUS sps; GetSystemPowerStatus(&sps); if (sps.ACLineStatus) {// 连接电源 } else {// 连接电池 }
4、通过Windows的电源管理功能,我们可以得到(设定)显示器的关闭时间、硬盘停转时间、系统休眠时间。设定时间的API是:
![](/assets/blank.gif)
![](/assets/blank.gif)
BOOLEAN WritePwrScheme(PUINT puiID,/*Power Scheme索引*/LPTSTR lpszName,LPTSTR lpszDescription,PPOWER_POLICY pPowerPolicy );
显示器关闭时间:POWER_POLICY.user.VideoTimeoutAc(电源)或POWER_POLICY.user.VideoTimeoutDc(电池)
硬盘停转时间:POWER_POLICY.user.SpindownTimeoutAc(电源)或POWER_POLICY.user.SpindownTimeoutDc(电池)
系统Standby和Hibernate时间稍微复杂一点,以下以连接在电源(AC)为例,得到时间的逻辑为:
![](/assets/blank.gif)
![](/assets/blank.gif)
if (POWER_POLICY.user.IdleAc.Action == PowerActionHibernate) {Standby 时间 = 0;Hibernate 时间 = POWER_POLICY.user.IdleTimeoutAc; } else {Standby 时间 = POWER_POLICY.user.IdleTimeoutAc;Hibernate 时间 = POWER_POLICY.user.IdleTimeoutAc+POWER_POLICY.mach.DozeS4TimeoutAc; } // 设定的逻辑为(时间值为0表示从不Standby或从不Hibernate): if (!Standby时间 && !Hibernate时间) {POWER_POLICY.user.IdleTimeoutAc = 0;POWER_POLICY.mach.DozeS4TimeoutAc = 0;POWER_POLICY.user.IdleAc.Action = PowerActionNone; } else if (Hibernate时间 && !Standby时间) {POWER_POLICY.user.IdleTimeoutAc = Hibernate时间;POWER_POLICY.mach.DozeS4TimeoutAc = 0;POWER_POLICY.user.IdleAc.Action = PowerActionHibernate; } else {POWER_POLICY.user.IdleTimeoutAc = Standby时间;POWER_POLICY.mach.DozeS4TimeoutAc = 0;if (Hibernate时间 > Standby时间)POWER_POLICY.mach.DozeS4TimeoutAc = Hibernate时间 - Standby时间;POWER_POLICY.user.IdleAc.Action = PowerActionSleep; }
5、还可以指定是否允许休眠,从待机状态中回复过来是否需要密码等
5.1、检查系统是否允许Hibernate
BOOLEAN IsPwrHibernateAllowed(void);
5.2、禁止系统Hibernate
![](/assets/blank.gif)
![](/assets/blank.gif)
BOOL bEnable = FALSE; CallNtPowerInformation(SystemReserveHiberFile, &bEnable, sizeof(BOOLEAN), NULL, NULL);
这里注意:typedef BYTE BOOLEAN
5.3、允许系统Hibernate
![](/assets/blank.gif)
![](/assets/blank.gif)
BOOL bEnable = TRUE; CallNtPowerInformation(SystemReserveHiberFile, &bEnable, sizeof(BOOLEAN), NULL, NULL);
还有一种最简单的办法是直接执行系统提供的程序windows\system32\powercfg.exe /hibernate {on|off}
6.还有一种更简便的方法得到(设定)系统当前的屏幕关闭时间;待机、休眠时间等,那就是调用CallNtPowerInformation:
![](/assets/blank.gif)
![](/assets/blank.gif)
NTSTATUS CallNtPowerInformation(POWER_INFORMATION_LEVEL InformationLevel,PVOID lpInputBuffer,ULONG nInputBufferSize,PVOID lpOutputBuffer,ULONG nOutputBufferSize );
其中InformationLevel设为SystemPowerPolicyAc(连接电源)或SystemPowerPolicyDc(使用电池)
如:
SYSTEM_POWER_POLICY spp = {0};
CallNtPowerInformation(SystemPowerPolicyAc, NULL, 0, &spp, sizeof(spp)); //get power information
//IdleTimeout:Time that the level of system activity must remain below the idle detection threshold before the system idle timer expires, in seconds.
//Idle: A POWER_ACTION_POLICY structure that defines the system power action to initiate when the system idle timer expires.
spp.IdleTimeout = ?;
//DozeS4Timeout: Time to wait between entering the suspend state and entering the hibernate sleeping state, in seconds. A value of zero indicates never hibernate.
spp.DoseS4Timeout = ?;
//SpindownTimeout:Time before power to fixed disk drives is turned off, in seconds.
spp.SpindownTimeout = ?;
//VideoTimeout:Time before the display is turned off, in seconds.
spp.VideoTimeout = ?;
CallNtPowerInformation(SystemPowerPolicyAc, &spp, sizeof(spp), NULL, 0); //set new power information
7.也可以通过WMI来设置,但需注意WMI版本是否支持:(如以下vbscript)
Set objService = GetObject("winmgmts:\\.\root\wmi")
Set objMonSet = objService.InstancesOf("WmiMonitorBrightnessMethods")
For Each objMon In objMonSet
objMon.WmiSetBrightness 10, 90
Next
8.另需注意:
调用SetActivePwrScheme()和ReadPwrScheme()时,若传递的Scheme ID不存在,则会在注册表(HKEY_CURRENT_USER\Control Panel\PowerCfg\PowerPolicies\)下创建这个ID,但内容为空,而这会导致电源管理遍历函数EnumPwrSchemes()不能正常工作(读不到该ID后的power scheme);VISTA上的对应函数(PowerGetActiveScheme/PowerReadxxx)就没发现这样的问题。
补充:
POWER_POLICY结构体的介绍http://msdn.microsoft.com/zh-cn/aa372709
POWER_POLICY结构体自己的分析:IdleTimeOut 系统待机;VideoTimeOut 关闭监视器;SpindownTimeout 关闭硬盘;AC 外接电源;DC 电池。
Windows XP电源管理及注册表分析相关推荐
- Windows XP 激活之修改注册表
1. 安装原版windows xp 2. 打开注册表regedit 3. 找到主键 Hkey_Local_Machine\Software\Microsoft\WindowsNT\CurrentVer ...
- 载录Windows 9X、2000、XP、2003所有注册表设置
『电脑网络』 [操作系统]9X.2000.XP.2003所有注册表设置 开始菜单及相关设置> [HKEY_CURRENT_USER/Software/Microsoft/Windows/ ...
- WINDOWS中关于内存管理的注册表优化
WINDOWS中关于内存管理的注册表优化 [HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Control/Session Manager/Memory Man ...
- Windows XP/2003 系统进程速查表
Windows XP/2003系统进程速查表 启动Windows XP/2003时,总会有相当多的程序调入到系统的内存中.我们可以通过按下Ctrl+Atl+Del组合键调出的"Windows ...
- 计算机注册表管理,学会注册表几个常用的设置,更好地管理自己的电脑!
注册表是Windows操作系统的核心数据库,存放着各种参数,直接控制着Windows的启动.硬件驱动程序的装载以及一些Windows应用程序的运行.从Microsoft Windows 95操作系统开 ...
- Windows与网络基础:注册表基础和注册表维护与优化
学习目标 1.理解注册表概念 2.掌握注册表维护及优化方法 目录 一.注册表基础 1.概述 2.早期的注册表 3.Windows 95后的注册表 4.注册表结构 4.1.注册表以树状结构进行呈现 4. ...
- windows中,什么是注册表与注册表的作用
Windows Operating System Registry 注册表(Registry)是微软公司从Windows95系统开始(至目前最新Win10系统依然使用的是它),引入用于代替原先Win3 ...
- 分享一个windows 10优化用的注册表
转摘于http://wuyou.net/forum.php?mod=viewthread&tid=413465 分享一个自己用的windows 10优化用的注册表,再也不用点来点去了... 开 ...
- 『恶意代码分析实战』Windows API编程——通过修改注册表的方式实现自启动
文章目录 前言 实验要求 实验环境 实验目的 代码 演示 完 前言 实验要求 编写代码,编辑注册表的Run/RunOnce/RunOnceEx键(任选其一),达到让某一程序在系统启动后自动运行的目的( ...
最新文章
- C语言中有bool变量吗?
- Java 获取文件目录最终的修改时间
- Building wheel for mmcv-full (setup.py) ... error和OSError: CUDA_HOME environment variable is not set
- 【生活】我的2019年度总结
- 机器学习算法总结之聚类:K-means
- Pycharm安装完出现interpreter field is empty
- SPSS时序全局主成分分析方法
- linux任务计划时间讲解,linux下计划任务详解
- 1.CLUSTERDOWN Hash slot not served
- php通用补丁,PHP受权验证系统V2.1完整版 带补丁包
- 本地HTML文档批量翻译软件
- css中图片不清晰解决方法
- Docker 进入容器出现Unable to find user root: no matching entries in passwd file
- 美团基于知识图谱的剧本杀标准化建设与应用
- Visio安装问题解决:office即点即用安装程序
- 百度地图api不支持windows平板 双指放大缩小解决方案
- java中next()和nextline()用法区别(详细说明)
- 自动泊车(之三)车位线定位(视觉定位)
- 制作chm文件的图片显示问题
- mysql优化or_mysql or语句的优化