Skip to content

Commit

Permalink
补充劫持器注释
Browse files Browse the repository at this point in the history
  • Loading branch information
wherewhere committed Oct 5, 2024
1 parent 819b790 commit 161b956
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 6 deletions.
50 changes: 49 additions & 1 deletion CoreAppUWP/Common/HookRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,68 @@

namespace CoreAppUWP.Common
{
public class HookRegistry : IDisposable
/// <summary>
/// Represents a hook for getting the value of the <c>HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\WinUI\Xaml\EnableUWPWindow</c> registry key always returning <see langword="00000001"/>.
/// </summary>
public partial class HookRegistry : IDisposable
{
/// <summary>
/// The value that indicates whether the class has been disposed.
/// </summary>
private bool disposed;

/// <summary>
/// The reference count for the hook.
/// </summary>
private static int refCount;

/// <summary>
/// The dictionary that maps the <see cref="HKEY"/> to a value that indicates whether the key is a real key.
/// </summary>
private static readonly Dictionary<HKEY, bool> xamlKeyMap = [];

/// <summary>
/// The object used to synchronize access to the <see cref="xamlKeyMap"/> dictionary.
/// </summary>
private static readonly object locker = new();

/// <remarks>The original <see cref="PInvoke.RegOpenKeyEx(HKEY, PCWSTR, uint, REG_SAM_FLAGS, HKEY*)"/> function.</remarks>
/// <inheritdoc cref="PInvoke.RegOpenKeyEx(HKEY, PCWSTR, uint, REG_SAM_FLAGS, HKEY*)"/>
private static unsafe delegate* unmanaged[Stdcall]<HKEY, PCWSTR, uint, REG_SAM_FLAGS, HKEY*, WIN32_ERROR> RegOpenKeyExW;

/// <remarks>The original <see cref="PInvoke.RegCloseKey(HKEY)"/> function.</remarks>
/// <inheritdoc cref="PInvoke.RegCloseKey(HKEY)"/>
private static unsafe delegate* unmanaged[Stdcall]<HKEY, WIN32_ERROR> RegCloseKey;

/// <remarks>The original <see cref="PInvoke.RegQueryValueEx(HKEY, PCWSTR, uint*, REG_VALUE_TYPE*, byte*, uint*)"/> function.</remarks>
/// <inheritdoc cref="PInvoke.RegQueryValueEx(HKEY, PCWSTR, uint*, REG_VALUE_TYPE*, byte*, uint*)"/>
private static unsafe delegate* unmanaged[Stdcall]<HKEY, PCWSTR, uint*, REG_VALUE_TYPE*, byte*, uint*, WIN32_ERROR> RegQueryValueExW;

/// <summary>
/// Initializes a new instance of the <see cref="HookRegistry"/> class.
/// </summary>
public HookRegistry()
{
refCount++;
StartHook();
}

/// <summary>
/// Finalizes this instance of the <see cref="HookRegistry"/> class.
/// </summary>
~HookRegistry()
{
Dispose();
}

/// <summary>
/// Gets the value that indicates whether the hook is active.
/// </summary>
public static bool IsHooked { get; private set; }

/// <summary>
/// Starts the hook for the <see cref="PInvoke.AppPolicyGetWindowingModel(HANDLE, AppPolicyWindowingModel*)"/> function.
/// </summary>
private static unsafe void StartHook()
{
if (!IsHooked)
Expand Down Expand Up @@ -69,6 +107,9 @@ private static unsafe void StartHook()
}
}

/// <summary>
/// Ends the hook for the <see cref="PInvoke.AppPolicyGetWindowingModel(HANDLE, AppPolicyWindowingModel*)"/> function.
/// </summary>
public static unsafe void EndHook()
{
if (--refCount == 0 && IsHooked)
Expand Down Expand Up @@ -96,6 +137,8 @@ public static unsafe void EndHook()
}
}

