How to convert dwmsEventTime genareted by SetWinEventHook API

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Archimede
Posts: 464
Joined: 25 Nov 2021, 09:49
Location: Switzerland / Italy

How to convert dwmsEventTime genareted by SetWinEventHook API

Post by Archimede » 30 Nov 2022, 12:57

Hallo.
I use the DLL call SetWinEventHook that generates a process call with many arguments; one of that is dwmsEventTime that specifies the time, in milliseconds, that the event was generated.
I need to convert it to date and time: how is it possible?
Thank you very much

User avatar
mikeyww
Posts: 26600
Joined: 09 Sep 2014, 18:38

Re: How to convert dwmsEventTime genareted by SetWinEventHook API

Post by mikeyww » 30 Nov 2022, 20:06

Code: Select all

MsgBox, 64, Event time, % time(A_TickCount)

time(dwmsEventTime) {
 eventTime += (dwmsEventTime - A_TickCount) / 1000, S
 FormatTime, tm, %eventTime%, MM/dd/yyyy HH:mm:ss
 Return tm
}

Archimede
Posts: 464
Joined: 25 Nov 2021, 09:49
Location: Switzerland / Italy

Re: How to convert dwmsEventTime genareted by SetWinEventHook API

Post by Archimede » 30 Nov 2022, 21:22

Interesting... I will try it.
I red FormatTime help: it no tell anything about conversion from mS or nS format... Can you tell me something about it?

Why the first line into the time(...) function is eventTime +=... instead eventTime :=... ?

User avatar
mikeyww
Posts: 26600
Joined: 09 Sep 2014, 18:38

Re: How to convert dwmsEventTime genareted by SetWinEventHook API

Post by mikeyww » 30 Nov 2022, 22:09

See :arrow: EnvAdd, which addresses your questions.
TimeUnits: If present, this parameter directs the command to add Value to Var, treating Var as a date-time stamp in the YYYYMMDDHH24MISS format and treating Value as the integer or floating point number of units to add (specify a negative number to perform subtraction). TimeUnits can be either Seconds, Minutes, Hours, or Days (or just the first letter of each of these). If Var is an empty variable, the current time will be used in its place.
The idea of this function ("time", in the script) is that we know two things:

1. How much time has elapsed since the event time

AND

2. The current time

Therefore, we can compute the event time, which is the difference between these two. In other words, if we subtract the difference (i.e., elapsed time) from the current time, we will get the event time.

#1, in milliseconds, is equal to the current tick count minus the dwmsEventTime. The number of elapsed seconds is this number divided by 1000.

We now want to subtract this number of seconds from the current time. We can use += to add a number to a variable's value. If the variable contains a timestamp and we use , S as a final parameter, the result will be a new timestamp, incremented by the number of seconds specified. If the variable is null while , S is used, then the variable is assumed to contain the current time before the operation occurs. To subtract the correct number of seconds from the current time, we thus use += to add the negative of this number to the current time.

FormatTime does nothing more than convert a timestamp into a more readable format.

The bottom line is that the timestamp of the event time is computed in one line.

Code: Select all

 eventTime += (dwmsEventTime - A_TickCount) / 1000, S
This adds the negative of the elapsed time to the current time.

teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to convert dwmsEventTime genareted by SetWinEventHook API

Post by teadrinker » 01 Dec 2022, 01:22

mikeyww wrote: The bottom line is that the timestamp of the event time is computed in one line.
I can compute it in one word: A_Now ;)

Archimede
Posts: 464
Joined: 25 Nov 2021, 09:49
Location: Switzerland / Italy

Re: How to convert dwmsEventTime genareted by SetWinEventHook API

Post by Archimede » 01 Dec 2022, 04:00

