3

I want to integrate a piece of c#/WPF code into a legacy (non .net) application. Normally one would use the DisableDpiAwareness attribute, but because the application is not running on .net Assembly.GetEntryAssembly() returns null and thus the DisableDpiAwareness attribute does not work (I believe).

I have tried using the dpiAware attribute in the manifest to disable the dpi awareness, but that didn't work either.

Is there a way to prevent WPF calling SetProcessDPIAware in this situation? These are the two options I can think of:

  • Somehow intercept the PInvoke call to SetProcessDPIAware.
  • Make GetEntryAssembly() return an assembly.

I've read about creating an AppDomain which has an entry assembly, but if this works I am afraid that I will then need to marshal all calls between the two AppDomains somehow.

Herman
  • 2,738
  • 19
  • 32
  • See fourth bullet in accepted answer for duplicate question. – Peter Duniho Sep 17 '20 at 00:26
  • @PeterDuniho Good find for the duplicate, didn't see that at the time. Although I think both my question and my answer contain valuable specifics which are not available in the duplicate. – Herman Sep 17 '20 at 00:34

1 Answers1

2

It seems I have been able to fix my issues by calling SetProcessDpiAwareness with the unaware setting before WPF is loaded. The SetProcessDPIAware is still called by WPF and it returns true, but IsProcessDPIAware always returns false and my application looks normal in my early tests.

[DllImport("SHCore.dll", SetLastError = true)]
private static extern bool SetProcessDpiAwareness(PROCESS_DPI_AWARENESS awareness);

private enum PROCESS_DPI_AWARENESS {
    Process_DPI_Unaware = 0,
    Process_System_DPI_Aware = 1,
    Process_Per_Monitor_DPI_Aware = 2
}

Then call this before any UI code:

SetProcessDpiAwareness(PROCESS_DPI_AWARENESS.Process_DPI_Unaware);

Beware that this function does not exist before Windows 8.1, so you need to check the OS version similar as shown in github page linked in my question (although that code checks for Vista).

Herman
  • 2,738
  • 19
  • 32