Click here to Skip to main content
11,490,642 members (69,047 online)
Click here to Skip to main content

Tagged as

User32.dll - Lock the computer

, 21 Apr 2014 CPOL 20.7K 468 32
Rate this:
Please Sign up or sign in to vote.
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:

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:

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

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

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

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)

Share

About the Author

Marco Bertschi
Software Developer (Junior)
Switzerland Switzerland
Software Developer (Swiss Federal VET Diploma), experienced with Qt C++, C#, RFC 5424 and Arduino Boards.
Music enthusiast, runner, part-time psychologist for friends, awesome guy and Air Force Sergeant First Class.
Follow on   Twitter

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
protectorMarco 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
protectorMarco 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
protectorMarco 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    Rant Rant    Admin Admin   

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150520.1 | Last Updated 22 Apr 2014
Article Copyright 2013 by Marco Bertschi
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid