Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C to Delphi: typedef struct ...

Posted on 2007-11-22
8
Medium Priority
?
1,954 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 11

Accepted Solution

by:
Deepu Abraham earned 900 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:Deepu Abraham
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 600 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them 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…
Suggested Courses

721 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