加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – 根据它们是否存在来声明外部函数

发布时间:2020-12-15 09:39:22 所属栏目:大数据 来源:网络整理
导读:我想从kernel32.dll库声明一个名为GetTickCount64的外部函数.据我所知,它仅在Vista和后来的 Windows版本中定义.这意味着当我定义函数时如下: function GetTickCount64: int64; external kernel32 name 'GetTickCount64'; 由于在应用程序启动时生成错误,我肯
我想从kernel32.dll库声明一个名为GetTickCount64的外部函数.据我所知,它仅在Vista和后来的 Windows版本中定义.这意味着当我定义函数时如下:

function GetTickCount64: int64; external kernel32 name 'GetTickCount64';

由于在应用程序启动时生成错误,我肯定无法在以前版本的Windows上运行我的应用程序.

这个问题有解决方法吗?假设我不想在不存在时包含该函数,然后在我的代码中使用一些替换函数.怎么做?是否有任何编译器指令可以帮助?
我猜这个定义必须被这样的指令所包围,我还必须使用一些指令,无论我在哪里使用GetTickCount64功能,对吧?

我们将不胜感激.提前致谢.

马里乌什.

解决方法

声明该类型的函数指针,然后在运行时使用 LoadLibraryGetModuleHandleGetProcAddress加载该函数.您可以在Delphi源代码中找到该技术的几个示例;看看TlHelp32.pas,它加载 ToolHelp library,这在旧版本的Windows NT上不可用.

interface

function GetTickCount64: Int64;

implementation

uses Windows,SysUtils;

type
   // Don't forget stdcall for API functions.
  TGetTickCount64 = function: Int64; stdcall;

var
  _GetTickCount64: TGetTickCount64;

// Load the Vista function if available,and call it.
// Raise EOSError if the function isn't available.
function GetTickCount64: Int64;
var
  kernel32: HModule;
begin
  if not Assigned(_GetTickCount64) then begin
    // Kernel32 is always loaded already,so use GetModuleHandle
    // instead of LoadLibrary
    kernel32 := GetModuleHandle('kernel32');
    if kernel32 = 0 then
      RaiseLastOSError;
    @_GetTickCount := GetProcAddress(kernel32,'GetTickCount64');
    if not Assigned(_GetTickCount64) then
      RaiseLastOSError;
  end;
  Result := _GetTickCount64;
end;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读