当前位置:首页 > Windows程序 > 正文

Delphi中增强程序的不同Windows版本的兼容性技巧

2021-03-24 Windows程序

在开发过程中,一个程序(DLL/EXE)经常需要同时兼容不同版本的Windows(XP、WIN7 ...),,但是由于新版本的Windows某些API在旧版本不存在,如果程序使用external引入API,则会导致在旧版本Windows中运行时提示“找不到某某程序文件路径...”、"无法加载DLL...",因为在旧版本Windows中运行EXE或DLL,系统会先检测external引入的API,如果找不到某个API,系统会直接报错,无法运行,这时候Delphi提供了关键字 delayed来修饰external

如下面的API

function RegDeleteKeyEx; external advapi32 name ‘RegDeleteKeyExW‘ delayed;
function RegDeleteKeyExA; external advapi32 name ‘RegDeleteKeyExA‘ delayed;
function RegDeleteKeyExW; external advapi32 name ‘RegDeleteKeyExW‘ delayed;

增加delayed意味着只有在真正第一次调用该API时,才去加载该DLL和API,当然这只是字面上的意思,

实际上(大概原理)Delphi是采用了HOOK的方法在系统加载EXE或DLL时拦截了LoadLibrary和GetProcAddress等(详情可查看SysInit单元),屏蔽了原来系统将会弹出的异常并结束进程的动作。

Delphi中增强程序的不同Windows版本的兼容性技巧

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/66797.html