Detect power mode

Get help with using AutoHotkey and its commands and hotkeys
User avatar
RandomGgames
Posts: 5
Joined: 28 Mar 2017, 07:12
Location: USA
Contact:

Detect power mode

28 Mar 2017, 10:31

Is there a way to detect the current power mode selected on your PC? For instance... you have a High performance and a Low Power mode for your laptop and I want to be able to read what is currently selected and read it as a variable so I can execute something else if High Power is active or if Low Power is.
-Random
qwerty12
Posts: 468
Joined: 04 Mar 2016, 04:33
GitHub: qwerty12

Re: Detect power mode

28 Mar 2017, 11:55

Assuming you're talking about Windows' power profiles feature and not something specific to whatever laptop you use, you can do something like to get the name of the currently active profile name:

Code: Select all

if (DllCall("powrprof\PowerGetActiveScheme", "Ptr", 0, "Ptr*", pguid) == 0) {
	VarSetCapacity(desc, (szdesc := 256) + 2)
	if (DllCall("powrprof\PowerReadFriendlyName", "Ptr", 0, "Ptr", pguid, "Ptr", 0, "Ptr", 0, "Ptr", &desc, "UInt*", szdesc) == 0, DllCall("LocalFree", "Ptr", pguid, "Ptr")) {
		activeProfileName := StrGet(&desc, "UTF-16")
		MsgBox % activeProfileName
	}
}
User avatar
RandomGgames
Posts: 5
Joined: 28 Mar 2017, 07:12
Location: USA
Contact:

Re: Detect power mode

29 Mar 2017, 08:01

I actually figured out how to do it through the registry by rereading the current profile (which gives me the ID), and I use that variable to read the friendly name of the ID

Code: Select all

RegRead, CurrentPowerMode, HKEY_LOCAL_MACHINE, SYSTEM\CurrentControlSet\Control\Power\User\PowerSchemes, ActivePowerScheme
RegRead, CurrentPowerModeName, HKEY_LOCAL_MACHINE, SYSTEM\CurrentControlSet\Control\Power\User\PowerSchemes\%CurrentPowerMode%, FriendlyName
MsgBox, 0, Test, %CurrentPowerModeName%
-Random
SvenBent
Posts: 266
Joined: 09 Aug 2015, 01:34

Re: Detect power mode

29 Mar 2017, 09:42

qwerty12 wrote:Assuming you're talking about Windows' power profiles feature and not something specific to whatever laptop you use, you can do something like to get the name of the currently active profile name:

Code: Select all

if (DllCall("powrprof\PowerGetActiveScheme", "Ptr", 0, "Ptr*", pguid) == 0) {
	VarSetCapacity(desc, (szdesc := 256) + 2)
	if (DllCall("powrprof\PowerReadFriendlyName", "Ptr", 0, "Ptr", pguid, "Ptr", 0, "Ptr", 0, "Ptr", &desc, "UInt*", szdesc) == 0, DllCall("LocalFree", "Ptr", pguid, "Ptr")) {
		activeProfileName := StrGet(&desc, "UTF-16")
		MsgBox % activeProfileName
	}
}
Since it seems you are into the Pwrproff.dll. can you give me a quick help
I wish to read this settings from the current powerprofile (bolded)

Subgroup GUID: 54533251-82be-4824-96c1-47b60b740d00 (Processor power management)
GUID Alias: SUB_PROCESSOR
Power Setting GUID: 0cc5b647-c1df-4637-891a-dec35c318583 (Processor performance core parking min cores)
GUID Alias: CPMINCORES
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: 0x00000005

Perhaps even change it
qwerty12
Posts: 468
Joined: 04 Mar 2016, 04:33
GitHub: qwerty12

Re: Detect power mode

30 Mar 2017, 19:35

SvenBent wrote: I wish to read this settings from the current powerprofile (bolded)
You can try something like this:

Code: Select all

