ASP. NET shutdown code (Windows is the local machine)

Source: Internet
Author: User

Compile the. NET shutdown code. First import the namespace using System. Runtime. InteropServices;

This is to close the code of the Local Machine. For example, if the program is placed on the Server, the Server is closed, not the client.

The following. NET shutdown code is applicable to windows

 
 
  1. using System;    
  2. using System.Data;    
  3. using System.Configuration;    
  4. using System.Web;    
  5. using System.Web.Security;    
  6. using System.Web.UI;    
  7. using System.Web.UI.WebControls;    
  8. using System.Web.UI.WebControls.WebParts;    
  9. using System.Web.UI.HtmlControls;    
  10. using System.Runtime.InteropServices;    
  11.  
  12. public partial class _Default : System.Web.UI.Page     
  13.  
  14. {    
  15.     protected void Page_Load(object sender, EventArgs e)    
  16.     {    
  17.         DoExitWin(EWX_SHUTDOWN);    
  18.     }    
  19.     [StructLayout(LayoutKind.Sequential, Pack = 1)]    
  20.     internal struct TokPriv1Luid    
  21.     {    
  22.         public int Count;    
  23.         public long Luid;    
  24.         public int Attr;    
  25.     }    
  26.    
  27. [DllImport("kernel32.dll", ExactSpelling = true)]    
  28.    internal static extern IntPtr GetCurrentProcess();    
  29.    
  30.    [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]    
  31.    internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);    
  32.    
  33.    [DllImport("advapi32.dll", SetLastError = true)]    
  34.    internal static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);    
  35.    
  36.    [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]    
  37.    internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall,    
  38.    ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);    
  39.    
  40.    [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]    
  41.    internal static extern bool ExitWindowsEx(int flg, int rea);    
  42.  
  43. internal const int SE_PRIVILEGE_ENABLED = 0x00000002;    
  44.    internal const int TOKEN_QUERY = 0x00000008;    
  45.    internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;    
  46.    internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";    
  47.    internal const int EWX_LOGOFF = 0x00000000;    
  48.    internal const int EWX_SHUTDOWN = 0x00000001;    
  49.    internal const int EWX_REBOOT = 0x00000002;    
  50.    internal const int EWX_FORCE = 0x00000004;    
  51.    internal const int EWX_POWEROFF = 0x00000008;    
  52.    internal const int EWX_FORCEIFHUNG = 0x00000010;    
  53.  
  54. private static void DoExitWin(int flg)    
  55.     {    
  56.         bool ok;    
  57.         TokPriv1Luid tp;    
  58.         IntPtr hproc = GetCurrentProcess();    
  59.         IntPtr htok = IntPtr.Zero;    
  60.         ok = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);    
  61.         tp.Count = 1;    
  62.         tp.Luid = 0;    
  63.         tp.Attr = SE_PRIVILEGE_ENABLED;    
  64.         ok = LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid);    
  65.         ok = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);    
  66.         ok = ExitWindowsEx(flg, 0);    
  67.     }    
  68. }    
  69.  

The above is the. NET shutdown code.

  1. ASP. NET DetailsView displays the details of the selected product
  2. ASP. NET 2.0 data Tutorial: select a row in the GridView
  3. ASP. NET 2.0 data Tutorial: display data in the GridView
  4. Add the GridView to the page in ASP. NET 2.0
  5. Notes for adding an ASP. NET page

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.