Solved

How To Read The Screensaver Timer

Posted on 2006-07-03
7
220 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
ID: 17034384
0
 
LVL 29

Assisted Solution

by:nffvrxqgrcfqvvc
nffvrxqgrcfqvvc earned 100 total points
ID: 17036546
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
ID: 17039345
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:s1m0ne
ID: 17125621
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
ID: 17265471
Still working on it ...
0
 
LVL 1

Author Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

863 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

28 Experts available now in Live!

Get 1:1 Help Now