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 co
【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】
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 co
using System.Runtime.InteropServices;
Now we are ready to imp
[DllImp
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 calledExitWindowsEx(). This function accepts two arguments, on
[DllImp
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 co
ExitWindowsEx(2, 0);
【Shutdown】
Now add the following co
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);
Please note that these methods were on