Solved

Program timeout

Posted on 2003-10-24
7
497 Views
Last Modified: 2010-04-17
I need in VB5 to read the timout of the screensaver to stop my program. Can anywhere help me?

Thanks
Wolfgang
0
Comment
Question by:wwberger
  • 4
  • 3
7 Comments
 
LVL 22

Expert Comment

by:cookre
ID: 9617900
The screen saver timeout value is held in the registry in
HKEY_CURRENT_USER\Control Panel\Desktop
value ScreenSaveTimeOut

To read it in VB:

Add these declarations:
Public Const ERROR_NONE = 0
Public Const REG_SZ As Long = 1
Declare Function RegOpenKeyEx Lib "advapi32.dll" _
                 Alias "RegOpenKeyExA" _
                 (ByVal hKey As Long _
                 , ByVal lpSubKey As String _
                 , ByVal ulOptions As Long _
                 , ByVal samDesired As Long _
                 , phkResult As Long) _
                 As Long

Declare Function RegQueryValueEx Lib "advapi32.dll" _
                 Alias "RegQueryValueExA" _
                 (ByVal hKey As Long _
                 , ByVal lpValueName As String _
                 , ByVal lpReserved As Long _
                 , lpType As Long _
                 , ByVal lpData As String _
                 , lpcbData As Long) _
                 As Long


Declare Function RegCloseKey Lib "advapi32.dll" _
                 (ByVal hKey As Long) _
                 As Long



And here's the code:

Dim RC As Long
Dim hKey As Long
Dim TimeOutString as String

RC = RegOpenKeyEx(HKEY_CURRENT_USER _
              , "Control Panel\Desktop" _
              , 0, KEY_ALL_ACCESS, hKey)
If RC <> ERROR_NONE Then
   ...oopsie
   End If

TimeOutString=String(10," ")
RC = RegQueryValueEx(hKey, "ScreenSaveTimeOut", 0, REG_SZ _
                   , TimeOutString, 10)
If RC <> ERROR_NONE Then
   ...oopsie
   End If
     
RegCloseKey (hKey)
0
 

Author Comment

by:wwberger
ID: 9617965
Thank you for the prompt answer, but if I try this  I get an error:

KEY_ALL_ACCESS .... variable not defined!

Can you help me?

Thanks
Wolfgang
0
 
LVL 22

Expert Comment

by:cookre
ID: 9618243
Oops, I missed some.  Add these up top after the other Public Consts but before the function declaration of RegOpenKeyEx:

Public Const SYNCHRONIZE = &H100000
Public Const STANDARD_RIGHTS_ALL = &H1F0000
Public Const KEY_QUERY_VALUE = &H1
Public Const KEY_SET_VALUE = &H2
Public Const KEY_CREATE_SUB_KEY = &H4
Public Const KEY_ENUMERATE_SUB_KEYS = &H8
Public Const KEY_NOTIFY = &H10
Public Const KEY_CREATE_LINK = &H20
Public Const KEY_ALL_ACCESS = (    (    STANDARD_RIGHTS_ALL _
                                                         Or KEY_QUERY_VALUE _
                                                         Or KEY_SET_VALUE _
                                                         Or KEY_CREATE_SUB_KEY _
                                                         Or KEY_ENUMERATE_SUB_KEYS _
                                                         Or KEY_NOTIFY _
                                                         Or KEY_CREATE_LINK _
                                                         ) _
                                                        And (Not SYNCHRONIZE))

0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:wwberger
ID: 9619112
Hi cookre!

Thanks for your answer. Now I try this code in a timer routine like this:

Private Sub Timer1_Timer()
Dim RC As Long
Dim hKey As Long
Dim TimeOutString As String

RC = RegOpenKeyEx(HKEY_CURRENT_USER _
              , "Control Panel\Desktop" _
              , 0, KEY_ALL_ACCESS, hKey)
If RC <> ERROR_NONE Then
   MsgBox "Screensaver"
   End If
Debug.Print RC
TimeOutString = String(10, " ")
RC = RegQueryValueEx(hKey, "ScreenSaveTimeOut", 0, REG_SZ _
                   , TimeOutString, 10)
Debug.Print RC
If RC <> ERROR_NONE Then
   MsgBox "Screensaver"
   End If
     
RegCloseKey (hKey)

End Sub

But I get always zero in the debug screen. This is the same when the screensaver ist started. I used W2k as operating system. this routine will also run on NT4 and XP.

Thanks for your help
Wolfgang
0
 
LVL 22

Expert Comment

by:cookre
ID: 9619527
It appears I misunderstood what you asked for.

The code I posted just retrieves the screen saver timeout period from the registry and places it in variable 'TimeOutString'.

As such, you should always get zeros in RC, since that means the API calls were successful.

What I apparently don't understand is how you want to use that timeout value.

Do you want your program to terminate if the screen save becomes active?  

Do you want your program to terminate just before the screen save becomes active?  
0
 

Author Comment

by:wwberger
ID: 9620064
Hi cookre!

Sorry if I describe my problem so bad.

I want to terminte my program when the screen save becomes active!

Wolfgang
0
 
LVL 22

Accepted Solution

by:
cookre earned 250 total points
ID: 9620190
Private Declare Function SystemParametersInfo Lib "user32" _
           Alias "SystemParametersInfoA" _
           (ByVal uAction As Long _
           ,ByVal uParam As Long _
           ,lpvParam As Any _
           ,ByVal fuWinIni As Long) _
           As Long
Private Const SPI_GETSCREENSAVEACTIVE = &H10
Private Const SPI_GETSCREENSAVERRUNNING = &H72


' Call this function to determine if the screen save has been enabled
Private Function IsSSEnabled() As Boolean
Dim TempBool As Boolean
TempBool = False
call SystemParametersInfo(SPI_GETSCREENSAVEACTIVE,0,TempBool,0)
IsSSEnabled=TempBool
End Function

' Call this to determine if the screen save is running - i.e. if it is displayed
Private Function IsSSRunning() As Boolean
Dim TempBool As Boolean
TempBool=False
call SystemParametersInfo(SPI_GETSCREENSAVERRUNNING,0,TempBool,0)
IsSSRunning=TempBool
End Function


Place calls to those subroutines in your timer handler:

Private Sub Timer1_Timer()
' Temporarily disable timer so we don't interrupt ourselves
Timer1.Enabled=false
if IsSSEnabled() then
   if IsSSRunning() then
      ' Your program shutdown code goes here
     end if
   end if
'Re-enable timer
Timer1.Enabled=true
end

Ganz einfach, oder?
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
A short article about problems I had with the new location API and permissions in Marshmallow
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

860 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