Skip to content

Commit

Permalink
Try to fix required process privileges for shutdown.
Browse files Browse the repository at this point in the history
  • Loading branch information
gigaherz committed Apr 15, 2017
1 parent 993637c commit b61ded7
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 16 deletions.
7 changes: 4 additions & 3 deletions NPowerTray/NPowerTray.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>NPowerTray</RootNamespace>
<AssemblyName>NPowerTray</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<TargetFrameworkProfile>
</TargetFrameworkProfile>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup>
Expand Down Expand Up @@ -45,7 +46,7 @@
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<CodeAnalysisRuleSetDirectories>;C:\Program Files (x86)\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\\Rule Sets</CodeAnalysisRuleSetDirectories>
<CodeAnalysisRuleDirectories>;C:\Program Files (x86)\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\FxCop\\Rules</CodeAnalysisRuleDirectories>
<Prefer32Bit>false</Prefer32Bit>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
Expand Down
29 changes: 20 additions & 9 deletions NPowerTray/PowerActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,27 @@ namespace NPowerTray
{
internal static class PowerActions
{
private static bool TryAdjustTokenPrivileges()
{
var hToken = IntPtr.Zero;
var tkp = new NativeMethods.TokPriv1Luid { Count = 1, Attributes = NativeMethods.SePrivilegeEnabled };

// Get a token for this process.
if (!NativeMethods.OpenProcessToken(NativeMethods.GetCurrentProcess(),
NativeMethods.TokenAdjustPrivileges | NativeMethods.TokenQuery, ref hToken))
return false;

NativeMethods.LookupPrivilegeValue(null, NativeMethods.SeShutdownName, ref tkp.Luid);
NativeMethods.AdjustTokenPrivileges(hToken, false, ref tkp, 0, IntPtr.Zero, IntPtr.Zero);

return true;
}

public static void Shutdown(ShutdownMode shutdownMode)
{
if (!TryAdjustTokenPrivileges())
return;

var mcWin32 = new ManagementClass("Win32_OperatingSystem");
mcWin32.Get();

Expand All @@ -34,17 +53,9 @@ public static void ShutdownHybrid()
if (osv.Platform != PlatformID.Win32NT || osv.Version < new Version(6, 2))
return;

var hToken = IntPtr.Zero;
var tkp = new NativeMethods.TokPriv1Luid { Count = 1, Attributes = NativeMethods.SePrivilegeEnabled };

// Get a token for this process.

if (!NativeMethods.OpenProcessToken(NativeMethods.GetCurrentProcess(),
NativeMethods.TokenAdjustPrivileges | NativeMethods.TokenQuery, ref hToken))
if (!TryAdjustTokenPrivileges())
return;

NativeMethods.LookupPrivilegeValue(null, NativeMethods.SeShutdownName, ref tkp.Luid);
NativeMethods.AdjustTokenPrivileges(hToken, false, ref tkp, 0, IntPtr.Zero, IntPtr.Zero);
NativeMethods.InitiateShutdown(null, null, 0, InitiateShutdownFlags.Hybrid | InitiateShutdownFlags.PowerOff, 0x80000000);
}

Expand Down
4 changes: 2 additions & 2 deletions NPowerTray/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.6.3.0")]
[assembly: AssemblyFileVersion("1.6.3.0")]
[assembly: AssemblyVersion("1.6.3.1")]
[assembly: AssemblyFileVersion("1.6.3.1")]
[assembly: NeutralResourcesLanguageAttribute("en")]
2 changes: 1 addition & 1 deletion NPowerTray/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion NPowerTray/app.config
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?xml version="1.0"?>
<configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client"/></startup></configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/></startup></configuration>

0 comments on commit b61ded7

Please sign in to comment.