Consultor Eletrônico



Kbase P168749: How to use Windows API to determine how much time has passed since the last user input?
Autor   Progress Software Corporation - Progress
Acesso   Público
Publicação   29/11/2010
Status: Unverified

GOAL:

How to use Windows API to determine how much time has passed since the last user input?

GOAL:

How to use GetLastInputInfo windows API ?

FACT(s) (Environment):

Windows
OpenEdge 10.x

FIX:

The GetLastInputInfo windows API returns the clock tick when the last user input (keyboard or mouse event) was received within the Windows session. This can be used to detect for example if a user has left his Windows desktop for N amount of time, and take appropriate action. (Re-ask for authentication, shut down the application, etc.)

The following code is an example how to call this API from within the ABL.

define variable iTick as int64 no-undo.
define variable iResult as INTEGER no-undo.
DEFINE VARIABLE mLASTINPUTINFO AS MEMPTR NO-UNDO.
DEFINE VARIABLE iTest AS INTEGER NO-UNDO.

/*
GetLastInputInfo
BOOL WINAPI GetLastInputInfo(
__out PLASTINPUTINFO plii
);
Parameters"
plii [out] : PLASTINPUTINFO
A pointer to a LASTINPUTINFO structure that receives the time of the last input event.
------
LASTINPUTINFO

typedef struct tagLASTINPUTINFO {
UINT cbSize;
DWORD dwTime;
} LASTINPUTINFO, *PLASTINPUTINFO;

Members
cbSize : UINT
The size of the structure, in bytes. This member must be set to sizeof(LASTINPUTINFO).

dwTime : DWORD
The tick count when the last input event was received.
*/

/*
Build the structure. Since 32-bit libraries are used, both UINT and DWORD end up
being 32-bit integers. This gives a total size of 8 bytes.
This may be different for 64-bit platforms!

Note that as documented above, for the structure to be handled correctly we need
to store the size of the structure inside the structure.
*/

SET-SIZE(mLASTINPUTINFO) = 8.
PUT-LONG(mLASTINPUTINFO,1) = 8.

DO WHILE iTest < 5:
PAUSE 3.
itest = itest + 1.

RUN GetLastInputInfo (output mLASTINPUTINFO, OUTPUT iResult).
IF iResult = 0 THEN DO:
/* as per MSDN documentation:
If the function fails, the return value is zero
*/
MESSAGE "GetLastInputInfo failed".
END.
ELSE DO:
iTick = GET-LONG(mLASTINPUTINFO,5).
message "Last Event timestamp:" iTick.
END.
END.

procedure GetLastInputInfo external "user32.dll":
DEFINE OUTPUT PARAMETER piTime as HANDLE TO memptr. /* the API expects a pointer, so use HANDLE TO syntax to pass one */
DEFINE RETURN PARAMETER piResult as LONG.
end.