GetCoreParkingMinCoresAcSettingForCurrentPowerProfile()
{
	static  cbguid := 16, CLSIDFromString := DllCall("GetProcAddress", "Ptr", DllCall("GetModuleHandle", "Str", "ole32.dll", "Ptr"), "AStr", "CLSIDFromString", "Ptr")
			,guids := {"GUID_PROCESSOR_SETTINGS_SUBGROUP": "{54533251-82be-4824-96c1-47b60b740d00}"
					 , "GUID_PROCESSOR_CORE_PARKING_MIN_CORES": "{0cc5b647-c1df-4637-891a-dec35c318583}"}

	if (guids.GetCapacity("GUID_PROCESSOR_SETTINGS_SUBGROUP") != cbguid) {
		for k, v in guids {
			guids.SetCapacity(k, cbguid)
			,DllCall(CLSIDFromString, "WStr", v, "Ptr", guids.GetAddress(k))
		}
	}

	if (DllCall("powrprof\PowerGetActiveScheme", "Ptr", 0, "Ptr*", pguid) == 0) {
		if (DllCall("powrprof\PowerReadACValueIndex", "Ptr", 0, "Ptr", pguid, "Ptr", guids.GetAddress("GUID_PROCESSOR_SETTINGS_SUBGROUP"), "Ptr", guids.GetAddress("GUID_PROCESSOR_CORE_PARKING_MIN_CORES"), "UInt*", acCoreParkingMincores, "UInt") == 0) {
			MsgBox % acCoreParkingMincores
		}
		; writing:
		; DllCall("powrprof\PowerWriteACValueIndex", "Ptr", 0, "Ptr", pguid, "Ptr", guids.GetAddress("GUID_PROCESSOR_SETTINGS_SUBGROUP"), "Ptr", guids.GetAddress("GUID_PROCESSOR_CORE_PARKING_MIN_CORES"), "UInt", 80, "UInt")
		DllCall("LocalFree", "Ptr", pguid, "Ptr")
	}
}
SvenBent
Posts: 266
Joined: 09 Aug 2015, 01:34

Re: Detect power mode

01 Apr 2017, 01:52

[quote="qwerty12"][/quote]

Thank you. that hellped a lot. i modified your code to my own written style and made a few adjustment. but the code works perfect.
I'm still having hard time in general with datastructures and dll calls

A funny part about this is that this read the settings from powerprofile and not the live settings

If i change a power profile but dot not activate it, so the changes are not live.
This code will show the changes even though its not active yet.
not a problem for me just a random facts to share
qwerty12
Posts: 468
Joined: 04 Mar 2016, 04:33
GitHub: qwerty12

Re: Detect power mode

01 Apr 2017, 06:08

SvenBent wrote: If i change a power profile but dot not activate it, so the changes are not live.
This code will show the changes even though its not active yet.
not a problem for me just a random facts to share
Oh, sorry, I know you have to reactivate the current profile if you change the brightness but it completely slipped my mind that doing it for effecting other changes might be necessary.

You can add DllCall("powrprof\PowerSetActiveScheme", "Ptr", 0, "Ptr", pguid, "UInt") before the DllCall to LocalFree if you want the system to reactivate (and take in the changes of) the currently active profile
SvenBent
Posts: 266
Joined: 09 Aug 2015, 01:34

Re: Detect power mode

03 Apr 2017, 04:53

qwerty12 wrote:
SvenBent wrote: If i change a power profile but dot not activate it, so the changes are not live.
This code will show the changes even though its not active yet.
not a problem for me just a random facts to share
Oh, sorry, I know you have to reactivate the current profile if you change the brightness but it completely slipped my mind that doing it for effecting other changes might be necessary.

You can add DllCall("powrprof\PowerSetActiveScheme", "Ptr", 0, "Ptr", pguid, "UInt") before the DllCall to LocalFree if you want the system to reactivate (and take in the changes of) the currently active profile
Thank you but its already works perfect for my purpose.
what i do now is i reade the states of the profile.
change the state in the profile
reactive the profile
Change the state back to the old value.

This way there is no permanetn change in the users profile and a reboot/restart is always back to normal

Return to “Ask For Help”

Who is online

Users browsing this forum: Bill, Meka_Tenshi, mikeyww, Rohwedder, Sabestian Caine, teadrinker and 54 guests