How to lock workstation programmatically? (C#)

by Vitaly Zayko 13. January 2009 15:48
There is no such .NET function (or at least I didn’t find). Fortunately we can use PInvoke instead.
Use this function to lock workstation’s display to protect from unauthorized use. Result of this function is the same as pressing Ctrl+Alt+Del.
  1. Add namespace at the top of your code:
    	using System.Runtime.InteropServices;
    	
  2. Declare PInvoke:
    	[DllImport("user32.dll", SetLastError = true)]
    	
    	static extern bool LockWorkStation();
    	
  3. Call this function like this:
    	if (!LockWorkStation())
    	
    	   throw new Win32Exception(Marshal.GetLastWin32Error());
    	
    	
    	
Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Tags:

Code Snippets

Comments are closed

Calendar

<<  September 2010  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

View posts in large calendar

About the author

Vitaly Zayko

Senior Software Developer / Team Lead / Product Manager

Moscow, Russia