1
yhj
2024-07-24 5e5d945e91568b973faa27d8ab0bcef99fc4a6c5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#region
 
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
 
#endregion
 
namespace CSFrameworkV5.Common
{
    /// <summary>
    ///     控制计算机关闭、重新启动、注销
    /// </summary>
    public class ComputerShutDown
    {
        private const int SE_PRIVILEGE_ENABLED = 0x00000002;
        private const int TOKEN_QUERY = 0x00000008;
        private const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
        private const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
        private const int EWX_LOGOFF = 0x00000000;
        private const int EWX_SHUTDOWN = 0x00000001;
        private const int EWX_REBOOT = 0x00000002;
        private const int EWX_FORCE = 0x00000004;
        private const int EWX_POWEROFF = 0x00000008;
        private const int EWX_FORCEIFHUNG = 0x00000010;
 
        [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
        private static extern bool AdjustTokenPrivileges(IntPtr htok,
            bool disall, ref TokPriv1Luid newst, int len,
            IntPtr prev, IntPtr relen);
 
        /// <summary>
        ///     关闭计算机
        /// </summary>
        public static void Close()
        {
            //创建访问控制本地系统进程的对象实例
            var myprocess = new Process();
            myprocess.StartInfo.FileName = "cmd.exe";
            myprocess.StartInfo.UseShellExecute = false;
            myprocess.StartInfo.RedirectStandardInput = true;
            myprocess.StartInfo.RedirectStandardOutput = true;
            myprocess.StartInfo.RedirectStandardError = true;
            myprocess.StartInfo.CreateNoWindow = true;
            myprocess.Start();
            myprocess.StandardInput.WriteLine("shutdown -s -t 0");
        }
 
        private static void DoExitWin(int DoFlag)
        {
            bool ok;
            TokPriv1Luid tp;
            var hproc = GetCurrentProcess();
            var htok = IntPtr.Zero;
            ok = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
                ref htok);
            tp.Count = 1;
            tp.Luid = 0;
            tp.Attr = SE_PRIVILEGE_ENABLED;
            ok = LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid);
            ok = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero,
                IntPtr.Zero);
            var i = ExitWindowsEx(DoFlag, 0);
        }
 
        //设置注销、关闭、重新启动计算机参数
        [DllImport("user32.dll", EntryPoint = "ExitWindowsEx",
            CharSet = CharSet.Ansi)]
        private static extern int ExitWindowsEx(int uFlags, int dwReserved);
 
        [DllImport("kernel32.dll", ExactSpelling = true)]
        private static extern IntPtr GetCurrentProcess();
 
        /// <summary>
        ///     注销用户
        /// </summary>
        public static void Logout()
        {
            ExitWindowsEx(0, 0);
        }
 
        [DllImport("advapi32.dll", SetLastError = true)]
        private static extern bool LookupPrivilegeValue(string host,
            string name, ref long pluid);
 
        [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
        private static extern bool OpenProcessToken(IntPtr h, int acc,
            ref IntPtr phtok);
 
        /// <summary>
        ///     重新启动计算机
        /// </summary>
        public static void Restart()
        {
            //创建访问控制本地系统进程的对象实例
            var myprocess = new Process();
            myprocess.StartInfo.FileName = "cmd.exe";
            myprocess.StartInfo.UseShellExecute = false;
            myprocess.StartInfo.RedirectStandardInput = true;
            myprocess.StartInfo.RedirectStandardOutput = true;
            myprocess.StartInfo.RedirectStandardError = true;
            myprocess.StartInfo.CreateNoWindow = true;
            myprocess.Start();
            myprocess.StandardInput.WriteLine("shutdown -r -t 0");
        }
 
        /// <summary>
        ///     注销用户
        /// </summary>
        public static void Win32_LogOff()
        {
            DoExitWin(EWX_FORCE | EWX_LOGOFF);
        }
 
        /// <summary>
        ///     关闭电源,关闭计算机
        /// </summary>
        public static void Win32_PowerOff()
        {
            DoExitWin(EWX_FORCE | EWX_POWEROFF);
        }
 
        /// <summary>
        ///     重新启动计算机
        /// </summary>
        public static void Win32_Reboot()
        {
            DoExitWin(EWX_FORCE | EWX_REBOOT);
        }
 
        [StructLayout(LayoutKind.Sequential, Pack = 1)]
        private struct TokPriv1Luid
        {
            public int Count;
            public long Luid;
            public int Attr;
        }
    }
}