Click here to Skip to main content
15,124,187 members
Articles / Programming Languages / C#
Posted 4 Dec 2013

Tagged as


32 bookmarked

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
A short example how you can programmatically lock the computer using user32.dll.


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:

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:

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):

 static void Main(string[] args)

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

using System.Runtime.InteropServices;
namespace LockComputer
    class Program
        public static extern void LockWorkStation();
        static void Main(string[] args)

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.


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


About the Author

Marco Bertschi
Software Developer
Switzerland Switzerland
I'm a young Software Engineer. Whilst I may accept a 'status quo' as given, it's my responsibility to improve it as soon as the opportunity arises.
Very familiar with the .Net framework since it has been the underlying foundation for most of my professional work so far. I'm that kind of guy who gets stuff done, while I may also improve the way things work whilst I'm at it. Generally can get along well with most people I meet as a professional.
Fervent volunteer firefigter, captain-ranked air force militia spec officer.
Great friend and generally nice to have me around - For a talk, a beer, or a bonfire (I might as well do all of those things combined). Oh, and I really love music - Rock & country that is, mostly.

Comments and Discussions

QuestionAnd to unlock? Pin
Antonio Blescia23-Apr-14 3:05
MemberAntonio Blescia23-Apr-14 3:05 
AnswerRe: And to unlock? Pin
Marco Bertschi23-Apr-14 3:30
professionalMarco Bertschi23-Apr-14 3:30 
GeneralMy vote of 5 Pin
wangfnso22-Apr-14 1:02
Memberwangfnso22-Apr-14 1:02 
GeneralRe: My vote of 5 Pin
Marco Bertschi23-Apr-14 4:27
professionalMarco Bertschi23-Apr-14 4:27 
GeneralMy vote of 5 Pin
Volynsky Alex2-Apr-14 6:15
professionalVolynsky Alex2-Apr-14 6:15 
GeneralRe: My vote of 5 Pin
Marco Bertschi3-Apr-14 0:06
professionalMarco Bertschi3-Apr-14 0:06 
GeneralRe: My vote of 5 Pin
Volynsky Alex3-Apr-14 0:23
professionalVolynsky Alex3-Apr-14 0:23 
Generala little format issue Pin
Southmountain23-Jan-14 5:59
MemberSouthmountain23-Jan-14 5:59 
GeneralRe: a little format issue Pin
Marco Bertschi23-Jan-14 7:02
professionalMarco Bertschi23-Jan-14 7:02 
GeneralFixed Pin
Marco Bertschi23-Jan-14 7:34
professionalMarco Bertschi23-Jan-14 7: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.