Skip to content

Commit

Permalink
Improve shutdown method, add Stand by and Lock options
Browse files Browse the repository at this point in the history
  • Loading branch information
aubymori committed Sep 30, 2023
1 parent 85e4baa commit b21603c
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 39 deletions.
76 changes: 46 additions & 30 deletions ClassicShutdown.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <windows.h>
#include <lmcons.h>
#include <powrprof.h>

#include "resource.h"

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand All @@ -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];
Expand Down Expand Up @@ -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)
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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");

Expand Down
22 changes: 13 additions & 9 deletions ClassicShutdown.rc
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
#include <windows.h>
#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
Expand All @@ -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
Expand All @@ -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."
}
4 changes: 4 additions & 0 deletions ClassicShutdown.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalManifestDependencies>"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'";%(AdditionalManifestDependencies)</AdditionalManifestDependencies>
<AdditionalDependencies>powrprof.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
Expand All @@ -98,6 +99,7 @@
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalManifestDependencies>"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'";%(AdditionalManifestDependencies)</AdditionalManifestDependencies>
<AdditionalDependencies>powrprof.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
Expand All @@ -111,6 +113,7 @@
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalManifestDependencies>"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'";%(AdditionalManifestDependencies)</AdditionalManifestDependencies>
<AdditionalDependencies>powrprof.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
Expand All @@ -128,6 +131,7 @@
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalManifestDependencies>"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'";%(AdditionalManifestDependencies)</AdditionalManifestDependencies>
<AdditionalDependencies>powrprof.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
Expand Down
4 changes: 4 additions & 0 deletions resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit b21603c

Please sign in to comment.