调用 GetProcessDPIAwareness 时出错

Error calling GetProcessDPIAwareness

我正在尝试从 Windows 8.1 SDK 移植 GetProcessDPIAwareness。然而,当我尝试启动这个程序时,它只是崩溃并出现错误:

system exception (code 0xc0000409) at 0x77929990'

该函数存在于 shcore.dll 中。我是 运行 Windows 8.1 所以它应该可以工作但没有。

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.

将 dll 名称添加到外部声明后,代码实际编译。那么你只需要按照TLama给出的建议,使用正确的程序名称即可:

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