Skip to content

Commit 5a8155b

Browse files
committed
Fixed the shutdown command.
1 parent ad65159 commit 5a8155b

File tree

5 files changed

+169
-16
lines changed

5 files changed

+169
-16
lines changed

.gitignore

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,3 +252,64 @@ paket-files/
252252
*.sln.iml
253253
/CommandEverything/CommandEverything/Framework/Util/Error.cs
254254
/CommandEverything/CommandEverything/Framework/Util/Secure.cs
255+
/CommandEverything/7Zip/Java/SevenZip/Compression/RangeCoder/Encoder.java
256+
/CommandEverything/7Zip/Java/SevenZip/Compression/RangeCoder/Decoder.java
257+
/CommandEverything/7Zip/Java/SevenZip/Compression/RangeCoder/BitTreeEncoder.java
258+
/CommandEverything/7Zip/Java/SevenZip/Compression/RangeCoder/BitTreeDecoder.java
259+
/CommandEverything/7Zip/Java/SevenZip/Compression/LZMA/Encoder.java
260+
/CommandEverything/7Zip/Java/SevenZip/Compression/LZMA/Decoder.java
261+
/CommandEverything/7Zip/Java/SevenZip/Compression/LZMA/Base.java
262+
/CommandEverything/7Zip/Java/SevenZip/Compression/LZ/OutWindow.java
263+
/CommandEverything/7Zip/Java/SevenZip/Compression/LZ/InWindow.java
264+
/CommandEverything/7Zip/Java/SevenZip/Compression/LZ/BinTree.java
265+
/CommandEverything/7Zip/CS/7zip/Compress/RangeCoder/RangeCoderBitTree.cs
266+
/CommandEverything/7Zip/CS/7zip/Compress/RangeCoder/RangeCoderBit.cs
267+
/CommandEverything/7Zip/CS/7zip/Compress/RangeCoder/RangeCoder.cs
268+
/CommandEverything/7Zip/CS/7zip/Compress/LzmaAlone/Properties/Settings.cs
269+
/CommandEverything/7Zip/CS/7zip/Compress/LzmaAlone/Properties/Resources.cs
270+
/CommandEverything/7Zip/CS/7zip/Compress/LzmaAlone/Properties/AssemblyInfo.cs
271+
/CommandEverything/7Zip/CS/7zip/Compress/LzmaAlone/LzmaBench.cs
272+
/CommandEverything/7Zip/CS/7zip/Compress/LzmaAlone/LzmaAlone2.sln
273+
/CommandEverything/7Zip/CS/7zip/Compress/LzmaAlone/LzmaAlone.sln
274+
/CommandEverything/7Zip/CS/7zip/Compress/LzmaAlone/LzmaAlone.csproj
275+
/CommandEverything/7Zip/CS/7zip/Compress/LzmaAlone/LzmaAlone.cs
276+
/CommandEverything/7Zip/CS/7zip/Compress/LZMA/LzmaEncoder.cs
277+
/CommandEverything/7Zip/CS/7zip/Compress/LZMA/LzmaDecoder.cs
278+
/CommandEverything/7Zip/CS/7zip/Compress/LZMA/LzmaBase.cs
279+
/CommandEverything/7Zip/CS/7zip/Compress/LZ/LzOutWindow.cs
280+
/CommandEverything/7Zip/CS/7zip/Compress/LZ/LzInWindow.cs
281+
/CommandEverything/7Zip/CS/7zip/Compress/LZ/LzBinTree.cs
282+
/CommandEverything/7Zip/CS/7zip/Compress/LZ/IMatchFinder.cs
283+
/CommandEverything/7Zip/CPP/7zip/Compress/StdAfx.h
284+
/CommandEverything/7Zip/CPP/7zip/Compress/PpmdRegister.cpp
285+
/CommandEverything/7Zip/CPP/7zip/Compress/PpmdEncoder.h
286+
/CommandEverything/7Zip/CPP/7zip/Compress/PpmdEncoder.cpp
287+
/CommandEverything/7Zip/CPP/7zip/Compress/PpmdDecoder.h
288+
/CommandEverything/7Zip/CPP/7zip/Compress/PpmdDecoder.cpp
289+
/CommandEverything/7Zip/CPP/7zip/Compress/LzmaRegister.cpp
290+
/CommandEverything/7Zip/CPP/7zip/Compress/LzmaEncoder.h
291+
/CommandEverything/7Zip/CPP/7zip/Compress/LzmaEncoder.cpp
292+
/CommandEverything/7Zip/CPP/7zip/Compress/LzmaDecoder.h
293+
/CommandEverything/7Zip/CPP/7zip/Compress/LzmaDecoder.cpp
294+
/CommandEverything/7Zip/CPP/7zip/Compress/Lzma2Register.cpp
295+
/CommandEverything/7Zip/CPP/7zip/Compress/Lzma2Encoder.h
296+
/CommandEverything/7Zip/CPP/7zip/Compress/Lzma2Encoder.cpp
297+
/CommandEverything/7Zip/CPP/7zip/Compress/Lzma2Decoder.h
298+
/CommandEverything/7Zip/CPP/7zip/Compress/Lzma2Decoder.cpp
299+
/CommandEverything/7Zip/CPP/7zip/Compress/DeltaFilter.cpp
300+
/CommandEverything/7Zip/CPP/7zip/Compress/CopyRegister.cpp
301+
/CommandEverything/7Zip/CPP/7zip/Compress/CopyCoder.h
302+
/CommandEverything/7Zip/CPP/7zip/Compress/CopyCoder.cpp
303+
/CommandEverything/7Zip/CPP/7zip/Compress/CodecExports.cpp
304+
/CommandEverything/7Zip/CPP/7zip/Compress/ByteSwap.cpp
305+
/CommandEverything/7Zip/CPP/7zip/Compress/BranchRegister.cpp
306+
/CommandEverything/7Zip/CPP/7zip/Compress/BranchMisc.h
307+
/CommandEverything/7Zip/CPP/7zip/Compress/BranchMisc.cpp
308+
/CommandEverything/7Zip/CPP/7zip/Compress/BcjRegister.cpp
309+
/CommandEverything/7Zip/CPP/7zip/Compress/BcjCoder.h
310+
/CommandEverything/7Zip/CPP/7zip/Compress/BcjCoder.cpp
311+
/CommandEverything/7Zip/CPP/7zip/Compress/Bcj2Register.cpp
312+
/CommandEverything/7Zip/CPP/7zip/Compress/Bcj2Coder.h
313+
/CommandEverything/7Zip/CPP/7zip/Compress/Bcj2Coder.cpp
314+
/CommandEverything/7Zip/CPP/7zip/Archive/7z/7zCompressionMode.h
315+
/CommandEverything/7Zip/CPP/7zip/Archive/7z/7zCompressionMode.cpp

