Add power actions to shutdown/restart menus

This commit is contained in:
dmex
2021-04-27 17:21:21 +10:00
parent 715c0498be
commit b7f1dd7703
4 changed files with 222 additions and 81 deletions
+163 -71
View File
@@ -573,108 +573,200 @@ BOOLEAN PhUiHibernateComputer(
}
BOOLEAN PhUiRestartComputer(
_In_ HWND hWnd,
_In_ HWND WindowHandle,
_In_ PH_POWERACTION_TYPE Action,
_In_ ULONG Flags
)
{
ULONG status;
BOOLEAN forceShutdown;
// Force shutdown when holding the control key. (dmex)
forceShutdown = !!(GetKeyState(VK_CONTROL) < 0);
if (!PhGetIntegerSetting(L"EnableWarnings") || PhShowConfirmMessage(
hWnd,
L"restart",
L"the computer",
NULL,
FALSE
))
switch (Action)
{
if (forceShutdown)
case PH_POWERACTION_TYPE_WIN32:
{
status = NtShutdownSystem(ShutdownReboot);
if (!PhGetIntegerSetting(L"EnableWarnings") || PhShowConfirmMessage(
WindowHandle,
L"restart",
L"the computer",
NULL,
FALSE
))
{
ULONG status;
status = InitiateShutdown(
NULL,
NULL,
0,
SHUTDOWN_RESTART | Flags,
SHTDN_REASON_FLAG_PLANNED
);
if (NT_SUCCESS(status))
return TRUE;
if (status == ERROR_SUCCESS)
return TRUE;
PhShowStatus(hWnd, L"Unable to restart the computer.", status, 0);
PhShowStatus(WindowHandle, L"Unable to restart the computer.", 0, status);
//if (ExitWindowsEx(EWX_REBOOT | EWX_BOOTOPTIONS, 0))
// return TRUE;
//else
// PhShowStatus(WindowHandle, L"Unable to restart the computer.", 0, GetLastError());
}
}
else
break;
case PH_POWERACTION_TYPE_NATIVE:
{
status = InitiateShutdown(
NULL,
NULL,
0,
SHUTDOWN_RESTART | Flags,
SHTDN_REASON_FLAG_PLANNED
);
// Ignore the EnableWarnings preference and always show the warning prompt. (dmex)
if (PhShowConfirmMessage(
WindowHandle,
L"restart",
L"the computer",
PhaFormatString(L"This option %s %s in an disorderly manner and may cause corrupted files or instability in the system.", L"preforms a hard", L"restart")->Buffer,
TRUE
))
{
NTSTATUS status;
if (status == ERROR_SUCCESS)
return TRUE;
status = NtShutdownSystem(ShutdownReboot);
PhShowStatus(hWnd, L"Unable to restart the computer.", 0, status);
if (NT_SUCCESS(status))
return TRUE;
//if (ExitWindowsEx(EWX_REBOOT | EWX_BOOTOPTIONS, 0))
// return TRUE;
//else
// PhShowStatus(hWnd, L"Unable to restart the computer.", 0, GetLastError());
PhShowStatus(WindowHandle, L"Unable to restart the computer.", status, 0);
}
}
break;
case PH_POWERACTION_TYPE_CRITICAL:
{
// Ignore the EnableWarnings preference and always show the warning prompt. (dmex)
if (PhShowConfirmMessage(
WindowHandle,
L"restart",
L"the computer",
PhaFormatString(L"This option %s %s in an disorderly manner and may cause corrupted files or instability in the system.", L"forces a critical", L"restart")->Buffer,
TRUE
))
{
NTSTATUS status;
status = NtSetSystemPowerState(
PowerActionShutdownReset,
PowerSystemShutdown,
POWER_ACTION_CRITICAL
);
//status = NtInitiatePowerAction(
// PowerActionShutdownReset,
// PowerSystemShutdown,
// POWER_ACTION_CRITICAL,
// FALSE
// );
if (NT_SUCCESS(status))
return TRUE;
PhShowStatus(WindowHandle, L"Unable to restart the computer.", status, 0);
}
}
break;
}
return FALSE;
}
BOOLEAN PhUiShutdownComputer(
_In_ HWND hWnd,
_In_ HWND WindowHandle,
_In_ PH_POWERACTION_TYPE Action,
_In_ ULONG Flags
)
{
ULONG status;
BOOLEAN forceShutdown;
// Force shutdown when holding the control key. (dmex)
forceShutdown = !!(GetKeyState(VK_CONTROL) < 0);
if (!PhGetIntegerSetting(L"EnableWarnings") || PhShowConfirmMessage(
hWnd,
L"shut down",
L"the computer",
NULL,
FALSE
))
switch (Action)
{
if (forceShutdown)
case PH_POWERACTION_TYPE_WIN32:
{
status = NtShutdownSystem(ShutdownPowerOff);
if (!NT_SUCCESS(status))
if (!PhGetIntegerSetting(L"EnableWarnings") || PhShowConfirmMessage(
WindowHandle,
L"shut down",
L"the computer",
NULL,
FALSE
))
{
PhShowStatus(hWnd, L"Unable to shut down the computer.", status, 0);
ULONG status;
status = InitiateShutdown(
NULL,
NULL,
0,
SHUTDOWN_POWEROFF | Flags,
SHTDN_REASON_FLAG_PLANNED
);
if (status == ERROR_SUCCESS)
return TRUE;
PhShowStatus(WindowHandle, L"Unable to shut down the computer.", 0, status);
//if (ExitWindowsEx(EWX_POWEROFF | EWX_HYBRID_SHUTDOWN, 0))
// return TRUE;
//else if (ExitWindowsEx(EWX_SHUTDOWN | EWX_HYBRID_SHUTDOWN, 0))
// return TRUE;
//else
// PhShowStatus(WindowHandle, L"Unable to shut down the computer.", 0, GetLastError());
}
}
else
break;
case PH_POWERACTION_TYPE_NATIVE:
{
status = InitiateShutdown(
NULL,
NULL,
0,
SHUTDOWN_POWEROFF | Flags,
SHTDN_REASON_FLAG_PLANNED
);
// Ignore the EnableWarnings preference and always show the warning prompt. (dmex)
if (PhShowConfirmMessage(
WindowHandle,
L"shut down",
L"the computer",
PhaFormatString(L"This option %s %s in an disorderly manner and may cause corrupted files or instability in the system.", L"preforms a hard", L"shut down")->Buffer,
TRUE
))
{
NTSTATUS status;
status = NtShutdownSystem(ShutdownPowerOff);
if (status == ERROR_SUCCESS)
return TRUE;
if (NT_SUCCESS(status))
return TRUE;
PhShowStatus(hWnd, L"Unable to shut down the computer.", 0, status);
//if (ExitWindowsEx(EWX_POWEROFF | EWX_HYBRID_SHUTDOWN, 0))
// return TRUE;
//else if (ExitWindowsEx(EWX_SHUTDOWN | EWX_HYBRID_SHUTDOWN, 0))
// return TRUE;
//else
// PhShowStatus(hWnd, L"Unable to shut down the computer.", 0, GetLastError());
PhShowStatus(WindowHandle, L"Unable to shut down the computer.", status, 0);
}
}
break;
case PH_POWERACTION_TYPE_CRITICAL:
{
// Ignore the EnableWarnings preference and always show the warning prompt. (dmex)
if (PhShowConfirmMessage(
WindowHandle,
L"shut down",
L"the computer",
PhaFormatString(L"This option %s %s in an disorderly manner and may cause corrupted files or instability in the system.", L"forces a critical", L"shut down")->Buffer,
TRUE
))
{
NTSTATUS status;
status = NtSetSystemPowerState(
PowerActionShutdownOff,
PowerSystemShutdown,
POWER_ACTION_CRITICAL
);
//status = NtInitiatePowerAction(
// PowerActionShutdownReset,
// PowerSystemShutdown,
// POWER_ACTION_CRITICAL,
// FALSE
// );
if (NT_SUCCESS(status))
return TRUE;
PhShowStatus(WindowHandle, L"Unable to shut down the computer.", status, 0);
}
}
break;
}
return FALSE;
+13 -2
View File
@@ -72,11 +72,21 @@ PhUiHibernateComputer(
_In_ HWND hWnd
);
typedef enum _PH_POWERACTION_TYPE
{
PH_POWERACTION_TYPE_NONE,
PH_POWERACTION_TYPE_WIN32,
PH_POWERACTION_TYPE_NATIVE,
PH_POWERACTION_TYPE_CRITICAL,
PH_POWERACTION_TYPE_MAXIMUM
} PH_POWERACTION_TYPE;
PHAPPAPI
BOOLEAN
NTAPI
PhUiRestartComputer(
_In_ HWND hWnd,
_In_ HWND WindowHandle,
_In_ PH_POWERACTION_TYPE Action,
_In_ ULONG Flags
);
@@ -84,7 +94,8 @@ PHAPPAPI
BOOLEAN
NTAPI
PhUiShutdownComputer(
_In_ HWND hWnd,
_In_ HWND WindowHandle,
_In_ PH_POWERACTION_TYPE Action,
_In_ ULONG Flags
);
+38 -4
View File
@@ -730,6 +730,10 @@ VOID PhMwpOnCommand(
case ID_COMPUTER_RESTARTBOOTOPTIONS:
case ID_COMPUTER_SHUTDOWN:
case ID_COMPUTER_SHUTDOWNHYBRID:
case ID_COMPUTER_RESTART_NATIVE:
case ID_COMPUTER_SHUTDOWN_NATIVE:
case ID_COMPUTER_RESTART_CRITICAL:
case ID_COMPUTER_SHUTDOWN_CRITICAL:
PhMwpExecuteComputerCommand(WindowHandle, Id);
break;
case ID_HACKER_EXIT:
@@ -2077,16 +2081,28 @@ BOOLEAN PhMwpExecuteComputerCommand(
PhUiHibernateComputer(WindowHandle);
return TRUE;
case ID_COMPUTER_RESTART:
PhUiRestartComputer(WindowHandle, 0);
PhUiRestartComputer(WindowHandle, PH_POWERACTION_TYPE_WIN32, 0);
return TRUE;
case ID_COMPUTER_RESTARTBOOTOPTIONS:
PhUiRestartComputer(WindowHandle, SHUTDOWN_RESTART_BOOTOPTIONS);
PhUiRestartComputer(WindowHandle, PH_POWERACTION_TYPE_WIN32, SHUTDOWN_RESTART_BOOTOPTIONS);
return TRUE;
case ID_COMPUTER_SHUTDOWN:
PhUiShutdownComputer(WindowHandle, 0);
PhUiShutdownComputer(WindowHandle, PH_POWERACTION_TYPE_WIN32, 0);
return TRUE;
case ID_COMPUTER_SHUTDOWNHYBRID:
PhUiShutdownComputer(WindowHandle, SHUTDOWN_HYBRID);
PhUiShutdownComputer(WindowHandle, PH_POWERACTION_TYPE_WIN32, SHUTDOWN_HYBRID);
return TRUE;
case ID_COMPUTER_RESTART_NATIVE:
PhUiRestartComputer(WindowHandle, PH_POWERACTION_TYPE_NATIVE, 0);
return TRUE;
case ID_COMPUTER_SHUTDOWN_NATIVE:
PhUiShutdownComputer(WindowHandle, PH_POWERACTION_TYPE_NATIVE, 0);
return TRUE;
case ID_COMPUTER_RESTART_CRITICAL:
PhUiRestartComputer(WindowHandle, PH_POWERACTION_TYPE_CRITICAL, 0);
return TRUE;
case ID_COMPUTER_SHUTDOWN_CRITICAL:
PhUiShutdownComputer(WindowHandle, PH_POWERACTION_TYPE_CRITICAL, 0);
return TRUE;
}
@@ -2173,6 +2189,15 @@ PPH_EMENU PhpCreateHackerMenu(
PhInsertEMenuItem(menuItem, PhCreateEMenuItem(0, ID_COMPUTER_RESTARTBOOTOPTIONS, L"Restart to boot options", NULL, NULL), ULONG_MAX);
PhInsertEMenuItem(menuItem, PhCreateEMenuItem(0, ID_COMPUTER_SHUTDOWN, L"Shu&t down", NULL, NULL), ULONG_MAX);
PhInsertEMenuItem(menuItem, PhCreateEMenuItem(0, ID_COMPUTER_SHUTDOWNHYBRID, L"H&ybrid shut down", NULL, NULL), ULONG_MAX);
if (PhGetIntegerSetting(L"EnableShutdownCriticalMenu"))
{
PhInsertEMenuItem(menuItem, PhCreateEMenuSeparator(), ULONG_MAX);
PhInsertEMenuItem(menuItem, PhCreateEMenuItem(0, ID_COMPUTER_RESTART_NATIVE, L"R&estart (Native)", NULL, NULL), ULONG_MAX);
PhInsertEMenuItem(menuItem, PhCreateEMenuItem(0, ID_COMPUTER_SHUTDOWN_NATIVE, L"Shu&t down (Native)", NULL, NULL), ULONG_MAX);
PhInsertEMenuItem(menuItem, PhCreateEMenuSeparator(), ULONG_MAX);
PhInsertEMenuItem(menuItem, PhCreateEMenuItem(0, ID_COMPUTER_RESTART_CRITICAL, L"R&estart (Critical)", NULL, NULL), ULONG_MAX);
PhInsertEMenuItem(menuItem, PhCreateEMenuItem(0, ID_COMPUTER_SHUTDOWN_CRITICAL, L"Shu&t down (Critical)", NULL, NULL), ULONG_MAX);
}
PhInsertEMenuItem(HackerMenu, menuItem, ULONG_MAX);
PhInsertEMenuItem(HackerMenu, PhCreateEMenuItem(0, ID_HACKER_EXIT, L"E&xit", NULL, NULL), ULONG_MAX);
@@ -2719,6 +2744,15 @@ PPH_EMENU PhpCreateIconMenu(
PhInsertEMenuItem(menuItem, PhCreateEMenuItem(0, ID_COMPUTER_RESTARTBOOTOPTIONS, L"Restart to boot &options", NULL, NULL), ULONG_MAX);
PhInsertEMenuItem(menuItem, PhCreateEMenuItem(0, ID_COMPUTER_SHUTDOWN, L"Shu&t down", NULL, NULL), ULONG_MAX);
PhInsertEMenuItem(menuItem, PhCreateEMenuItem(0, ID_COMPUTER_SHUTDOWNHYBRID, L"H&ybrid shut down", NULL, NULL), ULONG_MAX);
if (PhGetIntegerSetting(L"EnableShutdownCriticalMenu"))
{
PhInsertEMenuItem(menuItem, PhCreateEMenuSeparator(), ULONG_MAX);
PhInsertEMenuItem(menuItem, PhCreateEMenuItem(0, ID_COMPUTER_RESTART_NATIVE, L"R&estart (Native)", NULL, NULL), ULONG_MAX);
PhInsertEMenuItem(menuItem, PhCreateEMenuItem(0, ID_COMPUTER_SHUTDOWN_NATIVE, L"Shu&t down (Native)", NULL, NULL), ULONG_MAX);
PhInsertEMenuItem(menuItem, PhCreateEMenuSeparator(), ULONG_MAX);
PhInsertEMenuItem(menuItem, PhCreateEMenuItem(0, ID_COMPUTER_RESTART_CRITICAL, L"R&estart (Critical)", NULL, NULL), ULONG_MAX);
PhInsertEMenuItem(menuItem, PhCreateEMenuItem(0, ID_COMPUTER_SHUTDOWN_CRITICAL, L"Shu&t down (Critical)", NULL, NULL), ULONG_MAX);
}
PhInsertEMenuItem(menu, menuItem, ULONG_MAX);
PhInsertEMenuItem(menu, PhCreateEMenuItem(0, ID_ICON_EXIT, L"E&xit", NULL, NULL), ULONG_MAX);
+8 -4
View File
@@ -658,10 +658,14 @@
#define ID_COMPUTER_HIBERNATE 40163
#define ID_COMPUTER_RESTART 40164
#define ID_COMPUTER_SHUTDOWN 40165
#define ID_TRAYICONS_IOHISTORY 40168
#define ID_ICON_EXIT 40169
#define ID_ICON_SHOWHIDEPROCESSHACKER 40171
#define ID_ICON_SYSTEMINFORMATION 40172
#define ID_COMPUTER_RESTART_NATIVE 40166
#define ID_COMPUTER_SHUTDOWN_NATIVE 40167
#define ID_COMPUTER_RESTART_CRITICAL 40168
#define ID_COMPUTER_SHUTDOWN_CRITICAL 40169
#define ID_TRAYICONS_IOHISTORY 40170
#define ID_ICON_EXIT 40171
#define ID_ICON_SHOWHIDEPROCESSHACKER 40172
#define ID_ICON_SYSTEMINFORMATION 40173
#define ID_PROCESSES_DUMMY 40177
#define ID_NOTIFICATIONS_ENABLEALL 40178
#define ID_NOTIFICATIONS_DISABLEALL 40179