Click here to Skip to main content
11,705,610 members (55,411 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C#4.0
Hi.. Friends, I want to make a such type of application in which I can lock my computer using user name and password, in my c# window application, if user enter the correct username and password then computer will be unlock. I have no no idea about this, would you please help me?

Thanks & Regards in advance
Parveen Rathi
Posted 28-Feb-12 17:28pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

First method, probably the simplest:

System.Diagnostics.Process.Start("Rundll32.exe", "User32dll,LockWorkStation");


Nearly the same, without running a child process, immediately from the calling process:

class LockIt {
   internal static extern void LockWorkStation();

That's is.

Parveen Rathi at 29-Feb-12 1:10am
But how i unlock it again?
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Here is the sample windows application which does this task by using two APIs from user32.dll.

namespace Lock_Display
    static class Program
        private const int WmSyscommand = 0x0112;
        private const int ScMonitorpower = 0xF170;
        private const int HwndBroadcast = 0xFFFF;
        private const int ShutOffDisplay = 2;
        private static extern void LockWorkStation();
        [DllImport("user32.dll", SetLastError = true)]
        private static extern bool PostMessage(IntPtr hWnd, uint msg,
                      IntPtr wParam, IntPtr lParam);
        private static void TurnOffDisplay()
            PostMessage((IntPtr)HwndBroadcast, (uint)WmSyscommand,
                    (IntPtr)ScMonitorpower, (IntPtr)ShutOffDisplay);
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
SAKryukov at 29-Feb-12 0:43am
I do not understand why it's so complex. My two solutions work, my simpler. Want to see?
Parveen Rathi at 29-Feb-12 1:04am
Yes. my pleasure
Parveen Rathi at 29-Feb-12 1:07am
with this code I lock my computer and how can i unlock it again? and where I place your code?

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

  Print Answers RSS
0 OriginalGriff 229
1 Sergey Alexandrovich Kryukov 100
2 Inimicos 60
3 ProgramFOX 50
4 Richard MacCutchan 45
0 OriginalGriff 9,053
1 Sergey Alexandrovich Kryukov 8,347
2 CPallini 5,189
3 Maciej Los 4,726
4 Mika Wendelius 3,626

Advertise | Privacy | Mobile
Web03 | 2.8.150819.1 | Last Updated 29 Feb 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100