Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lock Windows workstation programmatically in C#

I ran into this example for locking Windows workstation:

using System.Runtime.InteropServices;
...
[DllImport("user32.dll", SetLastError = true)]
static extern bool LockWorkStation();

...
if (!LockWorkStation())
    throw new Win32Exception(Marshal.GetLastWin32Error()); // or any other thing

Is there a pure managed alternative to this snippet? Namely, without P-Invoke.

like image 247
Ron Klein Avatar asked Aug 11 '09 21:08

Ron Klein


People also ask

How do I lock a Windows workstation?

To lock a Windows workstation press CTRL + ALT + DEL and choose "Lock" from the list. To resume work, log back in with your password. Alternatively you can use the keyboard shortcut Windows button + L, which immediately locks the workstation.

What is the quickest way to lock your Windows workstation?

Using the Keyboard: Press Ctrl, Alt and Del at the same time. Then, select Lock from the options that appear on the screen.


1 Answers

No there is not. This is the best way to achieve this action.

Even if it was provided in the BCL, its implementation would almost certainly be identical to your sample. It's not something the CLR would natively implement.

like image 92
JaredPar Avatar answered Oct 08 '22 09:10

JaredPar