Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / C#
Tip/Trick

User32.dll - Lock the computer

Rate me:
Please Sign up or sign in to vote.
4.80/5 (19 votes)
21 Apr 2014CPOL2 min read 48.1K   733   31   10
A short example how you can programmatically lock the computer using user32.dll.

Introduction

I am quite certain that there is a use case when a program needs to lock the screen, for example in cooked embedded environments. If this matches your requirements, or you just want to play a prank by randomly locking someone's computer, this short reference is what you are looking for.

Background - What is user32.dll

User32.dll implements the Windows USER component which is responsible for creating and manipulating of the Windows user elements. User elements of Windows are the desktop, windows and menus. Most of the programs which have a Windows look and feel are using user32.dll in the background. Many of the functions in user32.dll call GDI functions provided by gdi32.dll to make the rendering of the many of the elements of the user interface. Some programs are also calling the GDI functions directly to perform lower-level operations within a previously by user32 created window.

The Windows USER component performs actions such as creating and managing windows, receiving window messages, displaying message boxes and displaying text in a window.

Using the code

The bad way would be to just start rundll32.exe with the arguments user32.dll, LockWorkStation:

C#
Process.Start(@"C:\WINDOWS\system32\rundll32.exe", "user32.dll,LockWorkStation"); 

This way of solving the problem is considered being a rather bad way, since the path to rundll32.exe is hard-coded in your program's code. As soon as Microsoft changes the path, an update of your program is needed and may leave your program unusable.

A much better way is to forward-declare the method and telling the program where it can find it:

C#
[DllImport("user32.dll")]
public static extern void LockWorkStation(); 

As you can see there is no absolute path left in the program code, making your program better maintainable and understandable. You can now call the function directly from your program code, without explicitly launching another process (I wrote a short console program for demonstration purposes):

C#
 static void Main(string[] args)
{
    LockWorkStation();
}

The whole program is using System.Runtime.InteropServices in the end and looks like this:

C#
using System.Runtime.InteropServices;
namespace LockComputer
{
    class Program
    {
        [DllImport("user32.dll")]
        public static extern void LockWorkStation();
        static void Main(string[] args)
        {
            LockWorkStation();
        }
    }
}

Points of Interest

Many people underestimate the pure power Microsoft Windows provides with their various API's, and therefore prefer Linux. It is true that you can't fully customize your Windows, but from a programmer's point of view you can do close to anything if you got a deep knowledge of what windows is 'under the hood'. If I got spare time, I am going to dig deeper into this interesting topic.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionAnd to unlock? Pin
Antonio Blescia23-Apr-14 2:05
Antonio Blescia23-Apr-14 2:05 
AnswerRe: And to unlock? Pin
Marco Bertschi23-Apr-14 2:30
protectorMarco Bertschi23-Apr-14 2:30 
GeneralMy vote of 5 Pin
wangfnso22-Apr-14 0:02
wangfnso22-Apr-14 0:02 
GeneralRe: My vote of 5 Pin
Marco Bertschi23-Apr-14 3:27
protectorMarco Bertschi23-Apr-14 3:27 
GeneralMy vote of 5 Pin
Volynsky Alex2-Apr-14 5:15
professionalVolynsky Alex2-Apr-14 5:15 
Nice example how to programmatically lock the computer using user32.dll!
GeneralRe: My vote of 5 Pin
Marco Bertschi2-Apr-14 23:06
protectorMarco Bertschi2-Apr-14 23:06 
GeneralRe: My vote of 5 Pin
Volynsky Alex2-Apr-14 23:23
professionalVolynsky Alex2-Apr-14 23:23 
Generala little format issue Pin
Southmountain23-Jan-14 4:59
Southmountain23-Jan-14 4:59 
GeneralRe: a little format issue Pin
Marco Bertschi23-Jan-14 6:02
protectorMarco Bertschi23-Jan-14 6:02 
GeneralFixed Pin
Marco Bertschi23-Jan-14 6:34
protectorMarco Bertschi23-Jan-14 6:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.