Solved

detect screensaver in vb.net

Posted on 2009-04-06
2
1,073 Views
Last Modified: 2012-05-06
Hi

I'm using VB.NET 2008 & WinForms. I need a function that returns True if the system screensaver is running.

I have tried this:

Private Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" (ByVal uAction As Integer, ByVal uParam As Integer, ByVal lpvParam As Integer, ByVal fuWinIni As Integer) As Integer

    Public Shared Function IsScreenSaverRunning() As Boolean
        Dim Running As Integer
        Const SPI_GETSCREENSAVEACTIVE As Integer = 16
        SystemParametersInfo(SPI_GETSCREENSAVEACTIVE, 0, Running, 0)
        Return Running
    End Function

But SystemParametersInfo returns an error value and after callin GetLastError I ger error 998 (Invalid access to memory location).

I guess the problem is with the API calling / passing parameters.

Any ideas?

Thanks
0
Comment
Question by:rettiseert
2 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 24083507
This worked for me...

Note that I'm using SPI_GETSCREENSAVERRUNNING and the third parameter is being passed ByRef instead of ByVal:
Public Class Form1
 

    Private Const SPI_GETSCREENSAVERRUNNING As Integer = &H72

    Private Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" _

        (ByVal uiAction As Integer, ByVal uiParam As Integer, ByRef pvParam As Integer, ByVal fWInIni As Integer) As Boolean
 

    Public Shared Function IsScreenSaverRunning() As Boolean

        Dim bRunning As Boolean

        SystemParametersInfo(SPI_GETSCREENSAVERRUNNING, 0, bRunning, False)

        Return bRunning

    End Function
 

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        If Form1.IsScreenSaverRunning Then

            My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Asterisk)

        End If

    End Sub
 

End Class

Open in new window

0
 
LVL 13

Author Closing Comment

by:rettiseert
ID: 31567311
Wonderful, thank you.
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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 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