Abstract: many windows software involve the automatic shutdown function. This article introduces how to implement the windows Shutdown function in section 2.
1. shutdown.exe
The principle is to call the shutdown command.
Advantages:
Easy to implement.
Disadvantages:
After the command is executed, a dialog box is displayed. (Currently, I have not found a method to cancel it)
C # implementation code:
[Csharp]
System. Diagnostics. Process boot_process = new System. Diagnostics. Process ();
Boot_process.StartInfo.FileName = "shutdown ";
Boot_process.StartInfo.Arguments = "/s/t 3 ";
Boot_process.StartInfo.CreateNoWindow = true;
Boot_process.StartInfo.UseShellExecute = false;
Boot_process.Start ();
System. Diagnostics. Process boot_process = new System. Diagnostics. Process ();
Boot_process.StartInfo.FileName = "shutdown ";
Boot_process.StartInfo.Arguments = "/s/t 3 ";
Boot_process.StartInfo.CreateNoWindow = true;
Boot_process.StartInfo.UseShellExecute = false;
Boot_process.Start ();
2. Call the windows underlying function ExitWindowsEx to shut down
BOOL ExitWindowsEx (
UINT uFlags, // close the Parameter
DWORD dwReserved // system reserved, generally 0
);
The following section describes the shutdown code of C #.
The main function is ExitWindowsEx, while the other is mainly to obtain the shutdown permission of the win7 system.
C # implementation code:
[Csharp]
Class Shutdown
{
[StructLayout (LayoutKind. Sequential, Pack = 1)]
Internal struct TokPriv1Luid
{
Public int Count;
Public long Luid;
Public int Attr;
}
[DllImport ("kernel32.dll", ExactSpelling = true)]
Internal static extern IntPtr GetCurrentProcess ();
[DllImport ("advapi32.dll", ExactSpelling = true, SetLastError = true)]
Internal static extern bool OpenProcessToken (IntPtr h, int acc, ref IntPtr phtok );
[DllImport ("advapi32.dll", SetLastError = true)]
Internal static extern bool LookupPrivilegeValue (string host, string name, ref long pluid );
[DllImport ("advapi32.dll", ExactSpelling = true, SetLastError = true)]
Internal static extern bool AdjustTokenPrivileges (IntPtr htok, bool disall,
Ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen );
[DllImport ("user32.dll", ExactSpelling = true, SetLastError = true)]
Internal static extern bool ExitWindowsEx (int DoFlag, int rea );
Internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
Internal const int TOKEN_QUERY = 0x00000008;
Internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
Internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege ";
Internal const int EWX_LOGOFF = 0x00000000;
Internal const int EWX_SHUTDOWN = 0x00000001;
Internal const int EWX_REBOOT = 0x00000002;
Integer const int EWX_FORCE = 0x00000004;
Internal const int EWX_POWEROFF = 0x00000008;
Internal const int EWX_FORCEIFHUNG = 0x00000010;
Public static void DoExitWin (int DoFlag)
{
Bool res;
TokPriv1Luid tp;
IntPtr hproc = GetCurrentProcess ();
IntPtr htok = IntPtr. Zero;
Res = OpenProcessToken (hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok );
Tp. Count = 1;
Tp. Luid = 0;
Tp. Attr = SE_PRIVILEGE_ENABLED;
Res = LookupPrivilegeValue (null, SE_SHUTDOWN_NAME, ref tp. Luid );
Res = AdjustTokenPrivileges (htok, false, ref tp, 0, IntPtr. Zero, IntPtr. Zero );
Res = ExitWindowsEx (DoFlag, 0 );
}
Public static void Reboot ()
{
DoExitWin (EWX_FORCE | EWX_REBOOT );
}
Public static void PowerOff ()
{
DoExitWin (EWX_FORCE | EWX_POWEROFF );
}
Public static void LogOff ()
{
DoExitWin (EWX_FORCE | EWX_LOGOFF );
}
}
Class Shutdown
{
[StructLayout (LayoutKind. Sequential, Pack = 1)]
Internal struct TokPriv1Luid
{
Public int Count;
Public long Luid;
Public int Attr;
}
[DllImport ("kernel32.dll", ExactSpelling = true)]
Internal static extern IntPtr GetCurrentProcess ();
[DllImport ("advapi32.dll", ExactSpelling = true, SetLastError = true)]
Internal static extern bool OpenProcessToken (IntPtr h, int acc, ref IntPtr phtok );
[DllImport ("advapi32.dll", SetLastError = true)]
Internal static extern bool LookupPrivilegeValue (string host, string name, ref long pluid );
[DllImport ("advapi32.dll", ExactSpelling = true, SetLastError = true)]
Internal static extern bool AdjustTokenPrivileges (IntPtr htok, bool disall,
Ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen );
[DllImport ("user32.dll", ExactSpelling = true, SetLastError = true)]
Internal static extern bool ExitWindowsEx (int DoFlag, int rea );
Internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
Internal const int TOKEN_QUERY = 0x00000008;
Internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
Internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege ";
Internal const int EWX_LOGOFF = 0x00000000;
Internal const int EWX_SHUTDOWN = 0x00000001;
Internal const int EWX_REBOOT = 0x00000002;
Integer const int EWX_FORCE = 0x00000004;
Internal const int EWX_POWEROFF = 0x00000008;
Internal const int EWX_FORCEIFHUNG = 0x00000010;
Public static void DoExitWin (int DoFlag)
{
Bool res;
TokPriv1Luid tp;
IntPtr hproc = GetCurrentProcess ();
IntPtr htok = IntPtr. Zero;
Res = OpenProcessToken (hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok );
Tp. Count = 1;
Tp. Luid = 0;
Tp. Attr = SE_PRIVILEGE_ENABLED;
Res = LookupPrivilegeValue (null, SE_SHUTDOWN_NAME, ref tp. Luid );
Res = AdjustTokenPrivileges (htok, false, ref tp, 0, IntPtr. Zero, IntPtr. Zero );
Res = ExitWindowsEx (DoFlag, 0 );
}
Public static void Reboot ()
{
DoExitWin (EWX_FORCE | EWX_REBOOT );
}
Public static void PowerOff ()
{
DoExitWin (EWX_FORCE | EWX_POWEROFF );
}
Public static void LogOff ()
{
DoExitWin (EWX_FORCE | EWX_LOGOFF );
}
}