Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 448
oO. Avatar asked Sep 15 '09 23:09

oO.


1 Answers

Putting together the cmd and vbs script ideas with the code from the answer to Launch System Screensaver from C# Windows Form I came up with the following:

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);

    [DllImport("user32.dll", EntryPoint = "LockWorkStation")]
    private static extern IntPtr LockWorkStation();

    private const int SC_SCREENSAVE = 0xF140;
    private const int WM_SYSCOMMAND = 0x0112;

    public static void SetScreenSaverRunning()
    {
        SendMessage(GetDesktopWindow(), WM_SYSCOMMAND, SC_SCREENSAVE, 0);
        LockWorkStation();
    }

    public static void Main()
    {
        LockDesktop.SetScreenSaverRunning();
    }
}

To build it, install the .NET Framework, copy and paste the above code into lock.cs, then run:

%SystemRoot%\Microsoft.NET\Framework\v3.5\csc.exe lock.cs

Put the created lock.exe in your path, after that, typing lock should engage the configured screen saver and lock your workstation.

like image 176
Grant Wagner Avatar answered Oct 20 '22 21:10

Grant Wagner