@@ -47,6 +47,69 @@ struct Chunk
4747 int start1, start2, len;
4848};
4949
50+ static bool IsExplorerShellRunning ( void )
51+ {
52+ HWND shellWindow=GetShellWindow ();
53+ if (!shellWindow)
54+ return false ;
55+
56+ DWORD shellProcessId=0 ;
57+ GetWindowThreadProcessId (shellWindow,&shellProcessId);
58+ if (!shellProcessId)
59+ return false ;
60+
61+ DWORD currentSession=0 ;
62+ DWORD shellSession=0 ;
63+ if (!ProcessIdToSessionId (GetCurrentProcessId (),¤tSession) ||
64+ !ProcessIdToSessionId (shellProcessId,&shellSession) ||
65+ currentSession!=shellSession)
66+ {
67+ return false ;
68+ }
69+
70+ HANDLE process=OpenProcess (PROCESS_QUERY_LIMITED_INFORMATION ,FALSE ,shellProcessId);
71+ if (!process)
72+ return false ;
73+
74+ wchar_t path[_MAX_PATH];
75+ DWORD pathSize=_countof (path);
76+ bool isExplorer=false ;
77+ if (QueryFullProcessImageName (process,0 ,path,&pathSize))
78+ isExplorer=_wcsicmp (PathFindFileName (path),L" explorer.exe" )==0 ;
79+
80+ CloseHandle (process);
81+ return isExplorer;
82+ }
83+
84+ static void RestoreExplorerShellIfNeeded ( bool wasRunning )
85+ {
86+ if (!wasRunning)
87+ return ;
88+
89+ // Restart Manager normally brings Explorer back before msiexec exits.
90+ // Give it a short grace period before applying our fallback so we don't
91+ // race a normal restart or create an extra Explorer process.
92+ for (int i=0 ;i<50 ;i++)
93+ {
94+ if (IsExplorerShellRunning ())
95+ return ;
96+ Sleep (100 );
97+ }
98+
99+ wchar_t explorerPath[_MAX_PATH];
100+ UINT len=GetWindowsDirectory (explorerPath,_countof (explorerPath));
101+ if (!len || len>=_countof (explorerPath) || !PathAppend (explorerPath,L" explorer.exe" ))
102+ return ;
103+
104+ STARTUPINFO startupInfo={sizeof (startupInfo)};
105+ PROCESS_INFORMATION processInfo={};
106+ if (CreateProcess (explorerPath,NULL ,NULL ,NULL ,FALSE ,0 ,NULL ,NULL ,&startupInfo,&processInfo))
107+ {
108+ CloseHandle (processInfo.hThread );
109+ CloseHandle (processInfo.hProcess );
110+ }
111+ }
112+
50113static void WriteFileXOR ( HANDLE hFile, const unsigned char *buf, int size )
51114{
52115 if (size>0 )
@@ -409,6 +472,11 @@ int APIENTRY wWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCm
409472 if (extractType == ARM64 )
410473 Wow64DisableWow64FsRedirection (&wow64FsRedirVal);
411474
475+ // Remember whether this interactive session had the Explorer desktop shell.
476+ // If Restart Manager closes it during MSI processing but fails to bring it
477+ // back, restore only the shell that was present before setup started.
478+ const bool explorerWasRunning=IsExplorerShellRunning ();
479+
412480 // start the installer
413481 STARTUPINFO startupInfo={sizeof (startupInfo)};
414482 PROCESS_INFORMATION processInfo;
@@ -438,6 +506,12 @@ int APIENTRY wWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCm
438506 GetExitCodeProcess (processInfo.hProcess ,&code);
439507 CloseHandle (processInfo.hProcess );
440508 DeleteFile (msiName);
509+
510+ // Only repair the shell after a successful transaction. Reboot-required
511+ // outcomes deliberately keep Windows Installer in control of recovery.
512+ if (code==ERROR_SUCCESS )
513+ RestoreExplorerShellIfNeeded (explorerWasRunning);
514+
441515 return code;
442516 }
443517}
0 commit comments