windowscommandscreensaver

How to turn screensaver on (windows 7) by a code (in cmd)?


How to turn screensaver on (windows 7) by a code (in cmd)?


Solution

  • using System;
    using System.Runtime.InteropServices;
    
    public static class LockDesktop
    {
        [DllImport("user32.dll", EntryPoint = "GetDesktopWindow")]
        private static extern IntPtr GetDesktopWindow();
    
        [DllImport("user32.dll")]
        private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
    
        private const int SC_SCREENSAVE = 0xF140;
        private const int WM_SYSCOMMAND = 0x0112;
    
        public static void SetScreenSaverRunning()
        {
            SendMessage(GetDesktopWindow(), WM_SYSCOMMAND, SC_SCREENSAVE, 0);
        }
    
        public static void Main()
        {
            LockDesktop.SetScreenSaverRunning();
        }
    }
    

    This works - only downside is that u cant interact with pc for something like 7 sec, but i guess its 7's to give ppl time before making screensaver 'permanent'.