/// <remarks>The overridden <see cref="PInvoke.RegOpenKeyEx(HKEY, PCWSTR, uint, REG_SAM_FLAGS, HKEY*)"/> function.</remarks>
/// <inheritdoc cref="PInvoke.RegOpenKeyEx(HKEY, PCWSTR, uint, REG_SAM_FLAGS, HKEY*)"/>
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])]
private static unsafe WIN32_ERROR OverrideRegOpenKeyExW(HKEY hKey, PCWSTR lpSubKey, uint ulOptions, REG_SAM_FLAGS samDesired, HKEY* phkResult)
{
Expand All @@ -117,6 +160,8 @@ private static unsafe WIN32_ERROR OverrideRegOpenKeyExW(HKEY hKey, PCWSTR lpSubK
return result;
}

/// <remarks>The overridden <see cref="PInvoke.RegCloseKey(HKEY)"/> function.</remarks>
/// <inheritdoc cref="PInvoke.RegCloseKey(HKEY)"/>
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])]
private static unsafe WIN32_ERROR OverrideRegCloseKey(HKEY hKey)
{
Expand All @@ -137,6 +182,8 @@ private static unsafe WIN32_ERROR OverrideRegCloseKey(HKEY hKey)
}
}

/// <remarks>The overridden <see cref="PInvoke.RegQueryValueEx(HKEY, PCWSTR, uint*, REG_VALUE_TYPE*, byte*, uint*)"/> function.</remarks>
/// <inheritdoc cref="PInvoke.RegQueryValueEx(HKEY, PCWSTR, uint*, REG_VALUE_TYPE*, byte*, uint*)"/>
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])]
private static unsafe WIN32_ERROR OverrideRegQueryValueExW(HKEY hKey, PCWSTR lpValueName, [Optional] uint* lpReserved, [Optional] REG_VALUE_TYPE* lpType, [Optional] byte* lpData, [Optional] uint* lpcbData)
{
Expand Down Expand Up @@ -205,6 +252,7 @@ private static unsafe WIN32_ERROR OverrideRegQueryValueExW(HKEY hKey, PCWSTR lpV
return RegQueryValueExW(hKey, lpValueName, lpReserved, lpType, lpData, lpcbData);
}

/// <inheritdoc/>
public void Dispose()
{
if (!disposed && IsHooked)
Expand Down
43 changes: 42 additions & 1 deletion CoreAppUWP/Common/HookWindowingModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,60 @@

namespace CoreAppUWP.Common
{
public sealed class HookWindowingModel : IDisposable
/// <summary>
/// Represents a hook for the <see cref="PInvoke.AppPolicyGetWindowingModel(HANDLE, AppPolicyWindowingModel*)"/> function.
/// </summary>
public sealed partial class HookWindowingModel : IDisposable
{
/// <summary>
/// The value that indicates whether the class has been disposed.
/// </summary>
private bool disposed;

/// <summary>
/// The reference count for the hook.
/// </summary>
private static int refCount;

/// <summary>
/// The value that represents the current process token.
/// </summary>
private const int currentProcessToken = -6;

/// <remarks>The original <see cref="PInvoke.AppPolicyGetWindowingModel(HANDLE, AppPolicyWindowingModel*)"/> function.</remarks>
/// <inheritdoc cref="PInvoke.AppPolicyGetWindowingModel(HANDLE, AppPolicyWindowingModel*)"/>
private static unsafe delegate* unmanaged[Stdcall]<HANDLE, AppPolicyWindowingModel*, WIN32_ERROR> AppPolicyGetWindowingModel;

/// <summary>
/// Initializes a new instance of the <see cref="HookWindowingModel"/> class.
/// </summary>
public HookWindowingModel()
{
refCount++;
StartHook();
}

/// <summary>
/// Finalizes this instance of the <see cref="HookWindowingModel"/> class.
/// </summary>
~HookWindowingModel()
{
Dispose();
}

/// <summary>
/// Gets the value that indicates whether the hook is active.
/// </summary>
public static bool IsHooked { get; private set; }

/// <summary>
/// Gets or sets the windowing model to use when the hooked <see cref="PInvoke.AppPolicyGetWindowingModel(HANDLE, AppPolicyWindowingModel*)"/> function is called.
/// </summary>
internal static AppPolicyWindowingModel WindowingModel { get; set; } = AppPolicyWindowingModel.AppPolicyWindowingModel_ClassicDesktop;

/// <summary>
/// Starts the hook for the <see cref="PInvoke.AppPolicyGetWindowingModel(HANDLE, AppPolicyWindowingModel*)"/> function.
/// </summary>
private static unsafe void StartHook()
{
if (!IsHooked)
Expand All @@ -52,6 +85,9 @@ private static unsafe void StartHook()
}
}

/// <summary>
/// Ends the hook for the <see cref="PInvoke.AppPolicyGetWindowingModel(HANDLE, AppPolicyWindowingModel*)"/> function.
/// </summary>
private static unsafe void EndHook()
{
if (--refCount == 0 && IsHooked)
Expand All @@ -69,6 +105,10 @@ private static unsafe void EndHook()
}
}

/// <param name="policy">A pointer to a variable of the <a href="https://docs.microsoft.com/windows/win32/api/appmodel/ne-appmodel-apppolicywindowingmodel">AppPolicyWindowingModel</a> enumerated type.
/// When the function returns successfully, the variable contains the <see cref="WindowingModel"/> when the identified process is current; otherwise, the windowing model of the identified process.</param>
/// <remarks>The overridden <see cref="PInvoke.AppPolicyGetWindowingModel(HANDLE, AppPolicyWindowingModel*)"/> function.</remarks>
/// <inheritdoc cref="PInvoke.AppPolicyGetWindowingModel(HANDLE, AppPolicyWindowingModel*)"/>
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])]
private static unsafe WIN32_ERROR OverrideAppPolicyGetWindowingModel(HANDLE processToken, AppPolicyWindowingModel* policy)
{
Expand All @@ -80,6 +120,7 @@ private static unsafe WIN32_ERROR OverrideAppPolicyGetWindowingModel(HANDLE proc
return AppPolicyGetWindowingModel(processToken, policy);
}

/// <inheritdoc/>
public void Dispose()
{
if (!disposed && IsHooked)
Expand Down
6 changes: 3 additions & 3 deletions CoreAppUWP/CoreAppUWP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<ApplicationManifest>app.manifest</ApplicationManifest>
<BuiltInComInteropSupport>True</BuiltInComInteropSupport>
<CsWinRTIncludes>CoreAppUWP.WinRT</CsWinRTIncludes>
<DefineConstants>DISABLE_XAML_GENERATED_MAIN</DefineConstants>
<DefineConstants>$(DefineConstants);DISABLE_XAML_GENERATED_MAIN</DefineConstants>
<EnableMsixTooling>True</EnableMsixTooling>
<IsAotCompatible>True</IsAotCompatible>
<NoWarn>$(NoWarn);NU1902;NU1903;NU1904</NoWarn>
Expand All @@ -31,8 +31,8 @@
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.3.106" PrivateAssets="all">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Windows.CsWinRT" Version="2.1.3" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.6.240829007" />
<PackageReference Include="Microsoft.Windows.CsWinRT" Version="2.1.5" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.6.240923002" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.26100.1742" />
<Manifest Include="$(ApplicationManifest)" />
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion CoreAppUWP/ViewModels/SettingsPages/SettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

namespace CoreAppUWP.ViewModels.SettingsPages
{
public class SettingsViewModel : INotifyPropertyChanged
public partial class SettingsViewModel : INotifyPropertyChanged
{
public static ConditionalWeakTable<DispatcherQueue, SettingsViewModel> Caches { get; } = [];

Expand Down
Binary file modified EnableUWPWindow.reg
Binary file not shown.

0 comments on commit 161b956

Please sign in to comment.