Skip to content

Commit

Permalink
添加 ARM64 Detours
Browse files Browse the repository at this point in the history
Hook 继承 IClosable
  • Loading branch information
wherewhere committed Nov 7, 2023
1 parent b202e16 commit 45f7feb
Show file tree
Hide file tree
Showing 12 changed files with 113 additions and 85 deletions.
10 changes: 1 addition & 9 deletions CoreAppUWP.WinRT/CoreAppUWP.WinRT.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@
<Configuration>Debug</Configuration>
<Platform>ARM64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|ARM64EC">
<Configuration>Debug</Configuration>
<Platform>ARM64EC</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
Expand All @@ -37,10 +33,6 @@
<Configuration>Release</Configuration>
<Platform>ARM64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|ARM64EC">
<Configuration>Release</Configuration>
<Platform>ARM64EC</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
Expand Down Expand Up @@ -100,7 +92,7 @@
<SubSystem>Console</SubSystem>
<GenerateWindowsMetadata>false</GenerateWindowsMetadata>
<ModuleDefinitionFile>CoreAppUWP.WinRT.def</ModuleDefinitionFile>
<AdditionalLibraryDirectories Condition="'$(Platform)'=='ARM64EC'">..\packages\Detours.4.0.1\lib\native\libs\x64;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalLibraryDirectories Condition="'$(PlatformTarget)'=='ARM64'">Detours\ARM64;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)'=='Debug'">
Expand Down
Binary file added CoreAppUWP.WinRT/Detours/ARM64/detours.lib
Binary file not shown.
Binary file added CoreAppUWP.WinRT/Detours/ARM64/detours.pdb
Binary file not shown.
61 changes: 34 additions & 27 deletions CoreAppUWP.WinRT/HookRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,46 @@ namespace winrt::CoreAppUWP::WinRT::implementation
{
void HookRegistry::IsHooked(bool value)
{
if (value == isHooked)
{
return;
}
if (value == isHooked) { return; }
if (value) { StartHook(); }
else { EndHook(); }
}

if (value)
{
StartHook();
}
else
void HookRegistry::StartHook()
{
if (!isHooked)
{
EndHook();
DetourTransactionBegin();
DetourUpdateThread(currentThread);
DetourAttach((PVOID*)&BaseRegOpenKeyExW, OverrideRegOpenKeyExW);
DetourAttach((PVOID*)&BaseRegCloseKey, OverrideRegCloseKey);
DetourAttach((PVOID*)&BaseRegQueryValueExW, OverrideRegQueryValueExW);
DetourTransactionCommit();
isHooked = true;
}
}

void HookRegistry::StartHook()
void HookRegistry::EndHook()
{
DetourTransactionBegin();
DetourUpdateThread(currentThread);
DetourAttach((PVOID*)&BaseRegOpenKeyExW, OverrideRegOpenKeyExW);
DetourAttach((PVOID*)&BaseRegCloseKey, OverrideRegCloseKey);
DetourAttach((PVOID*)&BaseRegQueryValueExW, OverrideRegQueryValueExW);
DetourTransactionCommit();
if (isHooked)
{
DetourTransactionBegin();
DetourUpdateThread(currentThread);
DetourDetach((PVOID*)&BaseRegOpenKeyExW, OverrideRegOpenKeyExW);
DetourDetach((PVOID*)&BaseRegCloseKey, OverrideRegCloseKey);
DetourDetach((PVOID*)&BaseRegQueryValueExW, OverrideRegQueryValueExW);
DetourTransactionCommit();
isHooked = false;
}
}

void HookRegistry::EndHook()
void HookRegistry::Close()
{
DetourTransactionBegin();
DetourUpdateThread(currentThread);
DetourDetach((PVOID*)&BaseRegOpenKeyExW, OverrideRegOpenKeyExW);
DetourDetach((PVOID*)&BaseRegCloseKey, OverrideRegCloseKey);
DetourDetach((PVOID*)&BaseRegQueryValueExW, OverrideRegQueryValueExW);
DetourTransactionCommit();
if (currentThread)
{
EndHook();
currentThread = nullptr;
}
}

LSTATUS APIENTRY HookRegistry::OverrideRegOpenKeyExW(HKEY hkey, LPCWSTR lpSubKey, DWORD ulOptions, REGSAM samDesired, PHKEY phkResult)
Expand Down Expand Up @@ -69,7 +76,7 @@ namespace winrt::CoreAppUWP::WinRT::implementation
bool isRealKey = false;

std::lock_guard<std::mutex> lock(xamlKeyMtx);
auto pos = xamlKeyMap.find(hKey);
std::map<HKEY, bool>::iterator pos = xamlKeyMap.find(hKey);

bool isXamlKey = pos != xamlKeyMap.end();
if (isXamlKey)
Expand All @@ -94,9 +101,9 @@ namespace winrt::CoreAppUWP::WinRT::implementation
{
bool isRealKey = false;
std::lock_guard<std::mutex> lock(xamlKeyMtx);
auto pos = xamlKeyMap.find(hKey);
std::map<HKEY, bool>::iterator pos = xamlKeyMap.find(hKey);

bool isXamlKey = pos != xamlKeyMap.end();
bool isXamlKey = pos != xamlKeyMap.end();
if (isXamlKey)
{
isRealKey = pos->second;
Expand Down
1 change: 1 addition & 0 deletions CoreAppUWP.WinRT/HookRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace winrt::CoreAppUWP::WinRT::implementation

void StartHook();
void EndHook();
void Close();

private:
bool isHooked = false;
Expand Down
2 changes: 1 addition & 1 deletion CoreAppUWP.WinRT/HookRegistry.idl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace CoreAppUWP.WinRT
{
[default_interface]
runtimeclass HookRegistry
runtimeclass HookRegistry : Windows.Foundation.IClosable
{
HookRegistry();

Expand Down
47 changes: 27 additions & 20 deletions CoreAppUWP.WinRT/HookWindowingModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,42 @@ namespace winrt::CoreAppUWP::WinRT::implementation
{
void HookWindowingModel::IsHooked(bool value)
{
if (value == isHooked)
{
return;
}
if (value == isHooked) { return; }
if (value) { StartHook(); }
else { EndHook(); }
}

if (value)
{
StartHook();
}
else
void HookWindowingModel::StartHook()
{
if (!isHooked)
{
EndHook();
DetourTransactionBegin();
DetourUpdateThread(currentThread);
DetourAttach((PVOID*)&BaseAppPolicyGetWindowingModel, OverrideAppPolicyGetWindowingModel);
DetourTransactionCommit();
isHooked = true;
}
}

void HookWindowingModel::StartHook()
void HookWindowingModel::EndHook()
{
DetourTransactionBegin();
DetourUpdateThread(currentThread);
DetourAttach((PVOID*)&BaseAppPolicyGetWindowingModel, OverrideAppPolicyGetWindowingModel);
DetourTransactionCommit();
if (isHooked)
{
DetourTransactionBegin();
DetourUpdateThread(currentThread);
DetourDetach((PVOID*)&BaseAppPolicyGetWindowingModel, OverrideAppPolicyGetWindowingModel);
DetourTransactionCommit();
isHooked = false;
}
}

void HookWindowingModel::EndHook()
void HookWindowingModel::Close()
{
DetourTransactionBegin();
DetourUpdateThread(currentThread);
DetourDetach((PVOID*)&BaseAppPolicyGetWindowingModel, OverrideAppPolicyGetWindowingModel);
DetourTransactionCommit();
if (currentThread)
{
EndHook();
currentThread = nullptr;
}
}

LONG APIENTRY HookWindowingModel::OverrideAppPolicyGetWindowingModel(HANDLE processToken, AppPolicyWindowingModel* policy)
Expand Down
1 change: 1 addition & 0 deletions CoreAppUWP.WinRT/HookWindowingModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace winrt::CoreAppUWP::WinRT::implementation

void StartHook();
void EndHook();
void Close();

private:
bool isHooked = false;
Expand Down
2 changes: 1 addition & 1 deletion CoreAppUWP.WinRT/HookWindowingModel.idl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace CoreAppUWP.WinRT
};

[default_interface]
runtimeclass HookWindowingModel
runtimeclass HookWindowingModel : Windows.Foundation.IClosable
{
HookWindowingModel();

Expand Down
4 changes: 2 additions & 2 deletions CoreAppUWP.sln
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ Global
{86F40308-5864-4E45-98A4-336A5C404BC9}.Release|x86.ActiveCfg = Release|x86
{86F40308-5864-4E45-98A4-336A5C404BC9}.Release|x86.Build.0 = Release|x86
{86F40308-5864-4E45-98A4-336A5C404BC9}.Release|x86.Deploy.0 = Release|x86
{2F7E9384-91BE-4751-8322-C504B0F61AF2}.Debug|ARM64.ActiveCfg = Debug|ARM64EC
{2F7E9384-91BE-4751-8322-C504B0F61AF2}.Debug|ARM64.Build.0 = Debug|ARM64EC
{2F7E9384-91BE-4751-8322-C504B0F61AF2}.Debug|ARM64.ActiveCfg = Debug|ARM64
{2F7E9384-91BE-4751-8322-C504B0F61AF2}.Debug|ARM64.Build.0 = Debug|ARM64
{2F7E9384-91BE-4751-8322-C504B0F61AF2}.Debug|x64.ActiveCfg = Debug|x64
{2F7E9384-91BE-4751-8322-C504B0F61AF2}.Debug|x64.Build.0 = Debug|x64
{2F7E9384-91BE-4751-8322-C504B0F61AF2}.Debug|x86.ActiveCfg = Debug|Win32
Expand Down
43 changes: 29 additions & 14 deletions CoreAppUWP/Controls/DesktopWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,22 @@ public static Task<DesktopWindow> CreateAsync(Action<DesktopWindowXamlSource> la
{
try
{
HookWindowingModel hook = new();
hook.StartHook();
DispatcherQueueController controller = DispatcherQueueController.CreateOnCurrentThread();
DispatcherQueue dispatcherQueue = controller.DispatcherQueue;
DispatcherQueueController controller;
DesktopWindowXamlSource source;
AppWindow window = AppWindow.Create();
window.AssociateWithDispatcherQueue(dispatcherQueue);
TrackWindow(window);
DesktopWindowXamlSource source = new();
using (HookWindowingModel hook = new())
{
hook.StartHook();
controller = DispatcherQueueController.CreateOnCurrentThread();
source = new();
}
source.Initialize(window.Id);
DesktopChildSiteBridge bridge = source.SiteBridge;
bridge.ResizePolicy = ContentSizePolicy.ResizeContentToParentWindow;
bridge.Show();
window.Changed += (sender, args) =>
{
if (args.DidPresenterChange)
Expand All @@ -142,15 +147,19 @@ public static Task<DesktopWindow> CreateAsync(Action<DesktopWindowXamlSource> la
bridge.ResizePolicy = ContentSizePolicy.ResizeContentToParentWindow;
}
};
bridge.Show();
DispatcherQueue dispatcherQueue = controller.DispatcherQueue;
window.AssociateWithDispatcherQueue(dispatcherQueue);
TrackWindow(window);
launched(source);
hook.EndHook();
DesktopWindow desktopWindow = new()
{
AppWindow = window,
WindowXamlSource = source
};
taskCompletionSource.SetResult(desktopWindow);
dispatcherQueue.RunEventLoop();
await controller.ShutdownQueueAsync();
}
Expand Down Expand Up @@ -180,15 +189,22 @@ public static Task<DesktopWindow> CreateAsync(DispatcherQueue dispatcherQueue, A
{
try
{
HookWindowingModel hook = new();
hook.StartHook();
DesktopWindowXamlSource source;
AppWindow window = AppWindow.Create();
window.AssociateWithDispatcherQueue(dispatcherQueue);
TrackWindow(window);
DesktopWindowXamlSource source = new();
using (HookWindowingModel hook = new())
{
hook.StartHook();
source = new();
}
source.Initialize(window.Id);
DesktopChildSiteBridge bridge = source.SiteBridge;
bridge.ResizePolicy = ContentSizePolicy.ResizeContentToParentWindow;
bridge.Show();
window.Changed += (sender, args) =>
{
if (args.DidPresenterChange)
Expand All @@ -197,9 +213,8 @@ public static Task<DesktopWindow> CreateAsync(DispatcherQueue dispatcherQueue, A
bridge.ResizePolicy = ContentSizePolicy.ResizeContentToParentWindow;
}
};
bridge.Show();
launched(source);
hook.EndHook();
DesktopWindow desktopWindow = new()
{
AppWindow = window,
Expand Down
27 changes: 16 additions & 11 deletions CoreAppUWP/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,25 +58,30 @@ private static bool IsSupportCoreWindow
}
}

[MTAThread]
private static void Main()
{
ComWrappersSupport.InitializeComWrappers();
if (IsPackagedApp)
{
if (!IsSupportCoreWindow)
try
{
hookRegistry = new HookRegistry();
hookRegistry.StartHook();
if (!IsSupportCoreWindow)
{
hookRegistry = new HookRegistry();
hookRegistry.StartHook();
}
XamlCheckProcessRequirements();
Application.Start(p =>
{
DispatcherQueueSynchronizationContext context = new(DispatcherQueue.GetForCurrentThread());
SynchronizationContext.SetSynchronizationContext(context);
_ = new App();
});
}
XamlCheckProcessRequirements();
Application.Start(p =>
finally
{
DispatcherQueueSynchronizationContext context = new(DispatcherQueue.GetForCurrentThread());
SynchronizationContext.SetSynchronizationContext(context);
_ = new App();
});
hookRegistry?.EndHook();
hookRegistry?.Dispose();
}
}
else
{
Expand Down

0 comments on commit 45f7feb

Please sign in to comment.