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.