CommandEverything/CommandEverything/CommandEverything.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@
129129
</PropertyGroup>
130130
<PropertyGroup />
131131
<ItemGroup>
132+
<Reference Include="Colorful.Console, Version=1.0.7.0, Culture=neutral, processorArchitecture=MSIL">
133+
<HintPath>..\packages\Colorful.Console.1.0.7\lib\net461\Colorful.Console.dll</HintPath>
134+
<Private>True</Private>
135+
</Reference>
132136
<Reference Include="Octokit, Version=0.24.0.0, Culture=neutral, processorArchitecture=MSIL">
133137
<HintPath>..\packages\Octokit.0.24.0\lib\net45\Octokit.dll</HintPath>
134138
<Private>True</Private>
@@ -181,10 +185,12 @@
181185
<Compile Include="Framework\Commands\GenerateReflectionData.cs" />
182186
<Compile Include="Framework\Commands\RestartCommand.cs" />
183187
<Compile Include="Framework\Util\Reflection.cs" />
188+
<Compile Include="Framework\Util\ShutdownUtil.cs" />
184189
<Compile Include="Framework\Util\Text\ConsoleWriter.cs" />
185190
<Compile Include="Framework\Util\Text\Logging.cs" />
186191
<Compile Include="Framework\Util\Text\Writable.cs" />
187192
<Compile Include="Framework\Commands\CMDCommands.cs" />
193+
<Compile Include="Framework\WIP\CompressCommand.cs" />
188194
<Compile Include="Framework\WIP\FreezeProgram.cs" />
189195
<Compile Include="Framework\WIP\ChangeDirectory.cs" />
190196
<Compile Include="Framework\Commands\ExitCommand.cs" />

CommandEverything/CommandEverything/Framework/Commands/ShutdownCommand.cs

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Management;
66
using System.Text;
77
using System.Threading.Tasks;
8+
using System.Runtime.InteropServices;
89

