Solved

C to Delphi: typedef struct ...

Posted on 2007-11-22
8
1,953 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:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

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 the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses
Course of the Month9 days, 5 hours left to enroll

615 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