Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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

First method, probably the simplest:

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


See http://msdn.microsoft.com/en-us/library/h6ak8zt5.aspx[^].

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

C#
class LockIt {
   [System.Runtime.InteropServices.DllImport("user32.dll")]
   internal static extern void LockWorkStation();
}


That's is.

—SA
 
Share this answer
 
Comments
Parveen Rathi 29-Feb-12 1:10am    
But how i unlock it again?
Here is the sample windows application which does this task by using two APIs from user32.dll.

C#
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;
        [DllImport("user32.dll")]
        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>
        [STAThread]
        static void Main()
        {
            LockWorkStation();
            TurnOffDisplay();
        }
    }
}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 29-Feb-12 0:43am    
I do not understand why it's so complex. My two solutions work, my simpler. Want to see?
--SA
Parveen Rathi 29-Feb-12 1:04am    
Yes. my pleasure
Parveen Rathi 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900