@@ -61,17 +61,24 @@ static INT_PTR CALLBACK RenameDlgProc( HWND hwndDlg, UINT uMsg, WPARAM wParam, L
61
61
return FALSE ;
62
62
}
63
63
64
- static void SetShutdownPrivileges ( void )
64
+ static bool SetShutdownPrivileges ()
65
65
{
66
+ bool retval = false ;
67
+
66
68
HANDLE hToken;
67
- if (OpenProcessToken (GetCurrentProcess (),TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,&hToken))
69
+ if (OpenProcessToken (GetCurrentProcess (), TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY, &hToken))
68
70
{
69
71
TOKEN_PRIVILEGES tp={1 };
70
- if (LookupPrivilegeValue (NULL ,L" SeShutdownPrivilege" ,&tp.Privileges [0 ].Luid ))
71
- tp.Privileges [0 ].Attributes =SE_PRIVILEGE_ENABLED;
72
- AdjustTokenPrivileges (hToken,FALSE ,&tp,sizeof (TOKEN_PRIVILEGES),NULL ,NULL );
72
+ if (LookupPrivilegeValue (NULL , L" SeShutdownPrivilege" , &tp.Privileges [0 ].Luid ))
73
+ {
74
+ tp.Privileges [0 ].Attributes = SE_PRIVILEGE_ENABLED;
75
+ if (AdjustTokenPrivileges (hToken, FALSE , &tp, sizeof (TOKEN_PRIVILEGES), NULL , NULL ) && GetLastError () == ERROR_SUCCESS)
76
+ retval = true ;
77
+ }
73
78
CloseHandle (hToken);
74
79
}
80
+
81
+ return retval;
75
82
}
76
83
77
84
static void DoSearchSubst ( wchar_t *buf, int size, const wchar_t *search )
@@ -658,6 +665,23 @@ class ExitGuard
658
665
bool m_bArmed;
659
666
};
660
667
668
+ static TOKEN_ELEVATION_TYPE GetCurrentTokenElevationType ()
669
+ {
670
+ TOKEN_ELEVATION_TYPE retval = TokenElevationTypeDefault;
671
+
672
+ HANDLE token;
673
+ if (OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY, &token))
674
+ {
675
+ TOKEN_ELEVATION_TYPE elevationType;
676
+ DWORD returnLength;
677
+ if (GetTokenInformation (token, TokenElevationType, &elevationType, sizeof (elevationType), &returnLength) && returnLength == sizeof (elevationType))
678
+ retval = elevationType;
679
+
680
+ CloseHandle (token);
681
+ }
682
+
683
+ return retval;
684
+ }
661
685
662
686
static bool ExecuteShutdownCommand (TMenuID menuCommand)
663
687
{
@@ -714,8 +738,29 @@ static bool ExecuteShutdownCommand(TMenuID menuCommand)
714
738
715
739
if (flags)
716
740
{
717
- SetShutdownPrivileges ();
718
- InitiateShutdown (NULL , NULL , 0 , flags, SHTDN_REASON_FLAG_PLANNED);
741
+ if (SetShutdownPrivileges ())
742
+ {
743
+ InitiateShutdown (NULL , NULL , 0 , flags, SHTDN_REASON_FLAG_PLANNED);
744
+ }
745
+ else
746
+ {
747
+ // we don't have shutdown rights
748
+ // lets try silent elevate via SystemSettingsAdminFlows (for limited admin users only)
749
+ if (GetCurrentTokenElevationType () == TokenElevationTypeLimited)
750
+ {
751
+ wchar_t cmdLine[32 ]{};
752
+ Sprintf (cmdLine, _countof (cmdLine), L" Shutdown %d %d" , flags, SHTDN_REASON_FLAG_PLANNED);
753
+
754
+ SHELLEXECUTEINFO sei{};
755
+ sei.cbSize = sizeof (sei);
756
+ sei.lpFile = L" %systemroot%\\ system32\\ SystemSettingsAdminFlows.exe" ;
757
+ sei.lpParameters = cmdLine;
758
+ sei.lpVerb = L" runas" ;
759
+ sei.fMask = SEE_MASK_DOENVSUBST | SEE_MASK_FLAG_NO_UI;
760
+
761
+ ShellExecuteEx (&sei);
762
+ }
763
+ }
719
764
720
765
return true ;
721
766
}
0 commit comments