修改电源配置

2019-07-13 23:23发布

使用注册表,修改电源配置

可以在cmd.exd命令行窗口执行powercfg.exe
电源配置所在注册表位置:HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlPowerUserPowerSchemes 最开始的时候,是想通过直接修改注册表去处理,可是发现各种没权限,在inno setup中写注册表不行,用api也不行,最后找到了这种方法。
powerfcg guid 参考地址:https://blogs.technet.microsoft.com/richardsmith/2007/11/29/powercfg-useful-if-you-know-the-guids/ 1.获取当前使用的电源计划,如:平衡, 高性能, 节能等。
对应的guid:
平衡:381b4222-f694-41f0-9685-ff5bb260df2e
高性能:8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c
节能:a1841308-3541-4fab-bc81-f71556f20b4a //get current powercfg guid QString currPowerGuid; QProcess powercfg; powercfg.start("powercfg -getactivescheme"); //if(powercfg.waitForReadyRead()) while(powercfg.waitForFinished()) { QString result = QString::fromLocal8Bit(powercfg.readAll()); QStringList resultList = result.split(" "); //eg: 电源方案 GUID: 381b4222-f694-41f0-9685-ff5bb260df2e (平衡) if(resultList.count() >= 4) { currPowerGuid = resultList[2]; } else { g_log->logFunc2(LOG_ERROR, "get current powercfg guid error."); return; } } powercfg.close(); 2.设置唤醒时不需要密码 QString wakeupParentGuid = "fea3413e-7e05-4911-9a71-700331f1c294"; QString wakeupPasswordGuid = "0e796bdb-100d-47d6-a2d5-f7d2daa51f51"; //enable wake up password, not use wake up password QString cmdPasswordStr = "powercfg -setacvalueindex " + currPowerGuid + " " + wakeupParentGuid + " " + wakeupPasswordGuid + " " + QString::number(0); powercfg.start(cmdPasswordStr); while(powercfg.waitForFinished()) { g_log->logFunc2(LOG_INFO, "disable wakeup password success"); } powercfg.close(); 3.设置启用定时器唤醒功能 QString sleepGuid = "238c9fa8-0aad-41ed-83f4-97be242c8f20"; QString enableTimerWakeGuid = "bd3b718a-0680-4d9d-8ab2-e1d2b4ac806d"; //enable timer wake up, use wake up timer QString cmdStr = "powercfg -setacvalueindex " + currPowerGuid + " " + sleepGuid + " " + enableTimerWakeGuid + " " + QString::number(enable); powercfg.start(cmdStr); while(powercfg.waitForFinished()) { g_log->logFunc2(LOG_INFO, "enable wakeup timer success"); } powercfg.close();