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

1
  • Accidentally entered an answer as a comment (see below for my answer). Commented Jan 20, 2011 at 11:24

2 Answers 2

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;
Sign up to request clarification or add additional context in comments.

4 Comments

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 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 synopse.info/fossil/finfo?name=SQLite3/SQLite3.pas
@Ramnish take a look at this site: rvelthuis.de/articles/articles-cobjs.html
a procedural variable is already a pointer, so the ^ in the TJAlloc record for every field is wrong? Or am I mistaken?
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 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.