Power settings

Post tutorials and script examples in this forum.
Forum rules
We have no special rules for UVK forums. Just try to be polite and clear in your posts.
Please don't post spam in this forum. Spammers will be banned by IP, e-mail and username.
We reserve the right to delete all posts and ban all users we consider not having respected these rules without warning.
Post Reply
Charger440
Posts: 1529
Joined: Sun May 25, 2014 7:44 am
Location: Missouri

Power settings

Post by Charger440 »

There was a request at one point (might have even been me that requested it, not sure) for power settings. After quite a bit of searching I finally found a way to do it.
This script sets the lid close to do nothing and Sleep, screen time and hard drive to never. The screen dim is set to 20 minutes. If you want to reset to close to default settings just run it again and select NO when it asks you if you want to use Tech settings.


It does need a lil work but Fred may add it before I get to fine tune it, if not, I will continue to work on it and fine tune it.

Jim
Attachments
PowerSettings.uvk
Power settings tool
(2.55 KiB) Downloaded 433 times
Jim

It is not "Can it be done?" but rather, "How can we do it?"
Fred
Site Admin
Posts: 2360
Joined: Sat Jul 30, 2011 12:05 pm
Location: Red coast, France
Contact:

Re: Power settings

Post by Fred »

Nice, but it can be easily done the right way: Using the Windows API.

You'd just need a few functions:

PowerGetActiveScheme to get the active power scheme.

LocalFree to free up the returned GUID pointer.

PowerWriteACValueIndex to set those values.

PowerSetActiveScheme to apply the changes.

UuidFromString so you don't have to hard code the GUIDs yourself.

That may be a good learning curve, if you're really interested in learning AutoIt, but I can give you a hand. Let me know.
One thing we humans have in common is that we are all different. So, if you think you're weird because you're different from everyone else, then we are all weird.

Fred
Charger440
Posts: 1529
Joined: Sun May 25, 2014 7:44 am
Location: Missouri

Re: Power settings

Post by Charger440 »

Fred
That's part of what took so long for me too do it. I was looking for an api or I figured the .net framework would do it. I seen these apis but was not sure if they would work and didn't find anything in. Net but there has to be something in there somewhere.

Jim
Jim

It is not "Can it be done?" but rather, "How can we do it?"
Fred
Site Admin
Posts: 2360
Joined: Sat Jul 30, 2011 12:05 pm
Location: Red coast, France
Contact:

Re: Power settings

Post by Fred »

Well, those APIs do exactly the same thing you did with your script. The code is pretty easy. Here it goes:

Code: Select all

 <AutoItScript>
Global Const $tagGUID = 'ULONG Data1;USHORT Data2;USHORT Data3;BYTE Data4[8]'
_SetOptimalPowerSettings()

Func _SetOptimalPowerSettings()
	Local $ret = 0, $PowerID  = _PowerGetActiveScheme()
	If $PowerID  = 0 Then
		MsgBox(0,'Error','The active scheme could not be retrieved.')
		Exit 1
	EndIf

	;Find out if we are turning off or on
	Local $Qresponse = MsgBox(4,"Technician Settings", _
	"Do you want to turn on Technician optimal power settings?")

	;Lid Close
	Local $value = ($QResponse = 6) ? 0 : 1
	$ret += _PowerWriteACValueIndex($PowerID, _
	_UuidFromString('4f971e89-eebd-4455-a8de-9e59040e7347'), _
	_UuidFromString('5ca83367-6e45-459f-a27b-476b1d01c936'), $value)

	$ret += _PowerWriteACValueIndex( _
	_UuidFromString('381b4222-f694-41f0-9685-ff5bb260df2e'), _
	_UuidFromString('4f971e89-eebd-4455-a8de-9e59040e7347'), _
	_UuidFromString('5ca83367-6e45-459f-a27b-476b1d01c936'), $value)

	;HD
	$value = ($QResponse = 6) ? 0 : 1800
	$ret += _PowerWriteACValueIndex($PowerID, _
	_UuidFromString('0012ee47-9041-4b5d-9b77-535fba8b1442'), _
	_UuidFromString('6738e2c4-e8a5-4a42-b16a-e040e769756e'), $value)

	;Sleep Time out
	$ret += _PowerWriteACValueIndex($PowerID, _
	_UuidFromString('238c9fa8-0aad-41ed-83f4-97be242c8f20'), _
	_UuidFromString('29f6c1db-86da-48c5-9fdb-f2b67b1f44da'), $value)

	;Dim Display
	$value = ($QResponse = 6) ? 900 : 300
	$ret += _PowerWriteACValueIndex($PowerID, _
	_UuidFromString('7516b95f-f776-4464-8c53-06167f40cc99'), _
	_UuidFromString('17aaa29b-8b43-4b94-aafe-35f64daaf1ee'), $value)

	;Turn off display
	$value = ($QResponse = 6) ? 0 : 1200
	$ret += _PowerWriteACValueIndex($PowerID, _
	_UuidFromString('7516b95f-f776-4464-8c53-06167f40cc99'), _
	_UuidFromString('3c0bc021-c8a8-4e07-a973-6b14cbcb2b7e'), $value)

	;Apply changes
	Local $msg = _PowerSetActiveScheme($PowerID) ? _
	'The technician optimal power settings were successfully applied.' : _
	'The technician optimal power settings could not be applied.'

	;Free the pointer
	_LocalFree($PowerID )

	MsgBox(0,'Technician Settings',$msg&@CRLF&@CRLF&$ret&' power policies applied.')
