Skip to content

Commit 8297e6b

Browse files
authored
Zero Detours if Detours isn't needed (#5784)
If Detours isn't needed (no functions hooked) we'd still call DetourRestoreAfterWith(), DetourTransactionBegin() and DetourTransactionCommit(). Omit these unnecessary calls if Detours isn't needed. Also removed some old dead (unused) code not used (excluded via #if)
1 parent fd347a9 commit 8297e6b

File tree

5 files changed

+37
-19
lines changed

5 files changed

+37
-19
lines changed

dev/DynamicDependency/API/MddDetourPackageGraph.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,22 @@ HRESULT WINAPI DynamicGetPackageGraphRevisionId();
110110

111111
typedef UINT32 (WINAPI* GetPackageGraphRevisionIdFunction)();
112112

113+
static bool MddUseOSImplementation()
114+
{
115+
// Use the Win11 APIs if available (instead of Detour'ing to our own implementation)
116+
return MddCore::Win11::IsSupported();
117+
}
118+
119+
bool MddNeedsDetours() noexcept
120+
{
121+
// Detour to our own implementation if Windows doesn't provide a sufficient implementation
122+
return !MddUseOSImplementation();
123+
}
124+
113125
HRESULT WINAPI MddDetourPackageGraphInitialize() noexcept
114126
{
115127
// Use the Win11 APIs if available (instead of Detour'ing to our own implementation)
116-
if (MddCore::Win11::IsSupported())
128+
if (MddUseOSImplementation())
117129
{
118130
RETURN_IF_FAILED(MddWin11Initialize());
119131
return S_OK;

dev/DynamicDependency/API/MddDetourPackageGraph.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
#include "appmodel_msixdynamicdependency.h"
88

9+
bool MddNeedsDetours() noexcept;
10+
911
HRESULT WINAPI MddDetourPackageGraphInitialize() noexcept;
1012

1113
void WINAPI MddDetourPackageGraphShutdown() noexcept;

dev/UndockedRegFreeWinRT/urfw.cpp

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -405,29 +405,25 @@ HRESULT ExtRoLoadCatalog()
405405
return S_OK;
406406
}
407407

408-
HRESULT UrfwInitialize() noexcept
408+
static bool UrfwUseOSImplementation() noexcept
409409
{
410-
#if defined(TODO_URFW_DELEGATE_TO_OS_19H1PLUS)
411-
// Windows' Reg-Free WinRT first appeared in Windows 10 Version 1903, May 2019 Update (aka 19H1)
412-
// https://blogs.windows.com/windowsdeveloper/2019/04/30/enhancing-non-packaged-desktop-apps-using-windows-runtime-components/
413-
// Delegate to the OS' implementation when available
414-
if (WindowsVersion::IsWindows10_19H1OrGreater())
415-
{
416-
return S_OK;
417-
}
418-
#elif defined(TODO_SEEME_PRODUCT_TARGET)
419-
// Delegate to the OS' implementation on >= Windows 11 24H1
420-
if (WindowsVersion::IsWindows11_22H2OrGreater())
421-
{
422-
return S_OK;
423-
}
424-
#else
425410
// Delegate to the OS' implementation on >= Windows 11 24H1
426-
if (WindowsVersion::IsWindows11_24H1OrGreater())
411+
return WindowsVersion::IsWindows11_24H1OrGreater();
412+
}
413+
414+
bool UrfwNeedsDetours() noexcept
415+
{
416+
// Detour to our own implementation if Windows doesn't provide a sufficient implementation
417+
return !UrfwUseOSImplementation();
418+
}
419+
420+
HRESULT UrfwInitialize() noexcept
421+
{
422+
// Delegate to the OS' implementation if we can
423+
if (UrfwUseOSImplementation())
427424
{
428425
return S_OK;
429426
}
430-
#endif
431427

432428
// OS Reg-Free WinRT isn't available so let's do it ourselves...
433429
DetourAttach(&(PVOID&)TrueRoActivateInstance, RoActivateInstanceDetour);

dev/UndockedRegFreeWinRT/urfw.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#if !defined(URFW_H)
55
#define URFW_H
66

7+
bool UrfwNeedsDetours() noexcept;
8+
79
HRESULT UrfwInitialize() noexcept;
810

911
void UrfwShutdown() noexcept;

dev/WindowsAppRuntime_DLL/dllmain.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ static HRESULT DetoursInitialize()
2222
}
2323

2424
// Do we need to detour APIs?
25+
if (!MddNeedsDetours() && !UrfwNeedsDetours())
26+
{
27+
return S_OK;
28+
}
29+
30+
// No detours needed in a 'detours helper process'
2531
if (DetourIsHelperProcess())
2632
{
2733
return S_OK;

0 commit comments

Comments
 (0)