@@ -841,31 +841,35 @@ static DWORD WINAPI ConnectAttemptThread( LPVOID param )
841841 return 0 ;
842842}
843843
844- static DWORD WINAPI ConnectThread ( LPVOID )
844+ static DWORD FinishConnectThread ( HMODULE moduleReference, HMODULE runtime, bool resetConnectionState )
845845{
846+ if (runtime)
847+ FreeLibrary (runtime);
848+ if (resetConnectionState)
849+ InterlockedExchange (&g_ConnectStarted, 0 );
850+
851+ // The connection worker executes from StartMenuDLL and can outlive the normal
852+ // Open-Shell unload path. Release its private module reference and terminate
853+ // atomically so the DLL cannot disappear underneath the thread's return path.
854+ FreeLibraryAndExitThread (moduleReference, 0 );
855+ return 0 ;
856+ }
857+
858+ static DWORD WINAPI ConnectThread ( LPVOID param )
859+ {
860+ HMODULE moduleReference = (HMODULE )param;
846861 HMODULE runtime = LoadLibraryEx (L" Windows.UI.Xaml.dll" , NULL , LOAD_LIBRARY_SEARCH_SYSTEM32 );
847862 if (!runtime)
848- {
849- InterlockedExchange (&g_ConnectStarted, 0 );
850- return 0 ;
851- }
863+ return FinishConnectThread (moduleReference, NULL , true );
852864
853865 InitXamlDiagnosticsEx_t init = (InitXamlDiagnosticsEx_t)GetProcAddress (runtime, " InitializeXamlDiagnosticsEx" );
854866 if (!init)
855- {
856- FreeLibrary (runtime);
857- InterlockedExchange (&g_ConnectStarted, 0 );
858- return 0 ;
859- }
867+ return FinishConnectThread (moduleReference, runtime, true );
860868
861869 HMODULE module = GetThisModule ();
862870 wchar_t dllPath[MAX_PATH ];
863871 if (!module || !GetModuleFileName (module , dllPath, _countof (dllPath)))
864- {
865- FreeLibrary (runtime);
866- InterlockedExchange (&g_ConnectStarted, 0 );
867- return 0 ;
868- }
872+ return FinishConnectThread (moduleReference, runtime, true );
869873
870874 const wchar_t *endpoints[] = { L" VisualDiagConnection1" , L" VisualDiagConnection2" };
871875 HRESULT last = E_FAIL ;
@@ -888,29 +892,38 @@ static DWORD WINAPI ConnectThread( LPVOID )
888892 if (SUCCEEDED (last))
889893 {
890894 LogToFile (STARTUP_LOG , L" Win11StartButton: connected using %s" , endpoints[i]);
891- FreeLibrary (runtime);
892- return 0 ;
895+ return FinishConnectThread (moduleReference, runtime, false );
893896 }
894897 }
895898 Sleep (500 );
896899 }
897900
898901 LogToFile (STARTUP_LOG , L" Win11StartButton: connection failed 0x%08X" , last);
899- FreeLibrary (runtime);
900- InterlockedExchange (&g_ConnectStarted, 0 );
901- return 0 ;
902+ return FinishConnectThread (moduleReference, runtime, true );
902903}
903904
904905static void EnsureConnection ( void )
905906{
906907 if (InterlockedCompareExchange (&g_ConnectStarted, 1 , 0 ) != 0 )
907908 return ;
908909
909- HANDLE thread = CreateThread (NULL , 0 , ConnectThread, NULL , 0 , NULL );
910+ HMODULE moduleReference = NULL ;
911+ if (!GetModuleHandleEx (GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS , (LPCTSTR )&ConnectThread, &moduleReference))
912+ {
913+ InterlockedExchange (&g_ConnectStarted, 0 );
914+ return ;
915+ }
916+
917+ HANDLE thread = CreateThread (NULL , 0 , ConnectThread, moduleReference, 0 , NULL );
910918 if (thread)
919+ {
911920 CloseHandle (thread);
921+ }
912922 else
923+ {
924+ FreeLibrary (moduleReference);
913925 InterlockedExchange (&g_ConnectStarted, 0 );
926+ }
914927}
915928
916929void StartWin11StartButtonMonitor ( void )
0 commit comments