1. using System;  
  2. using System.Collections;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Web;  
  7. using System.Web.SessionState;  
  8. using System.Web.UI;  
  9. using System.Web.UI.WebControls;  
  10. using System.Web.UI.HtmlControls;  
  11. using System.Runtime.InteropServices;  
  12. namespace WebApplication1_advapi  
  13. {  
  14.   /**//// <summary>  
  15.   /// WebForm1 的摘要说明。  
  16.   /// </summary>  
  17.   public class WebForm1 : System.Web.UI.Page  
  18.   {  
  19.     protected const int SE_PRIVILEGE_ENABLED = 0x2;  
  20.     protected const int TOKEN_QUERY = 0x8;  
  21.     protected const int TOKEN_ADJUST_PRIVILEGES = 0x20;  
  22.     protected const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";  
  23.     protected const int EWX_LOGOFF = 0x0;  
  24.     protected const int EWX_SHUTDOWN = 0x1;  
  25.     protected const int EWX_REBOOT = 0x2;  
  26.     protected const int EWX_FORCE = 0x4;  
  27.     protected const int EWX_POWEROFF = 0x8;  
  28.     protected System.Web.UI.WebControls.Button Button1;  
  29.     protected const int EWX_FORCEIFHUNG = 0x10;  
  30.     [StructLayout(LayoutKind.Sequential, Pack=1)]  
  31.       protected struct LuidStruct  
  32.     {  
  33.       public int Count;  
  34.       public long Luid;  
  35.       public int Attr;  
  36.     }  
  37.     [DllImport("kernel32.dll", ExactSpelling=true)]  
  38.     protected static extern IntPtr GetCurrentProcess();  
  39.     [DllImport("advapi32.dll", SetLastError=true)]  
  40.     protected static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);  
  41.     [DllImport("advapi32.dll", SetLastError=true)]  
  42.     protected static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);  
  43.     [DllImport("advapi32.dll", SetLastError=true, ExactSpelling=true)]  
  44.     protected static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall, ref LuidStruct newst, int len, IntPtr prev, IntPtr relen);  
  45.     [DllImport("user32.dll", SetLastError=true, ExactSpelling=true)]  
  46.     protected static extern bool ExitWindowsEx(int flg, int rea);  
  47.     protected static void DoExitWindows(int flg)  
  48.     {  
  49.       LuidStruct tp;  
  50.       IntPtr hproc = GetCurrentProcess();  
  51.       IntPtr htok = IntPtr.Zero;  
  52.       OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);  
  53.       tp.Count = 1;  
  54.       tp.Luid = 0;  
  55.       tp.Attr = SE_PRIVILEGE_ENABLED;  
  56.       LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid);  
  57.       AdjustTokenPrivileges(htok, falseref tp, 0, IntPtr.Zero, IntPtr.Zero);  
  58.       ExitWindowsEx(flg, 0);  
  59.     }  
  60.     public static void Shutdown()  
  61.     {  
  62.       DoExitWindows(EWX_SHUTDOWN);  
  63.     }  
  64.     public static void Reboot()  
  65.     {  
  66.       DoExitWindows(EWX_REBOOT | EWX_FORCE);  
  67.     }  
  68.     public static void Logoff()  
  69.     {  
  70.       DoExitWindows(EWX_LOGOFF);  
  71.     }  
  72.     [DllImport("advapi32.dll", SetLastError=true, ExactSpelling=false)]  
  73.     protected static extern bool InitiateSystemShutdown(string name, string msg, int timeout, bool force, bool reboot);  
  74.     private void Page_Load(object sender, System.EventArgs e)  
  75.     {  
  76.       // 在此处放置用户代码以初始化页面  
  77.     }  
  78.     Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码  
  79.     override protected void OnInit(EventArgs e)  
  80.     {  
  81.       //  
  82.       // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。  
  83.       //  
  84.       InitializeComponent();  
  85.       base.OnInit(e);  
  86.     }  
  87.     /**//// <summary>  
  88.     /// 设计器支持所需的方法 - 不要使用代码编辑器修改  
  89.     /// 此方法的内容。  
  90.     /// </summary>  
  91.     private void InitializeComponent()  
  92.     {    
  93.       this.Button1.Click += new System.EventHandler(this.Button1_Click);  
  94.       this.Load += new System.EventHandler(this.Page_Load);  
  95.     }  
  96.     #endregion  
  97.     private void Button1_Click(object sender, System.EventArgs e)  
  98.     {  
  99.         InitiateSystemShutdown(null,null,0,true,true);  
  100.     }  
  101.   }