SystemParametersInfo and SPI_SETSCREENSAVERACTIVE problem RRS feed

  • Question





    I'm trying to use a c# app to force a screen saver to display the login screen on resuming.  I'd like this to work under XP, Vista, and 7, but would settle for just 7 at this point.  I'm using Visual Studio 2010 with .Net 4.  Here's the code snippet:


    class Program
            [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            public static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);
            [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            public static extern int SystemParametersInfo(int uAction, bool uParam, string lpvParam, int fuWinIni);
            public const int SPI_GETSCREENSAVETIMEOUT = 0x000E;
            public const int SPI_SETSCREENSAVETIMEOUT = 0x000F;
            public const int SPI_SETSCREENSAVERSECURE = 0x0077;
            public const int SPI_SETSCREENSAVERACTIVE = 0x0011;
            public const int WM_SETTINGCHANGE = 0x001A;

            static void Main(string[] args)

                    SystemParametersInfo(SPI_SETSCREENSAVERSECURE, true, null, WM_SETTINGCHANGE);
                    SystemParametersInfo(SPI_SETSCREENSAVERACTIVE, 1, null, WM_SETTINGCHANGE);

    What happens is that if the box is not checked, this code correctly checks the box to require a login.  But the user is never actually prompted.  The screen saver comes on at the right time, but even if you let the screen saver run for a few minutes, you are never prompted for login credentials.  If you look under Personalize | Screen saver, the box is checked correctly, so it is doing that.  If the box was already checked before running the app, then you are still prompted for credentials, so it doesn't actively break anything. 

    I thought that WM_SETTINGCHANGE should update Windows.  I know that if screen saver is set to none and I set it to mystify.scr, the second SystemParametersInfo call does set the screen saver as active, so the rest of it works.

    Any ideas?




    Monday, July 26, 2010 7:25 PM


  • Hi,

    If your machine is in a domain, you may want to look at the ScreenSaverIsSecure setting in the system registry. Under Win7, it's under HKEY_CURRENT_USER\Control Panel\Desktop\ScreenSaverIsSecure (you may also need to look under HKEY_CURRENT_USER\Software\Policies\Microsoft\Windows\Control Panel\Desktop\ScreenSaverIsSecure).

    Setting this to a 1 will enable (check) the "On resume, show Welcome screen" option, and setting to a 0 will disable (uncheck) it. Or programmatically change this value.

    Or programmatically change this value.

    I have also tried the code you listed which is working fine on my windows vista machine. The machine is not in any domain.



    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Bin-ze Zhao Monday, August 2, 2010 6:35 AM
    Thursday, July 29, 2010 10:46 AM