From d6cdb5d8edc49cae9f35cd1027cfc13083cc8f17 Mon Sep 17 00:00:00 2001 From: Amrsatrio Date: Tue, 14 Nov 2023 01:51:36 +0700 Subject: [PATCH] All: Added option to enable UI sounds on `explorer.exe`'s XAML views Please treat this feature as a novelty or proof of concept, quirks related to this will never be mitigated. --- ExplorerPatcher-L10N | 2 +- ExplorerPatcher/dllmain.c | 81 +++++++++++++++++++++++++- ep_gui/resources/EPSettingsResources.h | 1 + ep_gui/resources/lang/ep_gui.en-US.rc | 1 + ep_gui/resources/settings.reg | 3 + ep_gui/resources/settings10.reg | 3 + 6 files changed, 87 insertions(+), 4 deletions(-) diff --git a/ExplorerPatcher-L10N b/ExplorerPatcher-L10N index b358f5caf..6012fe38c 160000 --- a/ExplorerPatcher-L10N +++ b/ExplorerPatcher-L10N @@ -1 +1 @@ -Subproject commit b358f5caf8bae8314d2a5c1611eae31c48a07c39 +Subproject commit 6012fe38c9220ebfc685e3a2f65ea6d8262717a5 diff --git a/ExplorerPatcher/dllmain.c b/ExplorerPatcher/dllmain.c index d252faad7..7ce4dc7ca 100644 --- a/ExplorerPatcher/dllmain.c +++ b/ExplorerPatcher/dllmain.c @@ -1917,6 +1917,43 @@ DWORD FixTaskbarAutohide(DWORD unused) #pragma endregion +#pragma region "Allow enabling XAML sounds" +void ForceEnableXamlSounds(HMODULE hWindowsUIXaml) +{ + MODULEINFO mi; + if (!hWindowsUIXaml || !GetModuleInformation(GetCurrentProcess(), hWindowsUIXaml, &mi, sizeof(MODULEINFO))) + return; + + // Patch DirectUI::ElementSoundPlayerService::ShouldPlaySound() to disregard XboxUtility::IsOnXbox() check + // 74 ?? 39 59 ?? 75 ?? E8 ?? ?? ?? ?? 84 C0 75 + // ^^ change jnz to jmp + PBYTE match = FindPattern( + mi.lpBaseOfDll, + mi.SizeOfImage, + "\x74\x00\x39\x59\x00\x75\x00\xE8\x00\x00\x00\x00\x84\xC0\x75", + "x?xx?x?x????xxx" + ); + if (match) + { + PBYTE jnz = match + 14; + DWORD flOldProtect = 0; + if (VirtualProtect(jnz, 1, PAGE_EXECUTE_READWRITE, &flOldProtect)) + { + *jnz = 0xEB; + VirtualProtect(jnz, 1, flOldProtect, &flOldProtect); + } + } +} + +BOOL IsXamlSoundsEnabled() +{ + DWORD dwRes = 0, dwSize = sizeof(DWORD); + RegGetValueW(HKEY_CURRENT_USER, TEXT(REGPATH_OLD), L"XamlSounds", RRF_RT_DWORD, NULL, &dwRes, &dwSize); + return dwRes != 0; +} +#pragma endregion + + #pragma region "EnsureXAML on OS builds 22621+" #ifdef _WIN64 DEFINE_GUID(uuidof_Windows_Internal_Shell_XamlExplorerHost_IXamlApplicationStatics, @@ -2032,8 +2069,12 @@ HRESULT WINAPI ICoreWindow5_get_DispatcherQueueHook(void* _this, void** ppValue) HMODULE __fastcall Windows11v22H2_combase_LoadLibraryExW(LPCWSTR lpLibFileName, HANDLE hFile, DWORD dwFlags) { HMODULE hModule = LoadLibraryExW(lpLibFileName, hFile, dwFlags); - if (hModule && hModule == GetModuleHandleW(L"Windows.Ui.Xaml.dll")) + if (hModule && hModule == GetModuleHandleW(L"Windows.UI.Xaml.dll")) { + if (IsXamlSoundsEnabled()) + { + ForceEnableXamlSounds(hModule); + } DWORD flOldProtect = 0; IActivationFactory* pWindowsXamlManagerFactory = NULL; HSTRING_HEADER hstringHeaderWindowsXamlManager; @@ -2071,6 +2112,16 @@ HMODULE __fastcall Windows11v22H2_combase_LoadLibraryExW(LPCWSTR lpLibFileName, } return hModule; } + +HMODULE __fastcall combase_LoadLibraryExW(LPCWSTR lpLibFileName, HANDLE hFile, DWORD dwFlags) +{ + HMODULE hModule = LoadLibraryExW(lpLibFileName, hFile, dwFlags); + if (hModule && hModule == GetModuleHandleW(L"Windows.UI.Xaml.dll")) + { + ForceEnableXamlSounds(hModule); + } + return hModule; +} #endif #pragma endregion @@ -9898,6 +9949,12 @@ DWORD InjectBasicFunctions(BOOL bIsExplorer, BOOL bInstall) { printf("Failed to hook RtlQueryFeatureConfiguration(). rv = %d\n", rv); } + + if (!bIsExplorer && IsXamlSoundsEnabled()) + { + HANDLE hCombase = LoadLibraryW(L"combase.dll"); + VnPatchIAT(hCombase, "api-ms-win-core-libraryloader-l1-2-0.dll", "LoadLibraryExW", combase_LoadLibraryExW); + } } #endif @@ -12005,9 +12062,9 @@ DWORD Inject(BOOL bIsExplorer) printf("Setup twinui.pcshell functions done\n"); + HANDLE hCombase = LoadLibraryW(L"combase.dll"); if (IsWindows11()) { - HANDLE hCombase = LoadLibraryW(L"combase.dll"); /*if (bOldTaskbar) // TODO Pulled back for now, crashes on 22621.2428 { // Hook RoGetActivationFactory() for old taskbar @@ -12030,8 +12087,12 @@ DWORD Inject(BOOL bIsExplorer) // Fixed a bug that crashed Explorer when a folder window was opened after a first one was closed on OS builds 22621+ VnPatchIAT(hCombase, "api-ms-win-core-libraryloader-l1-2-0.dll", "LoadLibraryExW", Windows11v22H2_combase_LoadLibraryExW); } - printf("Setup combase functions done\n"); } + if (!IsWindows11Version22H2OrHigher() && IsXamlSoundsEnabled()) + { + VnPatchIAT(hCombase, "api-ms-win-core-libraryloader-l1-2-0.dll", "LoadLibraryExW", combase_LoadLibraryExW); + } + printf("Setup combase functions done\n"); HANDLE hTwinui = LoadLibraryW(L"twinui.dll"); @@ -14030,6 +14091,13 @@ HRESULT EntryPoint(DWORD dwMethod) else if (bIsThisStartMEH) { InjectStartMenu(); +#ifdef _WIN64 + if (IsXamlSoundsEnabled()) + { + HMODULE hWindowsUIXaml = LoadLibraryW(L"Windows.UI.Xaml.dll"); + ForceEnableXamlSounds(hWindowsUIXaml); + } +#endif IncrementDLLReferenceCount(hModule); bInstanced = TRUE; } @@ -14043,6 +14111,13 @@ HRESULT EntryPoint(DWORD dwMethod) { InjectShellExperienceHost(); } +#ifdef _WIN64 + if (IsXamlSoundsEnabled()) + { + HMODULE hWindowsUIXaml = LoadLibraryW(L"Windows.UI.Xaml.dll"); + ForceEnableXamlSounds(hWindowsUIXaml); + } +#endif IncrementDLLReferenceCount(hModule); bInstanced = TRUE; } diff --git a/ep_gui/resources/EPSettingsResources.h b/ep_gui/resources/EPSettingsResources.h index 9f57c5e8a..ae8146c9f 100644 --- a/ep_gui/resources/EPSettingsResources.h +++ b/ep_gui/resources/EPSettingsResources.h @@ -369,6 +369,7 @@ #define IDS_ADV_DELAY_6000 1924 #define IDS_ADV_DELAY_8000 1925 #define IDS_ADV_DELAY_10000 1926 +#define IDS_ADV_XAMLSOUNDS 1927 #define IDS_ABOUT 2001 #define IDS_ABOUT_VERSION 2002 diff --git a/ep_gui/resources/lang/ep_gui.en-US.rc b/ep_gui/resources/lang/ep_gui.en-US.rc index d115e80a0..1ecf0a12f 100644 --- a/ep_gui/resources/lang/ep_gui.en-US.rc +++ b/ep_gui/resources/lang/ep_gui.en-US.rc @@ -412,6 +412,7 @@ BEGIN IDS_ADV_DELAY_6000 "6 seconds" IDS_ADV_DELAY_8000 "8 seconds" IDS_ADV_DELAY_10000 "10 seconds" + IDS_ADV_XAMLSOUNDS "Enable UI sounds in Explorer's XAML views" IDS_ABOUT "About" diff --git a/ep_gui/resources/settings.reg b/ep_gui/resources/settings.reg index 93e154b55..38bc663ce 100644 --- a/ep_gui/resources/settings.reg +++ b/ep_gui/resources/settings.reg @@ -744,6 +744,9 @@ ;x 8000 %R:1925% ;x 10000 %R:1926% "ExplorerReadyDelay"=dword:00000000 +[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\ExplorerPatcher] +;b %R:1927% * +"XamlSounds"=dword:00000000 diff --git a/ep_gui/resources/settings10.reg b/ep_gui/resources/settings10.reg index f302d875d..613a8bc2b 100644 --- a/ep_gui/resources/settings10.reg +++ b/ep_gui/resources/settings10.reg @@ -568,6 +568,9 @@ "PinnedItemsActAsQuickLaunch"=dword:00000000 ;b %R:1913% * "RemoveExtraGapAroundPinnedItems"=dword:00000000 +[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\ExplorerPatcher] +;b %R:1927% * +"XamlSounds"=dword:00000000 ;T %R:2001%