Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

User32.dll - Lock the computer

4.80/5 (19 votes)
21 Apr 2014CPOL2 min read 50.2K   733  
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)