:-(
I tested it.
It returns a wrong result (date result 07/09/2301, time result wrong ).
I think the problem is dwmsEventTime: I don't know how it is generated, if it contain an absolute value or relative value, and so on.
The API SetWinEventHook generates a function call with 7 arguments, one of that is dwmsEventTime (ms time), but I no found anything about it... :-(

I think one problem is dwmsEventTime is a DWORD dimension but the time var is a WORD dimension ( I'm not sure on it ); this is one example of the dwmsEventTime value (today):
8791804202256

User avatar
mikeyww
Posts: 26600
Joined: 09 Sep 2014, 18:38

Re: How to convert dwmsEventTime genareted by SetWinEventHook API

Post by mikeyww » 01 Dec 2022, 05:57

I've never actually used this value or variable, just assumed that the number is a number of milliseconds since the computer was booted. In other words, I assumed that the number represented the number of milliseconds from boot time to event time. That's what the function does: convert that number into a readable time. You provided no examples of input or output, and I also had none!

The example that I gave in the script shows that if the number of milliseconds from boot time to event time is provided as A_TickCount (which is that number for the current time), then--indeed!-- the value returned is the time given by A_Now, as it should be.

I do see that you're right about the issue. I can have a look at this issue that you raised (and others here may know more about that variable)-- no promises! I suspect that a simple answer is at hand.
Last edited by mikeyww on 01 Dec 2022, 06:20, edited 1 time in total.

teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to convert dwmsEventTime genareted by SetWinEventHook API

Post by teadrinker » 01 Dec 2022, 06:20

A tip:

Code: Select all

MsgBox, % dwmsEventTime & 0xFFFFFFFF . "`n" . A_TickCount

User avatar
mikeyww
Posts: 26600
Joined: 09 Sep 2014, 18:38

Re: How to convert dwmsEventTime genareted by SetWinEventHook API

Post by mikeyww » 01 Dec 2022, 06:21

As they say, DWORDs matter?

Thanks to teadrinker for helping me solve the puzzle! :)

Code: Select all

time(dwmsEventTime) {
 elapsed   := A_TickCount - dwmsEventTime & 0xFFFFFFFF
 eventTime += elapsed / -1000, S
 FormatTime, tm, %eventTime%, MM/dd/yyyy HH:mm:ss
 ToolTip, dwmsEventTime = %dwmsEventTime%`nElapsed = %elapsed% ms`nTime = %tm%
 Return tm
}
image221201-0635-001_cr.png
image221201-0635-001_cr.png (10.17 KiB) Viewed 649 times

It could be part of teadrinker's point that if you are generating dwmsEventTime in a script, then this may actually be the A_TickCount DWORD, in which case you do not need this function and can use FormatTime directly.

Code: Select all

#Include D:\utils\WinEvents.ahk ; https://www.autohotkey.com/boards/viewtopic.php?t=42657
#Persistent
HookEvent("adjusting", [EVENT_SYSTEM_MOVESIZEEND])

adjusting(hHook, event, hwnd, idObject, idChild, dwEventThread, dwmsEventTime) {
 FormatTime, tm,, MM/dd/yyyy HH:mm:ss
 MsgBox, 64, Finished!, %tm%
}
Thus, I think the answer is that line 5 shows how to convert dwmsEventTime into a date and time, as long as you save it quickly! :)

If you had saved the event time to use later, then you can use the time function posted here.
Last edited by mikeyww on 01 Dec 2022, 09:02, edited 1 time in total.

Archimede
Posts: 464
Joined: 25 Nov 2021, 09:49
Location: Switzerland / Italy

Re: How to convert dwmsEventTime genareted by SetWinEventHook API

Post by Archimede » 01 Dec 2022, 08:52

:-)
Interesting... It seem works well.
Please can me explain this line:
eventTime += elapsed / -1000, S
Why divide by -1000 instead 1000?

About this line:
elapsed := A_TickCount - dwmsEventTime & 0xFFFFFFFF
I think is the same like:
elapsed := ( A_TickCount - dwmsEventTime ) & 0xFFFFFFFF
Then, what is the meaning of the operation & 0xFFFFFFFF?
If A_TickCount is a WORD and dwmsEventTime is a DWORD, the ( A_TickCount - dwmsEventTime ) result I think is a DWORD... then what is the meaning of & 0xFFFFFFFF?

User avatar
mikeyww
Posts: 26600
Joined: 09 Sep 2014, 18:38

Re: How to convert dwmsEventTime genareted by SetWinEventHook API

Post by mikeyww » 01 Dec 2022, 09:19

Using a negative number with += will subtract the elapsed time from the current time. This is what is needed to yield the event time.

Perhaps should be:

Code: Select all

time(dwmsEventTime) {
 elapsed   := A_TickCount - (dwmsEventTime & 0xFFFFFFFF)
 eventTime += elapsed / -1000, S
 FormatTime, tm, %eventTime%, MM/dd/yyyy HH:mm:ss
 ToolTip, dwmsEventTime = %dwmsEventTime%`nElapsed = %elapsed% ms`nTime = %tm%
 Return tm
}
I am a novice regarding DWORD, so will need teadrinker or some other smart people, including you, to answer the question as to why the original script did not always work. I suppose that the general answer is here https://stackoverflow.com/questions/4447009/what-does-the-0xffffffff-do-in-this-hidword-macro, though I'm not sure that this actually answers your specific question.
(A_TickCount - dwmsEventTime) result I think is a DWORD
Although this may be true, it seems to me that the final number could be wrong if the highest bits of the event time were actually set. The alternative is that I'm wrong. :) As far as I can tell, the difference between the original script and the revised script shows that my hunch is correct, I think, or more likely that teadrinker was correct! Having a DWORD doesn't mean that the value is the right value.

Archimede
Posts: 464
Joined: 25 Nov 2021, 09:49
Location: Switzerland / Italy

Re: How to convert dwmsEventTime genareted by SetWinEventHook API

Post by Archimede » 01 Dec 2022, 10:11

Ok thank you.
I would like to see mS at the end of the date: what is the right procedure to see that?

teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to convert dwmsEventTime genareted by SetWinEventHook API

Post by teadrinker » 01 Dec 2022, 11:03

mikeyww wrote: why the original script did not always work
DWORD is "double word", 32-bit unsigned integer number, its value is from 0 to 0xFFFFFFFF. "Double word" means that it consists of two 16-bit "words", low word and high word.

dwmsEventTime for some reason has extra bits exceeding 0xFFFFFFFF:

Code: Select all

; 8791804202256 = 0x7FF005DCD10
MsgBox, % Format("0x{:X}", 8791804202256)
Perhaps this is some bug, I don't know.
This operation cuts off extra bits:

Code: Select all

res := 8791804202256 & 0xFFFFFFFF
MsgBox, % res . "`n" . Format("0x{:X}", res)

Archimede
Posts: 464
Joined: 25 Nov 2021, 09:49
Location: Switzerland / Italy

Re: How to convert dwmsEventTime genareted by SetWinEventHook API

Post by Archimede » 01 Dec 2022, 11:32

The binary value of 8791804202256 is:
1111111111100000000010111011100110100010000
...............3......................1....................0
...............1......................5....................0
(sorry for no perfect alignment)
then, the values over 32 bit seems really a bug: the & operation now remove the bug.
The better solution I think could be:
dwmsEventTime &= 0xFFFFFFFF

Do you have any idea about how to obtain mS time for the result?
Last edited by Archimede on 01 Dec 2022, 18:09, edited 1 time in total.

teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to convert dwmsEventTime genareted by SetWinEventHook API

Post by teadrinker » 01 Dec 2022, 12:06

Code: Select all

dwmsEventTime := 8791804202256 ; just for test

ms := mod(dwmsEventTime & 0xFFFFFFFF, 1000)
dateTime := A_Now

FormatTime, tm, dateTime, MM/dd/yyyy HH:mm:ss
tm .= ":" . ms

MsgBox, % tm

teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to convert dwmsEventTime genareted by SetWinEventHook API

Post by teadrinker » 01 Dec 2022, 12:12

Or even ms := mod(dwmsEventTime & 0xFFF, 1000)

User avatar
mikeyww
Posts: 26600
Joined: 09 Sep 2014, 18:38

Re: How to convert dwmsEventTime genareted by SetWinEventHook API

Post by mikeyww » 01 Dec 2022, 12:14

Thanks, teadrinker, for all of that information.


Post Reply

Return to “Ask for Help (v1)”