gpt4 book ai didi

delphi - 如何声明仅存在于 Windows 8 上的外部函数而不会使应用程序在 Windows 7 及更低版本上崩溃?

转载 作者:行者123 更新时间:2023-12-04 04:13:02 25 4
gpt4 key购买 nike

看起来很愚蠢,但它们是声明外部函数的一种方式,例如:

function EnumDynamicTimeZoneInformation(dwIndex: DWORD; lpTimeZoneInformation: PDynamicTimeZoneInformation): DWORD; stdcall; external advapi32;

因为 EnumDynamicTimeZoneInformation 仅在 Windows 8 中可用,所以此声明会使应用程序在 Windows 7 及更低版本上崩溃。

The procedure entry point EnumDynamicTimeZoneInformation could not be located in the dynamic link library

不管怎样,他们是否要让这个声明不会让应用程序崩溃,即使我们不在应用程序中使用它也是如此

最佳答案

在 Delphi 2010 及更高版本中,您可以使用 delayed directiveexternal 声明的末尾,例如:

function EnumDynamicTimeZoneInformation(dwIndex: DWORD; lpTimeZoneInformation: PDynamicTimeZoneInformation): DWORD; stdcall; external advapi32 delayed;

The delayed directive can be used to decorate an external routine to delay the loading of the library containing the routine. The actual loading happens when the routine is called for the first time.

...

The delayed directive is useful in the case where the imported routines do not exist on the target operating system on which the application is run. Statically imported routines require that the operating system find and load the library when the application is started. If the routine is not found in the loaded library, or the library does not exist, the Operating System halts the execution of the application. Using the delayed directive enables you to check, at run time, whether the Operating System supports the required APIs; only then you can call the imported routines.

例如:

if CheckWin32Version(6, 2) then
begin
EnumDynamicTimeZoneInformation(DesiredIndex);
end;

关于delphi - 如何声明仅存在于 Windows 8 上的外部函数而不会使应用程序在 Windows 7 及更低版本上崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60580212/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com