Windows Shutdown code

Source: Internet
Author: User

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 );
}
}

 

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.