Skip to content

Commit d21ce7a

Browse files
Migrate DPI awareness initialization to managed (#5765)
1 parent c52adfb commit d21ce7a

File tree

2 files changed

+25
-71
lines changed

2 files changed

+25
-71
lines changed

src/Microsoft.DotNet.Wpf/src/DirectWriteForwarder/main.cpp

-65
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,6 @@ using namespace System::Diagnostics;
3939
// code in this Assembly when the assembly is loaded into any AppDomain.
4040
//
4141

42-
//
43-
// We want to call SetProcessDPIAware from user32.dll only on machines
44-
// running Vista or later OSs. We provide our own declaration (the
45-
// original is in winuser.h) here so we can specify the DllImport attribute
46-
// which allows delayed loading of the function - thereby allowing us to
47-
// run on pre-Vista OSs.
48-
//
49-
50-
[DllImport("user32.dll", EntryPoint="SetProcessDPIAware")]
51-
WINUSERAPI
52-
BOOL
53-
WINAPI
54-
SetProcessDPIAware_Internal(
55-
VOID);
56-
57-
58-
#define WINNT_VISTA_VERSION 0x06
59-
6042
namespace MS { namespace Internal {
6143
private ref class NativeWPFDLLLoader sealed
6244
{
@@ -121,7 +103,6 @@ private class CModuleInitialize
121103
// Constructor of class CModuleInitialize
122104
__declspec(noinline) CModuleInitialize(void (*cleaningUpFunc)())
123105
{
124-
IsProcessDpiAware();
125106
MS::Internal::NativeWPFDLLLoader::LoadDwrite();
126107

127108
// Initialize some global arrays.
@@ -159,52 +140,6 @@ private class CModuleInitialize
159140
{
160141
return MS::Internal::NativeWPFDLLLoader::GetDWriteCreateFactoryFunctionPointer();
161142
}
162-
163-
private :
164-
165-
//
166-
// A private helper method to handle the DpiAwareness issue for current application.
167-
// This method is set as noinline since the MC++ compiler may otherwise inline it in a
168-
// Security Transparent method which will lead to a security violation where the transparent
169-
// method will be calling security critical code in this method.
170-
//
171-
__declspec(noinline) void IsProcessDpiAware( )
172-
{
173-
Version ^osVersion = (Environment::OSVersion)->Version;
174-
175-
if (osVersion->Major < WINNT_VISTA_VERSION)
176-
{
177-
// DPIAware feature is available only in Vista and after.
178-
return;
179-
}
180-
181-
//
182-
// Below code is only for Vista and newer platform.
183-
//
184-
Assembly ^ assemblyApp;
185-
Type ^ disableDpiAwareType = System::Windows::Media::DisableDpiAwarenessAttribute::typeid;
186-
bool bDisableDpiAware = false;
187-
188-
// By default, Application is DPIAware.
189-
assemblyApp = Assembly::GetEntryAssembly();
190-
191-
// Check if the Application has explicitly set DisableDpiAwareness attribute.
192-
if (assemblyApp != nullptr && Attribute::IsDefined(assemblyApp, disableDpiAwareType))
193-
{
194-
bDisableDpiAware = true;
195-
}
196-
197-
198-
if (!bDisableDpiAware)
199-
{
200-
// DpiAware composition is enabled for this application.
201-
SetProcessDPIAware_Internal( );
202-
}
203-
204-
// Only when DisableDpiAwareness attribute is set in Application assembly,
205-
// It will ignore the SetProcessDPIAware API call.
206-
}
207-
208143
};
209144

210145
void CleanUp();
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
2+
using System.Reflection;
63
using System.Runtime.CompilerServices;
4+
using System.Runtime.InteropServices;
75

86
internal static class ModuleInitializer
97
{
@@ -15,11 +13,32 @@ internal static class ModuleInitializer
1513
/// operations are carried out. To do this, we simply call LoadDwrite
1614
/// as the module constructor for DirectWriteForwarder would do this anyway.
1715
/// </summary>
18-
#pragma warning disable CA2255
16+
#pragma warning disable CA2255
1917
[ModuleInitializer]
2018
public static void Initialize()
2119
{
20+
IsProcessDpiAware();
21+
2222
MS.Internal.NativeWPFDLLLoader.LoadDwrite();
2323
}
24-
#pragma warning restore CA2255
24+
#pragma warning restore CA2255
25+
26+
private static void IsProcessDpiAware()
27+
{
28+
// By default, Application is DPIAware.
29+
Assembly assemblyApp = Assembly.GetEntryAssembly();
30+
31+
// Check if the Application has explicitly set DisableDpiAwareness attribute.
32+
if (assemblyApp != null && Attribute.IsDefined(assemblyApp, typeof(System.Windows.Media.DisableDpiAwarenessAttribute)))
33+
{
34+
// DpiAware composition is enabled for this application.
35+
SetProcessDPIAware_Internal();
36+
}
37+
38+
// Only when DisableDpiAwareness attribute is set in Application assembly,
39+
// It will ignore the SetProcessDPIAware API call.
40+
}
41+
42+
[DllImport("user32.dll", EntryPoint = "SetProcessDPIAware")]
43+
private static extern void SetProcessDPIAware_Internal();
2544
}

0 commit comments

Comments
 (0)