I wanted to add that you can get the
type in the same way you get the buffer size. Which would allow you to write a function wrapper that handles various types (like in the incomplete example below).
And, no, I am not a wizard. I'm not even particularly knowledgeable about this and my code is not guaranteed to be error free. In fact there was an error in how I used the
StringFromCLSID function above.
&rclsid should have been
rclsid because pguid already contains the address of a GUID it doesn't need to be dereferenced. As a result, the function was returning the wrong GUID string.
Code: Select all
hModule := DllCall("Kernel32\LoadLibrary", "str", "PowrProf.dll")
DllCall("PowrProf\PowerGetActiveScheme", "Ptr", 0, "Ptr*", pguid)
MsgBox, % "GUID: " StringFromCLSID(pguid) "`n"
. "Seconds until sleep: " PowerReadACValue(pguid, GUID(SubGroupOfPowerSettingsGuid, "{238C9FA8-0AAD-41ED-83F4-97BE242C8F20}")
, GUID(PowerSettingGuid, "{29f6c1db-86da-48c5-9fdb-f2b67b1f44da}"))
DllCall("Kernel32\LocalFree", "Ptr", pguid, "Ptr")
DllCall("Kernel32\FreeLibrary", "Ptr", hModule)
return
PowerReadACValue(pSchemeGuid, pSubGroupOfPowerSettingsGuid, pPowerSettingGuid) {
hr := DllCall("PowrProf\PowerReadACValue"
, "Ptr", 0 ; HKEY RootPowerKey
, "Ptr", pSchemeGuid ; GUID *SchemeGuid
, "Ptr", pSubGroupOfPowerSettingsGuid ; GUID *SubGroupOfPowerSettingsGuid
, "Ptr", pPowerSettingGuid ; GUID *PowerSettingGuid
, "Ptr", 0 ; PULONG Type
, "Ptr", 0 ; LPBYTE Buffer
, "UInt*", BufferSize) ; LPDWORD BufferSize ; BufferSize parameter receives the required buffer size.
if (hr != 0) ; Replace with appropriate error handling.
MsgBox, % "hr: " hr "`nLastError: " A_LastError "`nBufferSize: " BufferSize
VarSetCapacity(Buffer, BufferSize, 0)
hr := DllCall("PowrProf\PowerReadACValue"
, "Ptr", 0 ; HKEY RootPowerKey
, "Ptr", pSchemeGuid ; GUID *SchemeGuid
, "Ptr", pSubGroupOfPowerSettingsGuid ; GUID *SubGroupOfPowerSettingsGuid
, "Ptr", pPowerSettingGuid ; GUID *PowerSettingGuid
, "UInt*", Type ; PULONG Type ; Type parameter receives the type of data retrieved.
, "Ptr", &Buffer ; LPBYTE Buffer
, "Ptr", &BufferSize) ; LPDWORD BufferSize
if (hr != 0) ; Replace with appropriate error handling.
MsgBox, % "hr: " hr "`nLastError: " A_LastError "`nBufferSize: " BufferSize
else if (Type = 4) ; REG_DWORD := 4
return NumGet(Buffer, 0, "UInt")
; else if (Type = ... ; Handle other types here
else
MsgBox, % "Type: " Type
return
}
GUID(ByRef GUID, sGUID) ; Converts a string to a binary GUID and returns its address.
{ ; source = https://autohotkey.com/docs/commands/ComObjQuery.htm#ExIE
VarSetCapacity(GUID, 16, 0)
return DllCall("ole32\CLSIDFromString", "wstr", sGUID, "Ptr", &GUID) >= 0 ? &GUID : ""
}
StringFromCLSID(rclsid) ; Converts a binary GUID to a string. rclsid is the address of the GUID.
{
DllCall("ole32\StringFromCLSID", "Ptr", rclsid, "Ptr*", lplpsz)
s := StrGet(lplpsz, "UTF-16")
DllCall("ole32\CoTaskMemFree", "Ptr", lplpsz)
return s
}