EndFunc ;==> _SetOptimalPowerSettings

Func _PowerGetActiveScheme()
	Local $aCall = DllCall('PowrProf.dll', 'DWORD','PowerGetActiveScheme', 'ptr',0, 'ptr*',0)
	If @error Or $aCall[0] Then Return SetError(1,0,0)
	Return $aCall[2]
EndFunc ;==> _PowerGetActiveScheme

Func _LocalFree($hmem)
	Local $aCall = DllCall('Kernel32.dll', 'ptr','LocalFree', 'ptr',$hmem)
	If Not @error Then Return Number($aCall[0] = 0)
EndFunc ;==> _LocalFree

Func _PowerWriteACValueIndex($SchemeGuid, $SubGroupGuid, $SettingGuid, $AcValueIndex)
	Local $pSchemeGuid = IsDllStruct($SchemeGuid) ? DllStructGetPtr($SchemeGuid) : $SchemeGuid
	Local $pSubGroupGuid = IsDllStruct($SubGroupGuid) ? DllStructGetPtr($SubGroupGuid) : $SubGroupGuid
	Local $pSettingGuid = IsDllStruct($SettingGuid) ? DllStructGetPtr($SettingGuid) : $SettingGuid
	Local $aCall = DllCall('PowrProf.dll', 'DWORD','PowerWriteACValueIndex', _
	'ptr',0, 'ptr',$pSchemeGuid, 'ptr',$pSubGroupGuid , 'ptr',$pSettingGuid, 'DWORD',$AcValueIndex)
	If Not @error Then Return Number($aCall[0] = 0)
EndFunc ;==> _PowerWriteACValueIndex

Func _PowerSetActiveScheme($SchemeGuid)
	Local $pSchemeGuid = IsDllStruct($SchemeGuid) ? DllStructGetPtr($SchemeGuid) : $SchemeGuid
	Local $aCall = DllCall('PowrProf.dll', 'DWORD', 'PowerSetActiveScheme', 'ptr',0, 'ptr',$pSchemeGuid)
	If Not @error Then Return Number($aCall[0] = 0)
EndFunc ;==> _PowerSetActiveScheme

Func _UuidFromString($guidStr)
	Local $sret = DllStructCreate($tagGUID)
	Local $aCall = DllCall('Rpcrt4.dll', 'DWORD','UuidFromStringW', 'wstr',$guidStr, 'struct*',$sret)
	If @error Or $aCall[0] Then Return SetError(1,0,$sret)
	Return $sret
EndFunc ;==> _UuidFromString
Hope this helps you to get started using the Windows API from AutoIt. It allows you to do everything any other application does.

EDIT: Fixed a typo in the _LocalFree() function.
One thing we humans have in common is that we are all different. So, if you think you're weird because you're different from everyone else, then we are all weird.

Fred
Post Reply