From 45e4a6251b0bfd934aa76218ce5d36d4da711417 Mon Sep 17 00:00:00 2001 From: anders_k Date: Fri, 27 Jun 2014 16:30:16 +0000 Subject: [PATCH] InitiateShutdown is used to reboot the machine if available (patch #247) git-svn-id: https://svn.code.sf.net/p/nsis/code/NSIS/trunk@6506 212acab6-be3b-0410-9dea-997c60f758d6 --- Docs/src/history.but | 2 ++ Source/exehead/Main.c | 26 +++++++++++++++++++++++++- Source/exehead/util.c | 9 +++++---- Source/exehead/util.h | 3 ++- 4 files changed, 34 insertions(+), 6 deletions(-) diff --git a/Docs/src/history.but b/Docs/src/history.but index 958e2b05..3d227dfb 100644 --- a/Docs/src/history.but +++ b/Docs/src/history.but @@ -10,6 +10,8 @@ Released on ?, 2014 \b Added \R{makensis}{!makensis} command +\b InitiateShutdown is used to reboot the machine if available (\W{http://sf.net/p/nsis/patches/247}{patch #247}) + \b Added PPO and SafePPO preprocess-only compiler switches \b MakeNSIS WM_COPYDATA messages now use the QH_OUTPUTCHARSET encoding with CP_ACP as the default for compatibility with old IDEs. diff --git a/Source/exehead/Main.c b/Source/exehead/Main.c index 43fcf87f..44864ffa 100644 --- a/Source/exehead/Main.c +++ b/Source/exehead/Main.c @@ -28,6 +28,25 @@ #include "exec.h" #include "plugin.h" +#ifndef SHTDN_REASON_FLAG_PLANNED +#define SHTDN_REASON_FLAG_PLANNED 0x80000000 +#endif +#ifndef SHTDN_REASON_MAJOR_APPLICATION +#define SHTDN_REASON_MAJOR_APPLICATION 0x00040000 +#endif +#ifndef SHTDN_REASON_MINOR_INSTALLATION +#define SHTDN_REASON_MINOR_INSTALLATION 0x0002 +#endif +#ifndef SHUTDOWN_RESTART +#define SHUTDOWN_RESTART 0x00000004 +#endif +#ifndef SHUTDOWN_FORCE_OTHERS +#define SHUTDOWN_FORCE_OTHERS 0x00000001 +#endif +#ifndef SHUTDOWN_GRACE_OVERRIDE +#define SHUTDOWN_GRACE_OVERRIDE 0x00000020 +#endif + #if !defined(NSIS_CONFIG_VISIBLE_SUPPORT) && !defined(NSIS_CONFIG_SILENT_SUPPORT) #error One of NSIS_CONFIG_SILENT_SUPPORT or NSIS_CONFIG_VISIBLE_SUPPORT must be defined. #endif @@ -317,9 +336,11 @@ end: #ifdef NSIS_SUPPORT_REBOOT if (g_exec_flags.reboot_called) { + const DWORD reason = SHTDN_REASON_FLAG_PLANNED | SHTDN_REASON_MAJOR_APPLICATION | SHTDN_REASON_MINOR_INSTALLATION; BOOL (WINAPI *OPT)(HANDLE, DWORD,PHANDLE); BOOL (WINAPI *LPV)(LPCTSTR,LPCTSTR,PLUID); BOOL (WINAPI *ATP)(HANDLE,BOOL,PTOKEN_PRIVILEGES,DWORD,PTOKEN_PRIVILEGES,PDWORD); + BOOL (WINAPI *IS)(LPTSTR,LPTSTR,DWORD,DWORD,DWORD); #ifdef _WIN64 OPT=OpenProcessToken, LPV=LookupPrivilegeValue, ATP=AdjustTokenPrivileges; #else @@ -340,7 +361,10 @@ end: } } - if (!ExitWindowsEx(EWX_REBOOT,0)) + IS=myGetProcAddress(MGA_InitiateShutdown); + if (IS && !IS(NULL, NULL, 0, SHUTDOWN_RESTART | SHUTDOWN_FORCE_OTHERS | SHUTDOWN_GRACE_OVERRIDE, reason) + || !ExitWindowsEx(EWX_REBOOT, reason) + ) ExecuteCallbackFunction(CB_ONREBOOTFAILED); } #endif//NSIS_SUPPORT_REBOOT diff --git a/Source/exehead/util.c b/Source/exehead/util.c index 650d41b4..aa14d6a1 100644 --- a/Source/exehead/util.c +++ b/Source/exehead/util.c @@ -1031,29 +1031,30 @@ struct MGA_FUNC const char *func; }; -#ifdef _UNICODE struct MGA_FUNC MGA_FUNCS[] = { +#ifdef _UNICODE #ifndef _WIN64 {"KERNEL32", "GetDiskFreeSpaceExW"}, {"KERNEL32", "MoveFileExW"}, + {"KERNEL32", "GetUserDefaultUILanguage"}, {"ADVAPI32", "RegDeleteKeyExW"}, {"ADVAPI32", "OpenProcessToken"}, {"ADVAPI32", "LookupPrivilegeValueW"}, {"ADVAPI32", "AdjustTokenPrivileges"}, - {"KERNEL32", "GetUserDefaultUILanguage"}, #endif + {"ADVAPI32", "InitiateShutdownW"}, {"SHLWAPI", "SHAutoComplete"}, {"SHFOLDER", "SHGetFolderPathW"} }; #else -struct MGA_FUNC MGA_FUNCS[] = { {"KERNEL32", "GetDiskFreeSpaceExA"}, {"KERNEL32", "MoveFileExA"}, + {"KERNEL32", "GetUserDefaultUILanguage"}, {"ADVAPI32", "RegDeleteKeyExA"}, {"ADVAPI32", "OpenProcessToken"}, {"ADVAPI32", "LookupPrivilegeValueA"}, {"ADVAPI32", "AdjustTokenPrivileges"}, - {"KERNEL32", "GetUserDefaultUILanguage"}, + {"ADVAPI32", "InitiateShutdownA"}, {"SHLWAPI", "SHAutoComplete"}, {"SHFOLDER", "SHGetFolderPathA"} }; diff --git a/Source/exehead/util.h b/Source/exehead/util.h index 2ed6d5af..7578bfb1 100644 --- a/Source/exehead/util.h +++ b/Source/exehead/util.h @@ -120,12 +120,13 @@ enum myGetProcAddressFunctions { #ifndef _WIN64 MGA_GetDiskFreeSpaceEx, MGA_MoveFileEx, + MGA_GetUserDefaultUILanguage, MGA_RegDeleteKeyEx, MGA_OpenProcessToken, MGA_LookupPrivilegeValue, MGA_AdjustTokenPrivileges, - MGA_GetUserDefaultUILanguage, #endif + MGA_InitiateShutdown, MGA_SHAutoComplete, // x64 can link to shlwapi directly but as long as MGA_SHGetFolderPath is used we can stick with myGetProcAddress MGA_SHGetFolderPath, // TODO: This can probably call something else directly on x64 };