Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 516
  • Last Modified:

Program timeout

I need in VB5 to read the timout of the screensaver to stop my program. Can anywhere help me?

Thanks
Wolfgang
0
wwberger
Asked:
wwberger
  • 4
  • 3
1 Solution
 
cookreCommented:
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
 
wwbergerAuthor Commented:
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
 
cookreCommented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
wwbergerAuthor Commented:
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
 
cookreCommented:
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
 
wwbergerAuthor Commented:
Hi cookre!

Sorry if I describe my problem so bad.

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

Wolfgang
0
 
cookreCommented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now