DllCall to CallNtPowerInformation: cannot find function inside DLL

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
dcdc
Posts: 10
Joined: 07 Feb 2017, 11:19

DllCall to CallNtPowerInformation: cannot find function inside DLL

18 Dec 2020, 17:34

Hi,

I have little experience using DllCall and I am stuck with this DLL.
I want to extract idle info from CallNtPowerInformation, specifically the SystemPowerInformation structure.
That should give me a more accurate idle time compared to A_TimeIdle, considering that some cbt softwares as Skype, MS Teams and others can prevent the system to idle for example during a video conference, which is not considered inside A_TimeIdle (as it is based on GetLastInputInfo).

For some reasons I get an ErrorLevel -4

I am also not sure how to pass the enumeration parameter InformationLevel, I have tried it either as "Str", "SystemPowerInformation" or as "Uint" 12.

My code is:

Code: Select all

#NoEnv  
SendMode Input 
structSize:=4*3+1
VarSetCapacity(myStruct, structSize, 0)
returnValue:=DllCall("CallNtPowerInformation", "Str", "SystemPowerInformation", "Int", 0, "Uint", 0, "Ptr", &myStruct, "Uint", structSize)
msgbox, %ErrorLevel%
;Reference to DLL: https://docs.microsoft.com/en-us/windows/win32/api/powerbase/nf-powerbase-callntpowerinformation
Any suggestion is appreciated
dcdc
Posts: 10
Joined: 07 Feb 2017, 11:19

Re: DllCall to CallNtPowerInformation: cannot find function inside DLL

19 Dec 2020, 10:22

That worked!!!

For the enumerator, it was enough using "Uint", 12
Also, I found that the struct size was wrong: it must be 4*4 bytes even though the last member is an Uchar.

Thank you very much
AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: DllCall to CallNtPowerInformation: cannot find function inside DLL

19 Dec 2020, 11:48

dcdc wrote:
19 Dec 2020, 10:22
That worked!!!

For the enumerator, it was enough using "Uint", 12
Also, I found that the struct size was wrong: it must be 4*4 bytes even though the last member is an Uchar.

Thank you very much
can u post the final code?
thanks
dcdc
Posts: 10
Joined: 07 Feb 2017, 11:19

Re: DllCall to CallNtPowerInformation: cannot find function inside DLL

21 Dec 2020, 06:21

Sure
Here it is:

Code: Select all

#NoEnv
SendMode Input
structSize:=4*4
VarSetCapacity(myStruct, structSize, 0)
returnValue:=DllCall("PowrProf\CallNtPowerInformation", "Uint", 12, "Str", "NULL", "Uint", 0, "Ptr", &myStruct, "Uint", structSize)
msgbox % "ErrorLevel: " . ErrorLevel . "`nreturnValue: " . returnValue  . "`nreturnValueHex: " . Format("0x{:X}", returnValue) . "`nMaxIdlenessAllowed: " . NumGet(myStruct, 0, "UInt") . "`nIdleness: " . NumGet(myStruct, 4, "UInt") . "`nTimeRemaining: " . NumGet(myStruct, 8, "UInt") . "`nCoolingMode: " . NumGet(myStruct, 12, "Uchar")
;Reference to DLL: https://docs.microsoft.com/en-us/windows/win32/api/powerbase/nf-powerbase-callntpowerinformation
I still don't understand how to interpret the

Code: Select all

MaxIdlenessAllowed
and

Code: Select all

TimeRemaining
In fact, I get:
MaxIdlenessAllowed = 0 (always)
Idleness = according to the CPU unused %
TimeRemaining = 4294967295 (but a few times I read a lower value)
CoolingMode = 0

I tried on three different Win10 machines, waiting 20 seconds of inactivity or not, before reading, although I cannot get the logic of these parameters.

One of the machines runs an app which resets the idle timer (using SetThreadExecutionState), although also there I cannot notice any difference in the behavior.

Asked on SO as well: https://stackoverflow.com/questions/65371304/understanding-maxidlenessallowed-and-timeremaining-in-system-power-information-s
dcdc
Posts: 10
Joined: 07 Feb 2017, 11:19

Re: DllCall to CallNtPowerInformation: cannot find function inside DLL

21 Dec 2020, 08:17

Thank you just me. Although that doesn't explain much more than a couple of words.
A few points that are unclear:
-I understand that TimeRemaining decrements. It doesn't increment, instead. Although that is not written anywhere.
- What does it mean a MaxIdlenessAllowed of zero?
- When does TimeRemaining start decrementing?
- Why doesn't it decrement in my cases?
- Is it updated every second/millisecond/15 seconds?
dcdc
Posts: 10
Joined: 07 Feb 2017, 11:19

Re: DllCall to CallNtPowerInformation: cannot find function inside DLL

21 Dec 2020, 12:42

Oh yes, that does help, thanks! I'll need to figure out why my readings are consistently like that, with some tests with the sleep parameters.
Also, I didn't realize that, on a machine that is used as a server, is always on and accessible from the internet, sleep must most probably be disabled and therefore this dll wouldn't help to get the idle time.
Ouch.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], Chunjee, Hansielein, Lpanatt and 326 guests