From 4ece80c8cb53291c9bc6760bce5a8ccab16f036a Mon Sep 17 00:00:00 2001 From: Amrsatrio Date: Wed, 27 Sep 2023 14:21:51 +0700 Subject: [PATCH] All: Try to find `twinui.pcshell.dll` offsets from disk instead of from memory for compatibility with other tweak software --- ExplorerPatcher/dllmain.c | 351 ++++++++++++++++++++------------------ 1 file changed, 184 insertions(+), 167 deletions(-) diff --git a/ExplorerPatcher/dllmain.c b/ExplorerPatcher/dllmain.c index 52fa2b385..5d9560214 100644 --- a/ExplorerPatcher/dllmain.c +++ b/ExplorerPatcher/dllmain.c @@ -9928,17 +9928,17 @@ INT64 twinui_pcshell_IsUndockedAssetAvailableHook(INT a1, INT64 a2, INT64 a3, co } } -INT64(*twinui_pcshell_CMultitaskingViewManager__CreateDCompMTVHostFunc)(INT64 this, unsigned int a2, INT64 a3, INT64 a4, INT64* a5); -INT64(*twinui_pcshell_CMultitaskingViewManager__CreateXamlMTVHostFunc)(INT64 this, unsigned int a2, INT64 a3, INT64 a4, INT64* a5); -INT64 twinui_pcshell_CMultitaskingViewManager__CreateXamlMTVHostHook(INT64 this, unsigned int a2, INT64 a3, INT64 a4, INT64* a5) +INT64(*twinui_pcshell_CMultitaskingViewManager__CreateDCompMTVHostFunc)(INT64 _this, unsigned int a2, INT64 a3, INT64 a4, INT64* a5); +INT64(*twinui_pcshell_CMultitaskingViewManager__CreateXamlMTVHostFunc)(INT64 _this, unsigned int a2, INT64 a3, INT64 a4, INT64* a5); +INT64 twinui_pcshell_CMultitaskingViewManager__CreateXamlMTVHostHook(INT64 _this, unsigned int a2, INT64 a3, INT64 a4, INT64* a5) { if (!twinui_pcshell_IsUndockedAssetAvailableHook(a2, 0, 0, NULL)) - return twinui_pcshell_CMultitaskingViewManager__CreateDCompMTVHostFunc(this, a2, a3, a4, a5); - return twinui_pcshell_CMultitaskingViewManager__CreateXamlMTVHostFunc(this, a2, a3, a4, a5); + return twinui_pcshell_CMultitaskingViewManager__CreateDCompMTVHostFunc(_this, a2, a3, a4, a5); + return twinui_pcshell_CMultitaskingViewManager__CreateXamlMTVHostFunc(_this, a2, a3, a4, a5); } -HRESULT(*twinui_pcshell_PenMenuSystemTrayManager__GetDynamicSystemTrayHeightForMonitorFunc)(IInspectable* this, HMONITOR hMonitor, float* outHeight); -HRESULT twinui_pcshell_PenMenuSystemTrayManager__GetDynamicSystemTrayHeightForMonitorHook(IInspectable* this, HMONITOR hMonitor, float* outHeight) +HRESULT(*twinui_pcshell_PenMenuSystemTrayManager__GetDynamicSystemTrayHeightForMonitorFunc)(IInspectable* _this, HMONITOR hMonitor, float* outHeight); +HRESULT twinui_pcshell_PenMenuSystemTrayManager__GetDynamicSystemTrayHeightForMonitorHook(IInspectable* _this, HMONITOR hMonitor, float* outHeight) { if (bOldTaskbar) { @@ -9948,7 +9948,7 @@ HRESULT twinui_pcshell_PenMenuSystemTrayManager__GetDynamicSystemTrayHeightForMo *outHeight = (float)(mi.rcMonitor.bottom - mi.rcWork.bottom); return S_OK; } - return twinui_pcshell_PenMenuSystemTrayManager__GetDynamicSystemTrayHeightForMonitorFunc(this, hMonitor, outHeight); + return twinui_pcshell_PenMenuSystemTrayManager__GetDynamicSystemTrayHeightForMonitorFunc(_this, hMonitor, outHeight); } #ifdef _WIN64 @@ -10462,6 +10462,179 @@ BOOL explorer_IsOS(DWORD dwOS) return IsOS(dwOS); } +void TryToFindTwinuiPCShellOffsets(DWORD* pOffsets) +{ + // We read from the file instead of from memory because other tweak software might've modified the functions we're looking for + HANDLE hFile = CreateFileW(L"twinui.pcshell.dll", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + if (hFile == INVALID_HANDLE_VALUE) + { + printf("Failed to open twinui.pcshell.dll\n"); + return; + } + + DWORD dwSize = GetFileSize(hFile, NULL); + PBYTE pFile = malloc(dwSize); + DWORD dwRead = 0; + if (!ReadFile(hFile, pFile, dwSize, &dwRead, NULL) || dwRead != dwSize) + { + printf("Failed to read twinui.pcshell.dll\n"); + goto cleanup; + } + + if (IsWindows11Version22H2OrHigher()) + { + // All patterns here have been tested to work on: + // - 22621.1, 22621.1992, 22621.2134, 22621.2283, 22621.2359 (RP) + // - 23545.1000 + + if (!pOffsets[0] || pOffsets[0] == 0xFFFFFFFF) + { + // Ref: CMultitaskingViewFrame::v_WndProc() + // 4D 8B CF 4D 8B C4 8B D6 48 8B 49 08 E8 ? ? ? ? E9 + // ^^^^^^^ + PBYTE match = FindPattern( + pFile, dwSize, + "\x4D\x8B\xCF\x4D\x8B\xC4\x8B\xD6\x48\x8B\x49\x08\xE8\x00\x00\x00\x00\xE9", + "xxxxxxxxxxxxx????x" + ); + if (match) + { + match += 12; + pOffsets[0] = match + 5 + *(int*)(match + 1) - pFile; + printf("CImmersiveContextMenuOwnerDrawHelper::s_ContextMenuWndProc() = %lX\n", pOffsets[0]); + } + } + if (!pOffsets[1] || pOffsets[1] == 0xFFFFFFFF) + { + // 48 89 5C 24 ? 48 89 74 24 ? 57 48 83 EC 30 49 8B D8 48 8B FA 48 8B F1 49 83 20 00 41 B0 03 B2 01 + PBYTE match = FindPattern( + pFile, dwSize, + "\x48\x89\x5C\x24\x00\x48\x89\x74\x24\x00\x57\x48\x83\xEC\x30\x49\x8B\xD8\x48\x8B\xFA\x48\x8B\xF1\x49\x83\x20\x00\x41\xB0\x03\xB2\x01", + "xxxx?xxxx?xxxxxxxxxxxxxxxxxxxxxxx" + ); + if (match) + { + pOffsets[1] = match - pFile; + printf("CLauncherTipContextMenu::GetMenuItemsAsync() = %lX\n", pOffsets[1]); + } + } + if (!pOffsets[2] || pOffsets[2] == 0xFFFFFFFF) + { + // Ref: SwitchItemThumbnailElement::ShowContextMenu() + // E8 ? ? ? ? E8 ? ? ? ? 0F B7 C8 E8 ? ? ? ? F7 D8 + // ^^^^^^^ + PBYTE match = FindPattern( + pFile, dwSize, + "\xE8\x00\x00\x00\x00\xE8\x00\x00\x00\x00\x0F\xB7\xC8\xE8\x00\x00\x00\x00\xF7\xD8", + "x????x????xxxx????xx" + ); + if (match) + { + pOffsets[2] = match + 5 + *(int*)(match + 1) - pFile; + printf("ImmersiveContextMenuHelper::ApplyOwnerDrawToMenu() = %lX\n", pOffsets[2]); + } + } + if (!pOffsets[3] || pOffsets[3] == 0xFFFFFFFF) + { + // Ref: SwitchItemThumbnailElement::ShowContextMenu() + // E8 ? ? ? ? 85 DB 74 29 + // ^^^^^^^ + PBYTE match = FindPattern( + pFile, dwSize, + "\xE8\x00\x00\x00\x00\x85\xDB\x74\x29", + "x????xxxx" + ); + if (match) + { + pOffsets[3] = match + 5 + *(int*)(match + 1) - pFile; + printf("ImmersiveContextMenuHelper::RemoveOwnerDrawFromMenu() = %lX\n", pOffsets[3]); + } + } + if (!pOffsets[4] || pOffsets[4] == 0xFFFFFFFF) + { + // E8 ? ? ? ? 90 49 8D 56 38 49 8B CE + // ^^^^^^^ + PBYTE match = FindPattern( + pFile, dwSize, + "\xE8\x00\x00\x00\x00\x90\x49\x8D\x56\x38\x49\x8B\xCE", + "x????xxxxxxxx" + ); + if (match) + { + pOffsets[4] = match + 5 + *(int*)(match + 1) - pFile; + printf("CLauncherTipContextMenu::ExecuteShutdownCommand() = %lX\n", pOffsets[4]); + } + } + if (!pOffsets[5] || pOffsets[5] == 0xFFFFFFFF) + { + // E8 ? ? ? ? 90 48 8D 56 38 48 8B CE + // ^^^^^^^ + PBYTE match = FindPattern( + pFile, dwSize, + "\xE8\x00\x00\x00\x00\x90\x48\x8D\x56\x38\x48\x8B\xCE", + "x????xxxxxxxx" + ); + if (match) + { + pOffsets[5] = match + 5 + *(int*)(match + 1) - pFile; + printf("CLauncherTipContextMenu::ExecuteCommand() = %lX\n", pOffsets[5]); + } + } + if (!pOffsets[6] || pOffsets[6] == 0xFFFFFFFF) + { + // 48 83 EC 28 41 B0 03 B2 01 + PBYTE match = FindPattern( + pFile, dwSize, + "\x48\x83\xEC\x28\x41\xB0\x03\xB2\x01", + "xxxxxxxxx" + ); + if (match) + { + pOffsets[6] = match - pFile; + printf("CLauncherTipContextMenu::ShowLauncherTipContextMenu() = %lX\n", pOffsets[6]); + } + } + if (!pOffsets[7] || pOffsets[7] == 0xFFFFFFFF) + { + // Ref: CMultitaskingViewManager::_CreateMTVHost() + // 4C 89 74 24 ? ? 8B ? ? 8B ? 8B D7 48 8B CE E8 ? ? ? ? 8B + // ^^^^^^^ + PBYTE match = FindPattern( + pFile, dwSize, + "\x4C\x89\x74\x24\x00\x00\x8B\x00\x00\x8B\x00\x8B\xD7\x48\x8B\xCE\xE8\x00\x00\x00\x00\x8B", + "xxxx??x??x?xxxxxx????x" + ); + if (match) + { + match += 16; + pOffsets[7] = match + 5 + *(int*)(match + 1) - pFile; + printf("CMultitaskingViewManager::CreateXamlMTVHost() = %lX\n", pOffsets[7]); + } + } + if (!pOffsets[8] || pOffsets[8] == 0xFFFFFFFF) + { + // Ref: CMultitaskingViewManager::_CreateMTVHost() + // 4C 89 74 24 ? ? 8B ? ? 8B ? 8B D7 48 8B CE E8 ? ? ? ? 90 + // ^^^^^^^ + PBYTE match = FindPattern( + pFile, dwSize, + "\x4C\x89\x74\x24\x00\x00\x8B\x00\x00\x8B\x00\x8B\xD7\x48\x8B\xCE\xE8\x00\x00\x00\x00\x90", + "xxxx??x??x?xxxxxx????x" + ); + if (match) + { + match += 16; + pOffsets[8] = match + 5 + *(int*)(match + 1) - pFile; + printf("CMultitaskingViewManager::CreateDCompMTVHost() = %lX\n", pOffsets[8]); + } + } + } + +cleanup: + free(pFile); + CloseHandle(hFile); +} + DWORD Inject(BOOL bIsExplorer) { #if defined(DEBUG) | defined(_DEBUG) @@ -10937,164 +11110,8 @@ DWORD Inject(BOOL bIsExplorer) MODULEINFO miTwinuiPcshell; GetModuleInformation(GetCurrentProcess(), hTwinuiPcshell, &miTwinuiPcshell, sizeof(MODULEINFO)); - if (IsWindows11Version22H2OrHigher()) - { - // All patterns here have been tested to work on: - // - 22621.1, 22621.1992, 22621.2134, 22621.2283, 22621.2359 (RP) - // - 23545.1000 - - // ZeroMemory(symbols_PTRS.twinui_pcshell_PTRS, sizeof(symbols_PTRS.twinui_pcshell_PTRS)); - if (!symbols_PTRS.twinui_pcshell_PTRS[0] || symbols_PTRS.twinui_pcshell_PTRS[0] == 0xFFFFFFFF) - { - // Ref: CMultitaskingViewFrame::v_WndProc() - // 4D 8B CF 4D 8B C4 8B D6 48 8B 49 08 E8 ? ? ? ? E9 - // ^^^^^^^ - PBYTE match = FindPattern( - hTwinuiPcshell, - miTwinuiPcshell.SizeOfImage, - "\x4D\x8B\xCF\x4D\x8B\xC4\x8B\xD6\x48\x8B\x49\x08\xE8\x00\x00\x00\x00\xE9", - "xxxxxxxxxxxxx????x" - ); - if (match) - { - match += 12; - symbols_PTRS.twinui_pcshell_PTRS[0] = match + 5 + *(int*)(match + 1) - hTwinuiPcshell; - printf("symbols_PTRS.twinui_pcshell_PTRS[0] = %llX\n", symbols_PTRS.twinui_pcshell_PTRS[0]); - } - } - if (!symbols_PTRS.twinui_pcshell_PTRS[1] || symbols_PTRS.twinui_pcshell_PTRS[1] == 0xFFFFFFFF) - { - // 48 89 5C 24 ? 48 89 74 24 ? 57 48 83 EC 30 49 8B D8 48 8B FA 48 8B F1 49 83 20 00 41 B0 03 B2 01 - PBYTE match = FindPattern( - hTwinuiPcshell, - miTwinuiPcshell.SizeOfImage, - "\x48\x89\x5C\x24\x00\x48\x89\x74\x24\x00\x57\x48\x83\xEC\x30\x49\x8B\xD8\x48\x8B\xFA\x48\x8B\xF1\x49\x83\x20\x00\x41\xB0\x03\xB2\x01", - "xxxx?xxxx?xxxxxxxxxxxxxxxxxxxxxxx" - ); - if (match) - { - symbols_PTRS.twinui_pcshell_PTRS[1] = match - hTwinuiPcshell; - printf("symbols_PTRS.twinui_pcshell_PTRS[1] = %llX\n", symbols_PTRS.twinui_pcshell_PTRS[1]); - } - } - if (!symbols_PTRS.twinui_pcshell_PTRS[2] || symbols_PTRS.twinui_pcshell_PTRS[2] == 0xFFFFFFFF) - { - // Ref: SwitchItemThumbnailElement::ShowContextMenu() - // E8 ? ? ? ? E8 ? ? ? ? 0F B7 C8 E8 ? ? ? ? F7 D8 - // ^^^^^^^ - PBYTE match = FindPattern( - hTwinuiPcshell, - miTwinuiPcshell.SizeOfImage, - "\xE8\x00\x00\x00\x00\xE8\x00\x00\x00\x00\x0F\xB7\xC8\xE8\x00\x00\x00\x00\xF7\xD8", - "x????x????xxxx????xx" - ); - if (match) - { - symbols_PTRS.twinui_pcshell_PTRS[2] = match + 5 + *(int*)(match + 1) - hTwinuiPcshell; - printf("symbols_PTRS.twinui_pcshell_PTRS[2] = %llX\n", symbols_PTRS.twinui_pcshell_PTRS[2]); - } - } - if (!symbols_PTRS.twinui_pcshell_PTRS[3] || symbols_PTRS.twinui_pcshell_PTRS[3] == 0xFFFFFFFF) - { - // Ref: SwitchItemThumbnailElement::ShowContextMenu() - // E8 ? ? ? ? 85 DB 74 29 - // ^^^^^^^ - PBYTE match = FindPattern( - hTwinuiPcshell, - miTwinuiPcshell.SizeOfImage, - "\xE8\x00\x00\x00\x00\x85\xDB\x74\x29", - "x????xxxx" - ); - if (match) - { - symbols_PTRS.twinui_pcshell_PTRS[3] = match + 5 + *(int*)(match + 1) - hTwinuiPcshell; - printf("symbols_PTRS.twinui_pcshell_PTRS[3] = %llX\n", symbols_PTRS.twinui_pcshell_PTRS[3]); - } - } - if (!symbols_PTRS.twinui_pcshell_PTRS[4] || symbols_PTRS.twinui_pcshell_PTRS[4] == 0xFFFFFFFF) - { - // E8 ? ? ? ? 90 49 8D 56 38 49 8B CE - // ^^^^^^^ - PBYTE match = FindPattern( - hTwinuiPcshell, - miTwinuiPcshell.SizeOfImage, - "\xE8\x00\x00\x00\x00\x90\x49\x8D\x56\x38\x49\x8B\xCE", - "x????xxxxxxxx" - ); - if (match) - { - symbols_PTRS.twinui_pcshell_PTRS[4] = match + 5 + *(int*)(match + 1) - hTwinuiPcshell; - printf("symbols_PTRS.twinui_pcshell_PTRS[4] = %llX\n", symbols_PTRS.twinui_pcshell_PTRS[4]); - } - } - if (!symbols_PTRS.twinui_pcshell_PTRS[5] || symbols_PTRS.twinui_pcshell_PTRS[5] == 0xFFFFFFFF) - { - // E8 ? ? ? ? 90 48 8D 56 38 48 8B CE - // ^^^^^^^ - PBYTE match = FindPattern( - hTwinuiPcshell, - miTwinuiPcshell.SizeOfImage, - "\xE8\x00\x00\x00\x00\x90\x48\x8D\x56\x38\x48\x8B\xCE", - "x????xxxxxxxx" - ); - if (match) - { - symbols_PTRS.twinui_pcshell_PTRS[5] = match + 5 + *(int*)(match + 1) - hTwinuiPcshell; - printf("symbols_PTRS.twinui_pcshell_PTRS[5] = %llX\n", symbols_PTRS.twinui_pcshell_PTRS[5]); - } - } - if (!symbols_PTRS.twinui_pcshell_PTRS[6] || symbols_PTRS.twinui_pcshell_PTRS[6] == 0xFFFFFFFF) - { - // 48 83 EC 28 41 B0 03 B2 01 - PBYTE match = FindPattern( - hTwinuiPcshell, - miTwinuiPcshell.SizeOfImage, - "\x48\x83\xEC\x28\x41\xB0\x03\xB2\x01", - "xxxxxxxxx" - ); - if (match) - { - symbols_PTRS.twinui_pcshell_PTRS[6] = match - hTwinuiPcshell; - printf("symbols_PTRS.twinui_pcshell_PTRS[6] = %llX\n", symbols_PTRS.twinui_pcshell_PTRS[6]); - } - } - if (!symbols_PTRS.twinui_pcshell_PTRS[7] || symbols_PTRS.twinui_pcshell_PTRS[7] == 0xFFFFFFFF) - { - // Ref: CMultitaskingViewManager::_CreateMTVHost() - // 4C 89 74 24 ? ? 8B ? ? 8B ? 8B D7 48 8B CE E8 ? ? ? ? 8B - // ^^^^^^^ - PBYTE match = FindPattern( - hTwinuiPcshell, - miTwinuiPcshell.SizeOfImage, - "\x4C\x89\x74\x24\x00\x00\x8B\x00\x00\x8B\x00\x8B\xD7\x48\x8B\xCE\xE8\x00\x00\x00\x00\x8B", - "xxxx??x??x?xxxxxx????x" - ); - if (match) - { - match += 16; - symbols_PTRS.twinui_pcshell_PTRS[7] = match + 5 + *(int*)(match + 1) - hTwinuiPcshell; - printf("symbols_PTRS.twinui_pcshell_PTRS[7] = %llX\n", symbols_PTRS.twinui_pcshell_PTRS[7]); - } - } - if (!symbols_PTRS.twinui_pcshell_PTRS[8] || symbols_PTRS.twinui_pcshell_PTRS[8] == 0xFFFFFFFF) - { - // Ref: CMultitaskingViewManager::_CreateMTVHost() - // 4C 89 74 24 ? ? 8B ? ? 8B ? 8B D7 48 8B CE E8 ? ? ? ? 90 - // ^^^^^^^ - PBYTE match = FindPattern( - hTwinuiPcshell, - miTwinuiPcshell.SizeOfImage, - "\x4C\x89\x74\x24\x00\x00\x8B\x00\x00\x8B\x00\x8B\xD7\x48\x8B\xCE\xE8\x00\x00\x00\x00\x90", - "xxxx??x??x?xxxxxx????x" - ); - if (match) - { - match += 16; - symbols_PTRS.twinui_pcshell_PTRS[8] = match + 5 + *(int*)(match + 1) - hTwinuiPcshell; - printf("symbols_PTRS.twinui_pcshell_PTRS[8] = %llX\n", symbols_PTRS.twinui_pcshell_PTRS[8]); - } - } - } + // ZeroMemory(symbols_PTRS.twinui_pcshell_PTRS, sizeof(symbols_PTRS.twinui_pcshell_PTRS)); // Uncomment for testing + TryToFindTwinuiPCShellOffsets(symbols_PTRS.twinui_pcshell_PTRS); if (symbols_PTRS.twinui_pcshell_PTRS[0] && symbols_PTRS.twinui_pcshell_PTRS[0] != 0xFFFFFFFF) { @@ -11145,7 +11162,7 @@ DWORD Inject(BOOL bIsExplorer) } if (rv != 0) { - printf("Failed to hook CLauncherTipContextMenu_ShowLauncherTipContextMenu(). rv = %d\n", rv); + printf("Failed to hook CLauncherTipContextMenu::ShowLauncherTipContextMenu(). rv = %d\n", rv); } rv = -1;