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

发布于 2024-07-18 04:22:56 字数 459 浏览 8 评论 0原文

我想从 kernel32.dll 库中声明一个外部函数,其名称为 GetTickCount64。 据我所知,它仅在 Vista 和更高版本的 Windows 版本中定义。 这意味着当我按如下方式定义函数时:

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

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

这个问题有解决方法吗? 假设我不想在该函数不存在时包含该函数,然后在我的代码中使用一些替代函数。 怎么做? 是否有任何编译器指令有帮助? 我想这个定义必须被这样的指令包围,而且我还必须在使用 GetTickCount64 函数的地方使用一些指令,对吧?

我们将不胜感激您的帮助。 提前致谢。

马吕斯。

I would like to declare an external function from the kernel32.dll library whose name is GetTickCount64. As far as I know, it is defined only in Vista and in later Windows versions. This means that when I define the function as follows:

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

I will certainly not be able to run my application on previous versions of Windows because of error generated on application startup.

Is there a workaround to that problem? Let's say I would like not to include that function when it does not exist and then use some substitute function in my code. How to do that? Are there any compiler directives that would help?
I gues the definition would have to be surrounded by such directive and I would also have to use some directives wherever I use the GetTickCount64 founction, right?

Your help will be appreciated. Thanks in advance.

Mariusz.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光与爱终年不遇 2024-07-25 04:22:56

声明该类型的函数指针,然后在运行时使用 LoadLibraryGetModuleHandle GetProcAddress。 您可以在 Delphi 源代码中找到该技术的几个示例; 查看TlHelp32.pas,它加载ToolHelp 库< /a>,这在旧版本的 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;

Declare a function pointer of that type, and then load the function at run time with LoadLibrary or GetModuleHandle and GetProcAddress. You can find several examples of the technique in the Delphi source code; look at TlHelp32.pas, which loads the ToolHelp library, which isn't available on older versions of 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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文