-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSystemControl.cs
More file actions
97 lines (83 loc) · 2.92 KB
/
SystemControl.cs
File metadata and controls
97 lines (83 loc) · 2.92 KB
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
//-------------------------------------------------------------------
// Copyright © 2012 Kindel Systems, LLC
// http://www.kindel.com
// charlie@kindel.com
//
// Published under the MIT License.
// Source control on SourceForge
// http://sourceforge.net/projects/mcecontroller/
//-------------------------------------------------------------------
using System;
using System.Diagnostics;
using Microsoft.Win32.Security;
namespace MCEControl
{
/// <summary>
/// Summary description for SystemControl.
/// </summary>
sealed public class SystemControl : IDisposable
{
private bool _disposed;
public SystemControl()
{
AdjustToken(true);
}
#region IDisposable Members
public void Dispose()
{
Dispose(true);
// This object will be cleaned up by the Dispose method.
// Therefore, you should call GC.SupressFinalize to
// take this object off the finalization queue
// and prevent finalization code for this object
// from executing a second time.
GC.SuppressFinalize(this);
}
#endregion
~SystemControl()
{
// Do not re-create Dispose clean-up code here.
// Calling Dispose(false) is optimal in terms of
// readability and maintainability.
Dispose(false);
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
private void Dispose(bool disposing)
{
// Check to see if Dispose has already been called.
if (!_disposed && disposing)
{
AdjustToken(false);
}
_disposed = true;
}
public void AdjustToken(bool enable)
{
var p = Process.GetCurrentProcess();
var at = new AccessTokenProcess(p.Id, TokenAccessType.TOKEN_ADJUST_PRIVILEGES);
var tp = new TokenPrivilege(TokenPrivilege.SE_SHUTDOWN_NAME, enable);
at.EnablePrivilege(tp);
}
public bool Standby()
{
return (Win32.SetSystemPowerState(Win32.TRUE, Win32.FALSE) == Win32.TRUE);
}
public bool Hibernate()
{
return (Win32.SetSystemPowerState(Win32.FALSE, Win32.FALSE) == Win32.TRUE);
}
public void Shutdown(string msg, uint timeout, bool forceAppsClosed, bool rebootAfterShutdown)
{
var n = Win32.InitiateSystemShutdown(null, msg, timeout, forceAppsClosed ? Win32.TRUE : Win32.FALSE,
rebootAfterShutdown ? Win32.TRUE : Win32.FALSE);
Win32.CheckCall(n);
}
public void Abort()
{
var n = Win32.AbortSystemShutdown(null);
Win32.CheckCall(n);
}
}
}