From d21ce7a8f67b068d9543068ce1d3b09b8dfbb758 Mon Sep 17 00:00:00 2001 From: ThomasGoulet73 <51839772+ThomasGoulet73@users.noreply.github.com> Date: Mon, 3 Jan 2022 05:59:47 -0500 Subject: [PATCH] Migrate DPI awareness initialization to managed (#5765) --- .../src/DirectWriteForwarder/main.cpp | 65 ------------------- .../src/PresentationCore/ModuleInitializer.cs | 31 +++++++-- 2 files changed, 25 insertions(+), 71 deletions(-) diff --git a/src/Microsoft.DotNet.Wpf/src/DirectWriteForwarder/main.cpp b/src/Microsoft.DotNet.Wpf/src/DirectWriteForwarder/main.cpp index ab78bb08d..06ba7f1c1 100644 --- a/src/Microsoft.DotNet.Wpf/src/DirectWriteForwarder/main.cpp +++ b/src/Microsoft.DotNet.Wpf/src/DirectWriteForwarder/main.cpp @@ -39,24 +39,6 @@ using namespace System::Diagnostics; // code in this Assembly when the assembly is loaded into any AppDomain. // -// -// We want to call SetProcessDPIAware from user32.dll only on machines -// running Vista or later OSs. We provide our own declaration (the -// original is in winuser.h) here so we can specify the DllImport attribute -// which allows delayed loading of the function - thereby allowing us to -// run on pre-Vista OSs. -// - -[DllImport("user32.dll", EntryPoint="SetProcessDPIAware")] -WINUSERAPI -BOOL -WINAPI -SetProcessDPIAware_Internal( - VOID); - - -#define WINNT_VISTA_VERSION 0x06 - namespace MS { namespace Internal { private ref class NativeWPFDLLLoader sealed { @@ -121,7 +103,6 @@ public: // Constructor of class CModuleInitialize __declspec(noinline) CModuleInitialize(void (*cleaningUpFunc)()) { - IsProcessDpiAware(); MS::Internal::NativeWPFDLLLoader::LoadDwrite(); // Initialize some global arrays. @@ -159,52 +140,6 @@ public: { return MS::Internal::NativeWPFDLLLoader::GetDWriteCreateFactoryFunctionPointer(); } - -private : - - // - // A private helper method to handle the DpiAwareness issue for current application. - // This method is set as noinline since the MC++ compiler may otherwise inline it in a - // Security Transparent method which will lead to a security violation where the transparent - // method will be calling security critical code in this method. - // - __declspec(noinline) void IsProcessDpiAware( ) - { - Version ^osVersion = (Environment::OSVersion)->Version; - - if (osVersion->Major < WINNT_VISTA_VERSION) - { - // DPIAware feature is available only in Vista and after. - return; - } - - // - // Below code is only for Vista and newer platform. - // - Assembly ^ assemblyApp; - Type ^ disableDpiAwareType = System::Windows::Media::DisableDpiAwarenessAttribute::typeid; - bool bDisableDpiAware = false; - - // By default, Application is DPIAware. - assemblyApp = Assembly::GetEntryAssembly(); - - // Check if the Application has explicitly set DisableDpiAwareness attribute. - if (assemblyApp != nullptr && Attribute::IsDefined(assemblyApp, disableDpiAwareType)) - { - bDisableDpiAware = true; - } - - - if (!bDisableDpiAware) - { - // DpiAware composition is enabled for this application. - SetProcessDPIAware_Internal( ); - } - - // Only when DisableDpiAwareness attribute is set in Application assembly, - // It will ignore the SetProcessDPIAware API call. - } - }; void CleanUp(); diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/ModuleInitializer.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/ModuleInitializer.cs index b783164a5..f8e736e4b 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/ModuleInitializer.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/ModuleInitializer.cs @@ -1,9 +1,7 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.Reflection; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; internal static class ModuleInitializer { @@ -15,11 +13,32 @@ internal static class ModuleInitializer /// operations are carried out. To do this, we simply call LoadDwrite /// as the module constructor for DirectWriteForwarder would do this anyway. /// - #pragma warning disable CA2255 +#pragma warning disable CA2255 [ModuleInitializer] public static void Initialize() { + IsProcessDpiAware(); + MS.Internal.NativeWPFDLLLoader.LoadDwrite(); } - #pragma warning restore CA2255 +#pragma warning restore CA2255 + + private static void IsProcessDpiAware() + { + // By default, Application is DPIAware. + Assembly assemblyApp = Assembly.GetEntryAssembly(); + + // Check if the Application has explicitly set DisableDpiAwareness attribute. + if (assemblyApp != null && Attribute.IsDefined(assemblyApp, typeof(System.Windows.Media.DisableDpiAwarenessAttribute))) + { + // DpiAware composition is enabled for this application. + SetProcessDPIAware_Internal(); + } + + // Only when DisableDpiAwareness attribute is set in Application assembly, + // It will ignore the SetProcessDPIAware API call. + } + + [DllImport("user32.dll", EntryPoint = "SetProcessDPIAware")] + private static extern void SetProcessDPIAware_Internal(); } -- GitLab