C 函数指针转换为 Delphi/Pascal?

发布于 2024-10-13 19:26:17 字数 491 浏览 9 评论 0原文

我目前正在将一些 C 标头翻译成 Delphi。我找不到将函数指针从 C 转换为 Delphi 的参考。

typedef _JAlloc JAlloc;  
struct _JAlloc {  
    void *(*alloc) (JAlloc *allocator, size_t size);  
    void (*free) (JAlloc *allocator, void *p);  
    void *(*realloc) (JAlloc *allocator, void *p, size_t size);  
};
  1. 这个的 Delphi 翻译是什么?

  2. 在哪里可以找到将 C 头文件手动转换为 Delphi 的好资源(包括指针、预处理器指令等)?

I am currently translating some C headers into Delphi. I am unable find a reference for converting a function pointer from C into Delphi.

typedef _JAlloc JAlloc;  
struct _JAlloc {  
    void *(*alloc) (JAlloc *allocator, size_t size);  
    void (*free) (JAlloc *allocator, void *p);  
    void *(*realloc) (JAlloc *allocator, void *p, size_t size);  
};
  1. What will be the Delphi translation of this?

  2. Where can I find good resources for manual conversion of C headers to Delphi (including pointer, preprocessor directives, etc.)?

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

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

发布评论

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

评论(2

赴月观长安 2024-10-20 19:26:17

使用这种代码

type
  PJAlloc = ^TJAlloc;
  TJAllocAlloc = function(allocator: PJAlloc; size: integer): pointer; cdecl;
  TJAllocFree = procedure(allocator: PJAlloc; p: pointer); cdecl;
  TJAllocRealloc = function(allocator: PJAlloc; p: pointer; size: integer); cdecl;
  TJAlloc = record
    alloc: ^TJAllocAlloc;
    free: ^TJAllocFree;
    realloc: ^TJAllocRealloc;
  end;

并将 cdecl 更改为 stdcall,具体取决于您的 C 库的调用约定。

另一种声明(也许更“帕斯卡式”)可能是:

type
  TJAllocAlloc = function(var allocator: TJAlloc; size: integer): pointer; cdecl;
  TJAllocFree = procedure(var allocator: TJAlloc; p: pointer); cdecl;
  TJAllocRealloc = function(var allocator: TJAlloc; p: pointer; size: integer); cdecl;
  TJAlloc = record
    alloc: ^TJAllocAlloc;
    free: ^TJAllocFree;
    realloc: ^TJAllocRealloc;
  end;

Use this kind of code

type
  PJAlloc = ^TJAlloc;
  TJAllocAlloc = function(allocator: PJAlloc; size: integer): pointer; cdecl;
  TJAllocFree = procedure(allocator: PJAlloc; p: pointer); cdecl;
  TJAllocRealloc = function(allocator: PJAlloc; p: pointer; size: integer); cdecl;
  TJAlloc = record
    alloc: ^TJAllocAlloc;
    free: ^TJAllocFree;
    realloc: ^TJAllocRealloc;
  end;

And change cdecl to stdcall, depending of the calling convention of your C library.

An alternative declaration (more 'pascalish' perhaps) could be:

type
  TJAllocAlloc = function(var allocator: TJAlloc; size: integer): pointer; cdecl;
  TJAllocFree = procedure(var allocator: TJAlloc; p: pointer); cdecl;
  TJAllocRealloc = function(var allocator: TJAlloc; p: pointer; size: integer); cdecl;
  TJAlloc = record
    alloc: ^TJAllocAlloc;
    free: ^TJAllocFree;
    realloc: ^TJAllocRealloc;
  end;
苍风燃霜 2024-10-20 19:26:17

Bob 博士的 HeadConv 实用程序是一个很好的用于将 C 声明转换为 Delphi 的实用程序,也是一个很好的学习工具,用于将 C 源代码与等效的 Pascal 源代码进行比较。

您可以在这里找到它

Dr. Bob's HeadConv utility is a good one to use for converting C declarations into Delphi, and is a good learning tool for comparing C source code to the equivalent Pascal source code.

You can find it here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文