Solved

C to Delphi: typedef struct ...

Posted on 2007-11-22
8
1,947 Views
Last Modified: 2010-05-18
I'm trying to convert some of the headers from GSL package written in C, into Delphi.

There are several fields in some structures which I can't interpret, for example: gsl_rng_type.

-----------------------------------------
typedef struct
  {
    const char *name;
    unsigned long int max;
    unsigned long int min;
    size_t size;
    void (*set) (void *state, unsigned long int seed);    // <- ???????
    unsigned long int (*get) (void *state);     // <- ???????
    double (*get_double) (void *state);    // <- ???????
  }
gsl_rng_type;
-----------------------------------------

I got as far as....
-----------------------------------------
  gsl_rng_type = packed record
    name: PChar;
    max: LongInt;
    min: LongInt;
    size: Cardinal;
-----------------------------------------
0
Comment
Question by:den4b
  • 4
  • 2
  • 2
8 Comments
 
LVL 11

Accepted Solution

by:
DeepuAbrahamK earned 300 total points
ID: 20333843
These are function pointers or in other words call back functions.Check what is the replacement for function pointers

void (*set) (void *state, unsigned long int seed);
unsigned long int (*get) (void *state);
double (*get_double) (void *state);
0
 
LVL 6

Author Comment

by:den4b
ID: 20333853
I thought that they might be method pointers, but I'm not sure how to correctly translate them.... That is exactly my question.
0
 
LVL 6

Author Comment

by:den4b
ID: 20333874
Would that be correct translation?
  _gsl_rng_type = packed record
    name: PChar;
    max: LongInt;
    min: LongInt;
    size: Cardinal;
    set_: procedure(state: Pointer; seed: LongInt);
    get_: function(state: Pointer): LongInt;
    get_double: function(state: Pointer): Double;
  end;

Open in new window

0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 11

Expert Comment

by:DeepuAbrahamK
ID: 20334105
Not sure about call backs in Delphi, but have a look
http://delphi.about.com/od/windowsshellapi/a/callback_delphi.htm
0
 
LVL 19

Assisted Solution

by:MerijnB
MerijnB earned 200 total points
ID: 20334177
my guess you'll have to make a type for the functions:
type TProcSet = procedure(state: Pointer; seed: LongInt);
     PProcSet = ^TProcSet
 
     _gsl_rng_type = packed record
       name: PChar;
       max: LongInt;
       min: LongInt;
       size: Cardinal;
       set_: PProcSet;
       ...
     end;

Open in new window

0
 
LVL 6

Author Comment

by:den4b
ID: 20334557
Can't test it yet, because I have to convert a lost more headers first, but it sound good already!

The only uncertain thing left is "set_: TProcSet" or "set_: PProcSet". I thought that "TProcSet" would already be a function pointer in Delphi, in which case I could use just "set_: procedure(state: Pointer; seed: LongInt)".

Anyway, I can check that out later...
0
 
LVL 19

Expert Comment

by:MerijnB
ID: 20334568
I'm quite sure you'll need PProcSet, TProcSet is not a pointer itself.
0
 
LVL 6

Author Comment

by:den4b
ID: 20334582
Ohh, the only thing which I forgot to mention is to use "cdecl" calling convention for the functions written in C. For example: procedure(state: Pointer; seed: LongInt); cdecl;
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question