910
namespace CommandEverything.Framework.Commands
1011
{
@@ -25,22 +26,8 @@ public string GetName()
2526

2627
public void Run(string Input)
2728
{
28-
ManagementBaseObject mboShutdown = null;
29-
ManagementClass mcWin32 = new ManagementClass("Win32_OperatingSystem");
30-
mcWin32.Get();
31-
32-
// You can't shutdown without security privileges
33-
mcWin32.Scope.Options.EnablePrivileges = true;
34-
ManagementBaseObject mboShutdownParams =
35-
mcWin32.GetMethodParameters("Win32Shutdown");
36-
37-
// Flag 1 means we want to shut down the system. Use "2" to reboot.
38-
mboShutdownParams["Flags"] = "1";
39-
mboShutdownParams["Reserved"] = "0";
40-
foreach (ManagementObject manObj in mcWin32.GetInstances())
41-
{
42-
mboShutdown = manObj.InvokeMethod("Win32Shutdown", mboShutdownParams, null);
43-
}
29+
ShutdownUtil a = new ShutdownUtil();
30+
a.ExitWindows(ShutdownUtil.EWX_SHUTDOWN);
4431
}
4532

4633
public bool ShouldRunThisCommand(string Input)
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using System.Runtime.InteropServices;
7+
8+
namespace CommandEverything.Framework.Util
9+
{
10+
/// <summary>
11+
/// Used to shutdown or restart the computer.
12+
/// </summary>
13+
public class ShutdownUtil
14+
{
15+
[StructLayout(LayoutKind.Sequential, Pack = 1)]
16+
internal struct TokPriv1Luid
17+
{
18+
public int Count;
19+
public long Luid;
20+
public int Attr;
21+
}
22+
23+
[DllImport("kernel32.dll", ExactSpelling = true)]
24+
internal static extern IntPtr GetCurrentProcess();
25+
26+
[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
27+
internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr
28+
phtok);
29+
30+
[DllImport("advapi32.dll", SetLastError = true)]
31+
internal static extern bool LookupPrivilegeValue(string host, string name,
32+
ref long pluid);
33+
34+
[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
35+
internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall,
36+
ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);
37+
38+
[DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
39+
internal static extern bool ExitWindowsEx(int flg, int rea);
40+
41+
internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
42+
internal const int TOKEN_QUERY = 0x00000008;
43+
internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
44+
internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
45+
46+
/// <summary>
47+
/// Flag tells the computer to log off the current user.
48+
/// </summary>
49+
internal const int EWX_LOGOFF = 0x00000000;
50+
51+
/// <summary>
52+
/// Flag tells the computer to shutdown.
53+
/// </summary>
54+
internal const int EWX_SHUTDOWN = 0x00000001;
55+
56+
/// <summary>
57+
/// Flag tells the computer to reboot.
58+
/// </summary>
59+
internal const int EWX_REBOOT = 0x00000002;
60+
61+
/// <summary>
62+
/// Has no effect if terminal services is enabled. Makes the system to not send the WM_QUERYENDSESSION message. This can cause applications to lose data. Causes the user to logout quickily.
63+
/// </summary>
64+
internal const int EWX_FORCE = 0x00000004;
65+
66+
internal const int EWX_POWEROFF = 0x00000008;
67+
68+
/// <summary>
69+
/// Forces processes to terminate if they do not respond to system messages.
70+
/// </summary>
71+
internal const int EWX_FORCEIFHUNG = 0x00000010;
72+
73+
/// <summary>
74+
/// Partially shuts down the computer for faster startup times.
75+
/// </summary>
76+
internal const int EWX_HYBRID_SHUTDOWN = 0x00400000;
77+
78+
/// <summary>
79+
/// Restarts the computer, and launches applications register applications.
80+
/// </summary>
81+
internal const int EWX_RESTARTAPPS = 0x00000040;
82+
83+
public void ExitWindows(int flg)
84+
{
85+
bool ok;
86+
TokPriv1Luid tp;
87+
IntPtr hproc = GetCurrentProcess();
88+
IntPtr htok = IntPtr.Zero;
89+
ok = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
90+
tp.Count = 1;
91+
tp.Luid = 0;
92+
tp.Attr = SE_PRIVILEGE_ENABLED;
93+
ok = LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid);
94+
ok = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
95+
ok = ExitWindowsEx(flg, 0);
96+
}
97+
}
98+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
33
<package id="Cmd.Net" version="1.4.1" targetFramework="net461" />
4+
<package id="Colorful.Console" version="1.0.7" targetFramework="net461" />
45
<package id="Octokit" version="0.24.0" targetFramework="net461" />
56
</packages>

0 commit comments

Comments
 (0)