6

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.)?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ramnish
  • 322
  • 3
  • 12

2 Answers2

9

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;
Arnaud Bouchez
  • 42,305
  • 3
  • 71
  • 159
  • Thanks Bouchez. Do you know where could i find more information about translating from C to delphi in internet? I am currently using Rudy's Blog. – Ramnish Jan 20 '11 at 10:38
  • 1
    @Ramnish I don't have any resource at hand. I learned it with experiment. But take a look at some manual conversion of some C APIs (like OpenGL/GDI+/SQLite) and you'll find some tips. What is nice with Delphi is that you can compile the C code with C++ builder (including the free command line compiler), then link the resulting .obj to your Delphi unit. I've used some low-level asm tips like _ftol/_ftoul/_lldiv/_llshr and such in http://synopse.info/fossil/finfo?name=SQLite3/SQLite3.pas – Arnaud Bouchez Jan 20 '11 at 14:06
  • @Ramnish take a look at this site: http://rvelthuis.de/articles/articles-cobjs.html – Arnaud Bouchez Jan 31 '11 at 16:57
  • a procedural variable is already a pointer, so the ^ in the TJAlloc record for every field is wrong? Or am I mistaken? – Marco van de Voort Nov 23 '16 at 11:58
1

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

  • 1
    It worked for the ODBC 3.0 API headers, with a few tweaks to the resultant code. As for ancient - C hasn't changed, nor has the Pascal syntax for calling external DLLs and libraries, in the last 12-15 years. – Tim Young - Elevate Software Jan 20 '11 at 15:27