• 0

[C#] Enable/ disable screen saver


Question

Hello,

I'm trying to use SystemParametersInfo in order to disable/ enable the screen saver. I try this:

[DllImport("user32", CharSet=CharSet.Auto)]
  	private static extern bool SystemParametersInfo(int uAction, bool uParam, int lpvParam, int fuWinIni);
  
  [DllImport("user32", CharSet=CharSet.Auto)]
  	private static extern bool SystemParametersInfo(int uAction, int uParam, bool lpvParam, int fuWinIni);

private const int HWND_BROADCAST = 65535;
  private const int WM_SYSCOMMAND = 274;
  private const int SC_SCREENSAVE = 61760;
  private const int SPI_GETSCREENSAVETIMEOUT = 14;
 private const int SPI_SETSCREENSAVETIMEOUT = 15;
 private const int SPIF_UPDATEINIFILE = 1;
 private const int SPIF_SENDWININICHANGE = 2;
 private const int SPI_SETSCREENSAVEACTIVE = 17;
private const int SPI_GETSCREENSAVEACTIVE = 16;

bool IsActive;

SystemParametersInfo(SPI_GETSCREENSAVEACTIVE, 0, IsActive, 0);
// disable the screen saver if it is enabled
if(IsActive)
{
SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, false, 0, 0);
}

But it doesn't work :( I'm not sure that the SystemParametersInfo is OK, and I don't know how to use SPI_GETSCREENSAVEACTIVE and SPI_SETSCREENSAVEACTIVE.

Can anybody please tell me how to use it ?

Link to comment
https://www.neowin.net/forum/topic/298659-c-enable-disable-screen-saver/
Share on other sites

9 answers to this question

Recommended Posts

  • 0

I use this code but in VB.net, try to convert it to C# ;)

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 Function ToggleScreenSaverActive(ByRef Active As Boolean) As Boolean
        Dim lActiveFlag As Integer
        Dim retvaL As Integer

        lActiveFlag = IIf(Active, 1, 0)
        retvaL = SystemParametersInfo(17, lActiveFlag, 0, 0)
        ToggleScreenSaverActive = retvaL > 0
    End Function


    ' On
        ToggleScreenSaverActive(True)


    ' Off
        ToggleScreenSaverActive(False)

  • 0
  yyy said:
I know how to do that - it works fine. This is not the problem.

My problem is that I want to GET the screen saver state so I'll know whether the screen saver is active or not.

For that I need to use SPI_GETSCREENSAVEACTIVE

Do you know how to use that ?

585642321[/snapback]

ok change it to the line :

Private Const SPI_GETSCREENSAVEACTIVE As Long = &H10

and give a try

  • 0

No - I know that too. Nevermind - I've found the solution, I think.

I did it that way:

[DllImport("user32", CharSet=CharSet.Auto)]
 ?private static extern bool SystemParametersInfo(int uAction, int uParam, ref bool lpvParam, int fuWinIni

SystemParametersInfo(SPI_GETSCREENSAVEACTIVE, 0, ref IsActive, 0);

I had to add the ref before the bool to cause it get the value.

Thanks anyway :DD

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Perplexity starts testing its Comet AI browser on Windows by Usama Jawad AI is all the rage these days with Microsoft heavily pushing Copilot, Google investing its efforts in Gemini and Veo, Apple still figuring out Apple Intelligence, and OpenAI continuing to dominate the headlines. Now, another player in this space, Perplexity, has decided that it wants a bigger chunk of the market too. We already know that Perplexity is interested in the browser market. In May, it rolled out a version of its AI browser, Comet, exclusively to Mac customers running Apple Silicon. As expected, this piece of software features AI capabilities front and center, including the ability to engage in conversations with the AI model in natural language, surface emails that you haven't responded to, and a "Try on" feature that enables users to upload a picture of themselves and then see AI-generated images of themselves with different pieces of clothing, just to get an idea of how aesthetic they could look if they wear certain garments. Now, Perplexity CEO Aravind Srinivas has posted on X (formerly Twitter) that testing for the Windows version of Comet has now begun: Now that the testing for Comet on Windows has begun with a handful of testers, it will be interesting to see when the browser will roll out generally to customers and what feature set will eventually become available for the masses. That said, it is important to note that AI browser is not a novel concept; Opera Neon was recently announced as the first "AI agentic" browser. On a side note, Perplexity has been in hot waters recently. It received backlash from people after it revealed plans to find out everything its customers do online so that it can present them hyper-personalized ads. UK news outlet BBC also threatened the firm with legal action over its content scraping practices. Via: Engagdet
    • probably because these will be a minority compared to our general tech coverage, plus these stories, their premises do seem quite interesting.
    • Will Indian government upgrade my PC to Windows 11 if it's not supported? If they can't they should've mentioned alternatives as well.
  • Recent Achievements

    • One Year In
      78andyp earned a badge
      One Year In
    • One Month Later
      Cottonbud earned a badge
      One Month Later
    • One Month Later
      langat earned a badge
      One Month Later
    • One Month Later
      Nullun earned a badge
      One Month Later
    • Week One Done
      moojay67 earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      598
    2. 2
      ATLien_0
      219
    3. 3
      Michael Scrip
      197
    4. 4
      +FloatingFatMan
      144
    5. 5
      Xenon
      135
  • Tell a friend

    Love Neowin? Tell a friend!