Search code examples
delphiwinapidelphi-xe8

Error calling GetProcessDPIAwareness


I am trying to port GetProcessDPIAwareness from the Windows 8.1 SDK. However when I try to start this program it just crashes with the error:

system exception (code 0xc0000409) at 0x77929990'

The function is present in shcore.dll. I am running Windows 8.1 so it should work but it doesn't.

program Test;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils,
  Winapi.Windows;

const
  shcore = 'shcore.dll';

type
  HANDLE = THandle;

  PROCESS_DPI_AWARENESS = (
    PROCESS_DPI_UNAWARE = 0,
    PROCESS_SYSTEM_DPI_AWARE = 1,
    PROCESS_PER_MONITOR_DPI_AWARE = 2
  );

function GetProcessDPIAwareness(
  {_In_}  hprocess: HANDLE;
  {_Out_} out value: PROCESS_DPI_AWARENESS
): HRESULT; WINAPI; external  name 'GetProcessDPIAwareness';

var
  DPI: PROCESS_DPI_AWARENESS;
begin
  GetProcessDPIAwareness(0, DPI); // crashes here
  ReadLn;
end.

Solution

  • After adding the dll name to the external declaration the code actually compiles. Then you only have to follow the advice given by TLama and use the correct procedure name:

    function GetProcessDpiAwareness(
      {_In_}  hprocess: HANDLE;
      {_Out_} out value: PROCESS_DPI_AWARENESS
    ): HRESULT; WINAPI; external shcore;