diff --git a/ClassicShutdown.c b/ClassicShutdown.c index 1fef0c7..ad55772 100644 --- a/ClassicShutdown.c +++ b/ClassicShutdown.c @@ -1,5 +1,6 @@ #include #include +#include #include "resource.h" @@ -66,18 +67,6 @@ VOID MoveChildren(HWND hWnd, INT dx, INT dy) SWP_NOZORDER | SWP_NOMOVE); } -void ExecuteShutdown(LPCWSTR pczOptions) -{ - ShellExecuteW( - NULL, - L"open", - L"C:\\Windows\\System32\\shutdown.exe", - pczOptions, - NULL, - SW_HIDE - ); -} - void ScreenshotDesktop(void) { HDC hScreenDC = GetDC(NULL); @@ -174,7 +163,7 @@ BOOL CALLBACK ExitWindowsDlgProc( } HWND hComboBox = GetDlgItem(hWnd, IDD_EXITWINDOWS_COMBOBOX); - WCHAR szLogoffFormat[64], szLogoff[300], szShutdown[64], szRestart[64], szUsername[UNLEN + 1]; + WCHAR szLogoffFormat[64], szLogoff[300], szShutdown[64], szRestart[64], szStandby[64], szLock[64], szUsername[UNLEN + 1]; LoadStringW(g_hAppInstance, IDS_LOGOFF, szLogoffFormat, 64); @@ -185,10 +174,14 @@ BOOL CALLBACK ExitWindowsDlgProc( LoadStringW(g_hAppInstance, IDS_SHUTDOWN, szShutdown, 64); LoadStringW(g_hAppInstance, IDS_RESTART, szRestart, 64); + LoadStringW(g_hAppInstance, IDS_STANDBY, szStandby, 64); + LoadStringW(g_hAppInstance, IDS_LOCK, szLock, 64); SendMessageW(hComboBox, CB_ADDSTRING, 0, (LPARAM)szLogoff); SendMessageW(hComboBox, CB_ADDSTRING, 0, (LPARAM)szShutdown); SendMessageW(hComboBox, CB_ADDSTRING, 0, (LPARAM)szRestart); + SendMessageW(hComboBox, CB_ADDSTRING, 0, (LPARAM)szStandby); + SendMessageW(hComboBox, CB_ADDSTRING, 0, (LPARAM)szLock); SendMessageW(hComboBox, CB_SETCURSEL, 1, 0); WCHAR szShutdownDesc[256]; @@ -248,6 +241,12 @@ BOOL CALLBACK ExitWindowsDlgProc( case 2: uStringId = IDS_RESTART_DESC; break; + case 3: + uStringId = IDS_STANDBY_DESC; + break; + case 4: + uStringId = IDS_LOCK_DESC; + break; } if (uStringId != NULL) @@ -272,21 +271,31 @@ BOOL CALLBACK ExitWindowsDlgProc( ); switch (i) { + /* Log off */ case 0: - ShellExecuteW( - NULL, - L"open", - L"C:\\Windows\\System32\\logoff.exe", + ExitWindowsEx(EWX_LOGOFF, 0); + break; + /* Shut down */ + case 1: + InitiateSystemShutdownW( NULL, NULL, - SW_HIDE + 0, + FALSE, + FALSE ); break; - case 1: - ExecuteShutdown(L"-s -t 0"); - break; + /* Restart */ case 2: - ExecuteShutdown(L"-r -t 0"); + ExitWindowsEx(EWX_REBOOT, 0); + break; + /* Stand by */ + case 3: + SetSuspendState(FALSE, TRUE, FALSE); + break; + /* Lock */ + case 4: + LockWorkStation(); break; } EndDialog(hWnd, 0); @@ -353,14 +362,7 @@ BOOL CALLBACK LogoffDlgProc( switch (wParam) { case IDOK: - ShellExecuteW( - NULL, - L"open", - L"C:\\Windows\\System32\\logoff.exe", - NULL, - NULL, - SW_HIDE - ); + ExitWindowsEx(EWX_LOGOFF, 0); case IDCANCEL: EndDialog(hWnd, 0); } @@ -411,6 +413,20 @@ int WINAPI wWinMain( _In_ int nCmdShow ) { + /* Apply the needed privilege for shutting down */ + HANDLE hToken; + TOKEN_PRIVILEGES tp; + LUID luid; + + OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken); + LookupPrivilegeValueW(NULL, SE_SHUTDOWN_NAME, &luid); + + tp.PrivilegeCount = 1; + tp.Privileges[0].Luid = luid; + tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; + + AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL); + g_hAppInstance = hInstance; g_hShell32 = LoadLibraryW(L"shell32.dll"); diff --git a/ClassicShutdown.rc b/ClassicShutdown.rc index 3d7a488..e135675 100644 --- a/ClassicShutdown.rc +++ b/ClassicShutdown.rc @@ -1,18 +1,18 @@ #include #include "resource.h" -IDI_CLASSICSHUTDOWN ICON "ClassicShutdown.ico" +IDI_CLASSICSHUTDOWN ICON "ClassicShutdown.ico" IDD_EXITWINDOWS DIALOG 0, 0, 274, 103 STYLE DS_SHELLFONT | DS_MODALFRAME | DS_CENTER | WS_POPUP | WS_CAPTION | WS_SYSMENU CAPTION "Shut Down Windows" LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US -FONT 8, "MS Shell Dlg" +FONT 8, "MS Shell Dlg 2" { CONTROL 0, IDD_EXITWINDOWS_ICON, STATIC, SS_ICON | WS_CHILD | WS_VISIBLE | WS_GROUP, 7, 7, 20, 20 CONTROL "What do you want the computer to do?", -1, STATIC, SS_LEFT | WS_CHILD | WS_VISIBLE | WS_GROUP, 39, 7, 153, 10 CONTROL "", IDD_EXITWINDOWS_COMBOBOX, COMBOBOX, CBS_DROPDOWNLIST | WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_TABSTOP, 39, 20, 180, 57 - CONTROL "", IDD_EXITWINDOWS_LABEL, STATIC, SS_LEFT | WS_CHILD | WS_VISIBLE | WS_GROUP, 39, 40, 165, 33 + CONTROL "", IDD_EXITWINDOWS_LABEL, STATIC, SS_LEFT | WS_CHILD | WS_VISIBLE | WS_GROUP, 39, 40, 180, 33 CONTROL "OK", IDOK, BUTTON, BS_DEFPUSHBUTTON | WS_CHILD | WS_VISIBLE | WS_GROUP | WS_TABSTOP, 108, 82, 50, 14 CONTROL "Cancel", IDCANCEL, BUTTON, BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE | WS_TABSTOP, 162, 82, 50, 14 CONTROL "&Help", IDHELP, BUTTON, BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE | WS_TABSTOP, 216, 82, 50, 14 @@ -21,7 +21,7 @@ FONT 8, "MS Shell Dlg" IDD_LOGOFFWINDOWS DIALOGEX 0, 0, 188, 60 STYLE DS_SHELLFONT | DS_MODALFRAME | DS_CENTER | WS_POPUP | WS_CAPTION | WS_SYSMENU CAPTION "Log Off Windows" -FONT 8, "MS Shell Dlg" +FONT 8, "MS Shell Dlg 2" { CONTROL 0, IDD_LOGOFFWINDOWS_ICON, STATIC, SS_ICON | WS_CHILD | WS_VISIBLE, 8, 6, 18, 20 CONTROL "Are you sure you want to log off?", -1, STATIC, SS_LEFT | WS_CHILD | WS_VISIBLE | WS_GROUP, 49, 15, 131, 8 @@ -31,10 +31,14 @@ FONT 8, "MS Shell Dlg" STRINGTABLE { - IDS_LOGOFF "Log off %s" - IDS_LOGOFF_DESC "Ends your session, leaving the computer running on full power." - IDS_SHUTDOWN "Shut down" + IDS_LOGOFF "Log off %s" + IDS_LOGOFF_DESC "Ends your session, leaving the computer running on full power." + IDS_SHUTDOWN "Shut down" IDS_SHUTDOWN_DESC "Ends your session and shuts down Windows so that you can safely turn off power." - IDS_RESTART "Restart" - IDS_RESTART_DESC "Ends your session, shuts down Windows, and starts Windows again." + IDS_RESTART "Restart" + IDS_RESTART_DESC "Ends your session, shuts down Windows, and starts Windows again." + IDS_STANDBY "Stand by" + IDS_STANDBY_DESC "Maintains your session, keeping the computer running on low power with data still in memory." + IDS_LOCK "Lock" + IDS_LOCK_DESC "Maintains your session, requiring you to log in to access your desktop again." } \ No newline at end of file diff --git a/ClassicShutdown.vcxproj b/ClassicShutdown.vcxproj index da55e91..10a2b91 100644 --- a/ClassicShutdown.vcxproj +++ b/ClassicShutdown.vcxproj @@ -81,6 +81,7 @@ Windows true "type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'";%(AdditionalManifestDependencies) + powrprof.lib;%(AdditionalDependencies) @@ -98,6 +99,7 @@ true true "type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'";%(AdditionalManifestDependencies) + powrprof.lib;%(AdditionalDependencies) @@ -111,6 +113,7 @@ Windows true "type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'";%(AdditionalManifestDependencies) + powrprof.lib;%(AdditionalDependencies) @@ -128,6 +131,7 @@ true true "type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'";%(AdditionalManifestDependencies) + powrprof.lib;%(AdditionalDependencies) diff --git a/resource.h b/resource.h index 0834cb1..ecda2e6 100644 --- a/resource.h +++ b/resource.h @@ -14,3 +14,7 @@ #define IDS_SHUTDOWN_DESC 303 #define IDS_RESTART 304 #define IDS_RESTART_DESC 305 +#define IDS_STANDBY 306 +#define IDS_STANDBY_DESC 307 +#define IDS_LOCK 308 +#define IDS_LOCK_DESC 309