Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

detect screensaver in vb.net

Posted on 2009-04-06
2
1,093 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

790 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