From 738c8fc27c3ba0717439a7a9250a160964e0de20 Mon Sep 17 00:00:00 2001 From: Whindmar Saksit Date: Wed, 17 Jul 2024 12:42:22 +0200 Subject: [PATCH] [EXPLORER] Execute RunOnce[Ex] before desktop is created (#7143) CORE-19501 --- base/shell/explorer/explorer.cpp | 3 +++ base/shell/explorer/precomp.h | 7 ++++-- base/shell/explorer/startup.cpp | 39 ++++++++++++++++++++++---------- 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/base/shell/explorer/explorer.cpp b/base/shell/explorer/explorer.cpp index 78c308839c9..a88cf050eea 100644 --- a/base/shell/explorer/explorer.cpp +++ b/base/shell/explorer/explorer.cpp @@ -154,6 +154,8 @@ StartWithDesktop(IN HINSTANCE hInstance) buttons to work right) */ HideMinimizedWindows(TRUE); + ProcessRunOnceItems(); // Must be executed before the desktop is created + HANDLE hShellDesktop = NULL; if (Tray != NULL) hShellDesktop = DesktopCreateWindow(Tray); @@ -167,6 +169,7 @@ StartWithDesktop(IN HINSTANCE hInstance) ProcessStartupItems(); DoFinishStartupItems(); } + ReleaseStartupMutex(); // For ProcessRunOnceItems #endif if (Tray != NULL) diff --git a/base/shell/explorer/precomp.h b/base/shell/explorer/precomp.h index 5981b8f2329..fa6a34df2b6 100644 --- a/base/shell/explorer/precomp.h +++ b/base/shell/explorer/precomp.h @@ -252,9 +252,12 @@ HRESULT ShutdownShellServices(HDPA hdpa); * startup.cpp */ +VOID ReleaseStartupMutex(); +VOID ProcessRunOnceItems(); BOOL DoStartStartupItems(ITrayWindow *Tray); -INT ProcessStartupItems(VOID); -BOOL DoFinishStartupItems(VOID); +INT ProcessStartupItems(BOOL bRunOnce); +static inline INT ProcessStartupItems() { return ProcessStartupItems(FALSE); } +static inline VOID DoFinishStartupItems() { ReleaseStartupMutex(); } /* * trayprop.h diff --git a/base/shell/explorer/startup.cpp b/base/shell/explorer/startup.cpp index e76a9cd6e35..8dea2fc48cd 100644 --- a/base/shell/explorer/startup.cpp +++ b/base/shell/explorer/startup.cpp @@ -450,7 +450,7 @@ AutoStartupApplications(INT nCSIDL_Folder) return TRUE; } -INT ProcessStartupItems(VOID) +INT ProcessStartupItems(BOOL bRunOnce) { /* TODO: ProcessRunKeys already checks SM_CLEANBOOT -- items prefixed with * should probably run even in safe mode */ BOOL bNormalBoot = GetSystemMetrics(SM_CLEANBOOT) == 0; /* Perform the operations that are performed every boot */ @@ -478,11 +478,16 @@ INT ProcessStartupItems(VOID) */ res = TRUE; - if (res && bNormalBoot) - ProcessRunOnceEx(HKEY_LOCAL_MACHINE); + if (bRunOnce) + { + if (res && bNormalBoot) + ProcessRunOnceEx(HKEY_LOCAL_MACHINE); - if (res && (SHRestricted(REST_NOLOCALMACHINERUNONCE) == 0)) - res = ProcessRunKeys(HKEY_LOCAL_MACHINE, L"RunOnce", TRUE, TRUE); + if (res && (SHRestricted(REST_NOLOCALMACHINERUNONCE) == 0)) + res = ProcessRunKeys(HKEY_LOCAL_MACHINE, L"RunOnce", TRUE, TRUE); + + return !res; + } if (res && bNormalBoot && (SHRestricted(REST_NOLOCALMACHINERUN) == 0)) res = ProcessRunKeys(HKEY_LOCAL_MACHINE, L"Run", FALSE, FALSE); @@ -505,7 +510,7 @@ INT ProcessStartupItems(VOID) return res ? 0 : 101; } -BOOL DoFinishStartupItems(VOID) +VOID ReleaseStartupMutex() { if (s_hStartupMutex) { @@ -513,13 +518,10 @@ BOOL DoFinishStartupItems(VOID) CloseHandle(s_hStartupMutex); s_hStartupMutex = NULL; } - return TRUE; } -BOOL DoStartStartupItems(ITrayWindow *Tray) +static BOOL InitializeStartupMutex() { - DWORD dwWait; - if (!bExplorerIsShell) return FALSE; @@ -532,15 +534,22 @@ BOOL DoStartStartupItems(ITrayWindow *Tray) return FALSE; } - dwWait = WaitForSingleObject(s_hStartupMutex, INFINITE); + DWORD dwWait = WaitForSingleObject(s_hStartupMutex, INFINITE); TRACE("dwWait: 0x%08lX\n", dwWait); if (dwWait != WAIT_OBJECT_0) { TRACE("LastError: %ld\n", GetLastError()); - DoFinishStartupItems(); + ReleaseStartupMutex(); return FALSE; } + return TRUE; +} + +BOOL DoStartStartupItems(ITrayWindow *Tray) +{ + if (!bExplorerIsShell || !InitializeStartupMutex()) + return FALSE; const DWORD dwWaitTotal = 3000; // in milliseconds DWORD dwTick = GetTickCount(); @@ -575,3 +584,9 @@ BOOL DoStartStartupItems(ITrayWindow *Tray) return TRUE; } + +VOID ProcessRunOnceItems() +{ + if (bExplorerIsShell && IsUserAnAdmin() && InitializeStartupMutex()) + ProcessStartupItems(TRUE); +}