Wednesday, November 16, 2011
How can Lock, Logoff, Reboot, Shutdown, Hibernate, Standby in .Net?
Introduction
This article is about locking, logging off , rebooting, shutting down, hibernating and putting the system on stand by mode in .Net. Here we are going to use both unmanaged code and .Net framework for these functions.
Getting Started
This article is about locking, logging off , rebooting, shutting down, hibernating and putting the system on stand by mode in .Net. Here we are going to use both unmanaged code and .Net framework for these functions.
Getting Started
Let us start by creating a windows application. To our newly created form, add seven buttons entitled btnLockComp, btnLogOff, btnReboot, btnShutdown, btnForceLogOff, btnHibernate, btnStandby.
Lock Workstation
Lock Workstation
Let us start with locking the workstation, which is supposed to lock the current user session. We will call a windows API for doing this. For calling an un-managed piece of code, we need to add the System.Runtime.InteropServices namespace to the using directive.
using System.Runtime.InteropServices;
Now we are ready to import the windows API library and define the function that we intend to use. The function to lock the workstation resides in the user32.dll library. And the function for locking the desktop is LockWorkStation. The following statements should be added to the class to import the library.
[DllImport("user32.dll")]public static extern void LockWorkStation();
Next step is to double click the btnLockComp button to create a click event handler and call the LockWorkStation API to lock the workstation.
LockWorkStation();
Log Off
For logging off we are going to use an unmanaged API function called ExitWindowsEx(). This function accepts two arguments, one for flag(logoff, shutdown, reboot, etc.,) and the other for reason for this action(maintenance, software update, etc...). Now import the user32.dll once again, this time so we can use the ExitWindowsEx() function, as shown below:
[DllImport("user32.dll")]public static extern int ExitWindowsEx(int uFlags, int dwReason);
Double click the log off button and add the following function call to the event. Flag 0 indicates logoff,
ExitWindowsEx(0, 0);
To force processes to terminate while logging off, change the flag to 4 in the function as below:
ExitWindowsEx(4, 0);
Reboot
To reboot we are going to use the same function ExitWindowsEx but with a different flag. Add the following code to the click event handler of the reboot button.
ExitWindowsEx(2, 0);
Shutdown
Now add the following code to the button Shutdown's click event handler.
ExitWindowsEx(1, 0);
Hibernate and Standby
To put the system in hibernate and standby modes, we are going to use Application class's SetSuspendState method. There are three arguments for this function, power state, force and disable wake event. The first argument, power state is where we mention the state of the system (hibernate/suspend).
LockWorkStation();
Log Off
For logging off we are going to use an unmanaged API function called ExitWindowsEx(). This function accepts two arguments, one for flag(logoff, shutdown, reboot, etc.,) and the other for reason for this action(maintenance, software update, etc...). Now import the user32.dll once again, this time so we can use the ExitWindowsEx() function, as shown below:
[DllImport("user32.dll")]public static extern int ExitWindowsEx(int uFlags, int dwReason);
Double click the log off button and add the following function call to the event. Flag 0 indicates logoff,
ExitWindowsEx(0, 0);
To force processes to terminate while logging off, change the flag to 4 in the function as below:
ExitWindowsEx(4, 0);
Reboot
To reboot we are going to use the same function ExitWindowsEx but with a different flag. Add the following code to the click event handler of the reboot button.
ExitWindowsEx(2, 0);
Shutdown
Now add the following code to the button Shutdown's click event handler.
ExitWindowsEx(1, 0);
Hibernate and Standby
To put the system in hibernate and standby modes, we are going to use Application class's SetSuspendState method. There are three arguments for this function, power state, force and disable wake event. The first argument, power state is where we mention the state of the system (hibernate/suspend).
// Hibernate
Application.SetSuspendState(PowerState.Hibernate, true, true);
// Standby
Application.SetSuspendState(PowerState.Suspend true, true);
*********************Source Code****************************
/*
* Created by SharpDevelop.
* User: Thiagarajan Alagarsamy
* Date: 2/3/2007
* Time: 8:17 PM
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace LockComp
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm
{
// Importing Windows API library
[DllImport("user32.dll")]
public static extern void LockWorkStation();
[DllImport("user32.dll")]
public static extern int ExitWindowsEx(int uFlags, int dwReason);
[STAThread]
public static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}
// Lock workstation
void BtnLockCompClick(object sender, System.EventArgs e)
{
LockWorkStation();
}
// Log Off
void BtnLogOffClick(object sender, System.EventArgs e)
{
if(DialogResult.Yes==MessageBox.Show("Do you really want to Log Off?","Log Off", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2))
ExitWindowsEx(0, 0);
}
// Reboot
void BtnRebootClick(object sender, System.EventArgs e)
{
if(DialogResult.Yes==MessageBox.Show("Do you really want to Reboot?","Reboot", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2))
ExitWindowsEx(2, 0);
}
// Shutdown
void BtnShutdownClick(object sender, System.EventArgs e)
{
if(DialogResult.Yes==MessageBox.Show("Do you really want to Shutdown?","Shutdown", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2))
ExitWindowsEx(1, 0);
}
// Force LogOff
void BtnForceLogOffClick(object sender, System.EventArgs e)
{
if(DialogResult.Yes==MessageBox.Show("Do you really want to force Log Off?","Force LogOff", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2))
ExitWindowsEx(4, 0);
}
// Hibernate
void BtnHibernateClick(object sender, System.EventArgs e)
{
Application.SetSuspendState(PowerState.Hibernate, true, true);
}
// Stand By
void BtnStandbyClick(object sender, System.EventArgs e)
{
Application.SetSuspendState(PowerState.Suspend, true, true);
}
}
}