devxlogo

Locking Your Computer with C#

Locking Your Computer with C#

In order to lock your computer, you need to use the following two APIs:

[DllImport("user32.dll")]        public static extern void LockWorkStation();              [DllImport("user32.dll")]        public static extern int ExitWindowsEx(int uFlags, int dwReason); 

You should implement it as follows:

// Lock computer      void btnLock_Click(object sender, System.EventArgs e)      {         LockWorkStation();      }// Log Off      void btnLogOff_Click(object sender, System.EventArgs e)      {         if(DialogResult.Yes==MessageBox.Show("Log Off?","Log Off", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2))         ExitWindowsEx(0, 0);      }   // Shutdown      void btnShutdown_Click(object sender, System.EventArgs e)      {         if(DialogResult.Yes==MessageBox.Show("Shutdown?","Shutdown", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2))         ExitWindowsEx(1, 0 ); 
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist