Code for Shutdown,Restart and Logoff....
--------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace clikme
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[DllImport("user32.dll", SetLastError = true)]
static extern int ExitWindowsEx(uint uFlags, uint dwReason);
enum ExitFlags
{
Logoff = 0,
Shutdown = 1,
Reboot = 2,
Force = 4,
PowerOff = 8,
ForceIfHung = 16
}
enum Reason : uint
{
ApplicationIssue = 0x00040000,
HardwareIssue = 0x00010000,
SoftwareIssue = 0x00030000,
PlannedShutdown = 0x80000000
}
const int PrivilegeEnabled = 0x00000002;
const int TokenQuery = 0x00000008;
const int AdjustPrivileges = 0x00000020;
const string ShutdownPrivilege = "SeShutdownPrivilege";
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct TokenPrivileges
{
public int PrivilegeCount;
public long Luid;
public int Attributes;
}
[DllImport("kernel32.dll")]
internal static extern IntPtr GetCurrentProcess();
[DllImport("advapi32.dll", SetLastError = true)]
internal static extern int OpenProcessToken(
IntPtr processHandle,
int desiredAccess,
ref IntPtr tokenHandle);
[DllImport("advapi32.dll", SetLastError = true)]
internal static extern int LookupPrivilegeValue(
string systemName, string name, ref long luid);
[DllImport("advapi32.dll", SetLastError = true)]
internal static extern int AdjustTokenPrivileges(
IntPtr tokenHandle, bool disableAllPrivileges,
ref TokenPrivileges newState,
int bufferLength,
IntPtr previousState,
IntPtr length);
private void ElevatePrivileges()
{
IntPtr currentProcess = GetCurrentProcess();
IntPtr tokenHandle = IntPtr.Zero;
int result = OpenProcessToken(
currentProcess,
AdjustPrivileges | TokenQuery,
ref tokenHandle);
if (result == 0)
throw new Win32Exception(Marshal.GetLastWin32Error());
TokenPrivileges tokenPrivileges;
tokenPrivileges.PrivilegeCount = 1;
tokenPrivileges.Luid = 0;
tokenPrivileges.Attributes = PrivilegeEnabled;
result = LookupPrivilegeValue(
null,
ShutdownPrivilege,
ref tokenPrivileges.Luid);
if (result == 0)
throw new Win32Exception(Marshal.GetLastWin32Error());
result = AdjustTokenPrivileges(
tokenHandle,
false,
ref tokenPrivileges,
0, IntPtr.Zero,
IntPtr.Zero);
if (result == 0)
throw new Win32Exception(Marshal.GetLastWin32Error());
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void ShutdownButton_Click(object sender, EventArgs e)
{
MessageBox.Show("The System is going to be Shutdown in few minutes....");
ElevatePrivileges();
int result = ExitWindowsEx(
(uint)(ExitFlags.Shutdown | ExitFlags.PowerOff | ExitFlags.Force),
(uint)(Reason.HardwareIssue | Reason.PlannedShutdown));
if (result == 0)
throw new Win32Exception(Marshal.GetLastWin32Error());
}
private void RestartButton_Click(object sender, EventArgs e)
{
MessageBox.Show("The System is going to be Restart in few minutes....");
ElevatePrivileges();
int result = ExitWindowsEx(
(uint)(ExitFlags.Reboot),
(uint)(Reason.SoftwareIssue | Reason.PlannedShutdown));
if (result == 0)
 

