How to turn screensaver on (windows 7) by a code (in cmd)?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With