Solved

C to Delphi: typedef struct ...

Posted on 2007-11-22
8
1,941 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
 
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

746 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now