Solved

How To Read The Screensaver Timer

Posted on 2006-07-03
7
219 Views
Last Modified: 2008-02-01
Hello,

In VB 6, running under Windows 2000 or XP, how can one read the timer that the screensaver uses to determine how long it has been since the last user-generated activity. I realize a VB program could have its own timer, but ideally this program would not need to be running constantly, as a screensaver would. Rather it could execute, report (e.g. write to a log file), and close, whether running from the task scheduler, or remotely.

If the same could be accomplished with a .bat file, without resorting to VB, that would be just as good, if not better.

Thanks in advance,

s1m0ne
0
Comment
Question by:s1m0ne
7 Comments
 
LVL 76

Accepted Solution

by:
David Lee earned 100 total points
Comment Utility
0
 
LVL 29

Assisted Solution

by:nffvrxqgrcfqvvc
nffvrxqgrcfqvvc earned 100 total points
Comment Utility
Another approach is to get the screen saver timeout value. Once your armed with this value you can then use BeginIdleDetection API.  Since SystemParametersInfo API gives the screen saver timeout in seconds and BeginIdleDetection uses minutes we need to simply convert the seconds to minutes then pass it to our procedure. You might be notified 2-3 seconds early about when the screen saver is about to run, this is simply due to the messaging going on in the background. Basically as you pass the minute into the procedure it will detect when you become idle and wait for that duration. Once the duration is up it singnals the callback procedure. This callback fires once per action meaning it will poll endlessley about when you become idle and when you become active again, each time reseting itself.

'-------- Module1.bas ----------

Option Explicit

'Msidle define
Private Const USER_IDLE_BEGIN = 1
Private Const USER_IDLE_END = 2

'SystemParametersInfo define
Private Const SPI_GETSCREENSAVETIMEOUT = 14

Private Declare Function BeginIdleDetection Lib "Msidle.dll" Alias "#3" ( _
    ByVal pfnCallback As Long, _
    ByVal dwIdleMin As Long, _
    ByVal dwReserved As Long) As Long

Private Declare Function EndIdleDetection Lib "Msidle.dll" Alias "#4" ( _
    ByVal dwReserved As Long) As Long

Private Declare Function SystemParametersInfoA Lib "user32" ( _
    ByVal uAction As Long, _
    ByVal uParam As Long, _
    ByRef lpvParam As Any, _
    ByVal fuWinIni As Long) As Long

Public Function ScreenSaverTime() As Long
    Dim SvrSeconds  As Long
    SystemParametersInfoA SPI_GETSCREENSAVETIMEOUT, 0&, SvrSeconds, 0&
    ScreenSaverTime = SvrSeconds
End Function

Public Sub BeginIdle(IdleMin As Long)
    If BeginIdleDetection(AddressOf IdleCallBack, IdleMin, 0&) = 0 Then
        Debug.Print "Success - BeginIdleDetection"
    End If
End Sub

Public Sub EndIdle()
    If EndIdleDetection(0&) Then
        Debug.Print "Success - EndIdleDetection"
    End If
End Sub

Public Sub IdleCallBack(ByVal dwState As Long)
    Select Case dwState
        Case USER_IDLE_BEGIN
           Form1.List1.AddItem "Screen saver activated ---  " & Time
        Case USER_IDLE_END
           Form1.List1.AddItem "Screen saver deactivated ---  " & Time
    End Select
End Sub



'------- Form1 --------

'Add 1 Listbox
'Add 1 Command button
'When you close the form close it by X button and not the VB IDE stop button.

Option Explicit

Private Sub Command1_Click()
   
    Dim arg As Long
   
    arg = ScreenSaverTime   ' get screen saver run time
    arg = (arg / 60)        ' seconds to minutes for idle
    BeginIdle arg           ' set up the callback to let us know
   
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    EndIdle                 ' stop callback and idle detection
End Sub
0
 
LVL 41

Expert Comment

by:graye
Comment Utility
I'd agree that the GetLastInputInfo API is the way to go, but you won't be able to do this remotely... it must be run from the local PC
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 1

Author Comment

by:s1m0ne
Comment Utility
Thanks. Sorry for the (continuing) delay. I got the first example working. I'll try to do the second one in the next couple days, for comparison. Ideally, something like this could be run as a service, but that may be beyond the scope of this question.
0
 
LVL 1

Author Comment

by:s1m0ne
Comment Utility
Still working on it ...
0
 
LVL 1

Author Comment

by:s1m0ne
Comment Utility
Sorry for letting this drag on so long. I'm splitting the points evenly between BlueDevilFan and egl1044. Thanks to all.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now