Solved

Program timeout

Posted on 2003-10-24
7
468 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Problem to adjust sheet 1 82
copyEndy  challenge 15 58
for loop with Set 4 46
triangle challenge 4 79
I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

743 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

11 Experts available now in Live!

Get 1:1 Help Now