Recently in the design of the Web site background management system, I thought of the page can be restarted Windows Server
Went to Google and found a piece of code that seemed very common.
It turns out that this code works well when it comes to writing desktop applications such as console or Windows Form programs, but it doesn't work through the asp.net call.
But I'm going to post this code, because in addition to the individual two lines, the other is the necessary code to reboot the server
Create a new class that fills in the following code:
First is the namespace, when the win API is invoked, the interopservices is not less:
using System;
using System.Runtime.InteropServices;
Then there is a series of constant declarations:
protected const int SE_PRIVILEGE_ENABLED = 0x2;
protected const int TOKEN_QUERY = 0x8;
protected const int TOKEN_ADJUST_PRIVILEGES = 0x20;
protected const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
protected const int EWX_LOGOFF = 0x0;
protected const int EWX_SHUTDOWN = 0x1;
protected const int EWX_REBOOT = 0x2;
protected const int EWX_FORCE = 0x4;
protected const int EWX_POWEROFF = 0x8;
protected const int EWX_FORCEIFHUNG = 0x10;
Define LUID structure, note attributes:
[StructLayout(LayoutKind.Sequential, Pack=1)]
protected struct LuidStruct {
public int Count;
public long Luid;
public int Attr;
}Declaration of an external unmanaged DLL:
[DllImport("kernel32.dll", ExactSpelling=true)]
protected static extern IntPtr GetCurrentProcess();
[DllImport("advapi32.dll", SetLastError=true)]
protected static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);
[DllImport("advapi32.dll", SetLastError=true)]
protected static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);
[DllImport("advapi32.dll", SetLastError=true, ExactSpelling=true)]
protected static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall, ref LuidStruct newst, int len, IntPtr prev, IntPtr relen);
[DllImport("user32.dll", SetLastError=true, ExactSpelling=true)]
protected static extern bool ExitWindowsEx(int flg, int rea);