Chapter 72. How to Programmatically Lock the Console

Have you ever configured your screensaver to require a password or, on Windows XP, to “show the Welcome screen”? I've seen some special-purpose programs that need to implement similar behavior: locking the interactive console under certain conditions. It's easy to do, so I've included it here. The function you need to call is in Win32, and it's called LockWorkstation. Here's a C# program that locks the console when run:

using System.Runtime.InteropServices;

class LockItUp {
  static void Main() {
    LockWorkStation();
  }
  [DllImport("user32.dll")]
  static extern void LockWorkStation();
}

After this program runs, the default desktop will be hidden and the Winlogon desktop will be displayed. All programs ...

Get The .NET Developer's Guide to Windows Security now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.