Make CheckIsUserOnHighPerfPowerPlan use WIL

This commit is contained in:
Silent 2021-09-04 16:07:27 +02:00 committed by Kojin
parent ec9c6521fd
commit 45de8f77cd
1 changed files with 12 additions and 13 deletions

View File

@ -15,34 +15,33 @@
#include "PrecompiledHeader.h" #include "PrecompiledHeader.h"
#include <powrprof.h> #include <powrprof.h>
#include <wil/com.h>
using unique_any_guidptr = wil::unique_any<GUID*, decltype(&::LocalFree), ::LocalFree>;
// Checks the current active power plan // Checks the current active power plan
// If the power plan isn't named 'high performance', put a little message in the console // If the power plan isn't named 'high performance', put a little message in the console
// This function fails silently // This function fails silently
void CheckIsUserOnHighPerfPowerPlan() void CheckIsUserOnHighPerfPowerPlan()
{ {
GUID* pPwrGUID; unique_any_guidptr pPwrGUID;
DWORD ret = PowerGetActiveScheme(NULL, &pPwrGUID); DWORD ret = PowerGetActiveScheme(NULL, pPwrGUID.put());
if (ret != ERROR_SUCCESS) if (ret != ERROR_SUCCESS)
return; return;
UCHAR aBuffer[2048]; UCHAR aBuffer[2048];
DWORD aBufferSize = sizeof(aBuffer); DWORD aBufferSize = sizeof(aBuffer);
ret = PowerReadFriendlyName(NULL, pPwrGUID, &NO_SUBGROUP_GUID, NULL, aBuffer, &aBufferSize); ret = PowerReadFriendlyName(NULL, pPwrGUID.get(), &NO_SUBGROUP_GUID, NULL, aBuffer, &aBufferSize);
if (ret != ERROR_SUCCESS) if (ret != ERROR_SUCCESS)
goto cleanup; return;
DWORD acMax = 0,acMin = 0,dcMax = 0,dcMin = 0; DWORD acMax = 0,acMin = 0,dcMax = 0,dcMin = 0;
if (PowerReadACValueIndex(NULL, pPwrGUID, &GUID_PROCESSOR_SETTINGS_SUBGROUP, &GUID_PROCESSOR_THROTTLE_MAXIMUM, &acMax) || if (PowerReadACValueIndex(NULL, pPwrGUID.get(), &GUID_PROCESSOR_SETTINGS_SUBGROUP, &GUID_PROCESSOR_THROTTLE_MAXIMUM, &acMax) ||
PowerReadACValueIndex(NULL, pPwrGUID, &GUID_PROCESSOR_SETTINGS_SUBGROUP, &GUID_PROCESSOR_THROTTLE_MINIMUM, &acMin) || PowerReadACValueIndex(NULL, pPwrGUID.get(), &GUID_PROCESSOR_SETTINGS_SUBGROUP, &GUID_PROCESSOR_THROTTLE_MINIMUM, &acMin) ||
PowerReadDCValueIndex(NULL, pPwrGUID, &GUID_PROCESSOR_SETTINGS_SUBGROUP, &GUID_PROCESSOR_THROTTLE_MAXIMUM, &dcMax) || PowerReadDCValueIndex(NULL, pPwrGUID.get(), &GUID_PROCESSOR_SETTINGS_SUBGROUP, &GUID_PROCESSOR_THROTTLE_MAXIMUM, &dcMax) ||
PowerReadDCValueIndex(NULL, pPwrGUID, &GUID_PROCESSOR_SETTINGS_SUBGROUP, &GUID_PROCESSOR_THROTTLE_MINIMUM, &dcMin)) PowerReadDCValueIndex(NULL, pPwrGUID.get(), &GUID_PROCESSOR_SETTINGS_SUBGROUP, &GUID_PROCESSOR_THROTTLE_MINIMUM, &dcMin))
goto cleanup; return;
Console.WriteLn("The current power profile is '%S'.\nThe current min / max processor states\nAC: %d%% / %d%%\nBattery: %d%% / %d%%\n", aBuffer,acMin,acMax,dcMin,dcMax); Console.WriteLn("The current power profile is '%S'.\nThe current min / max processor states\nAC: %d%% / %d%%\nBattery: %d%% / %d%%\n", aBuffer,acMin,acMax,dcMin,dcMax);
cleanup:
LocalFree(pPwrGUID);
return;
} }