Discussion:
Converting "typedef struct" C++ structure to Delphi
(too old to reply)
Andrew
2005-05-10 14:27:39 UTC
Permalink
Hi there,

I'm attempting to port some some rather small C++ header file to
Delphi. I know that simple structures are equivalent to Delphi record
structures but this one has me confused. Here it is

typedef struct {
long (*SendMessage)(const char *to, const char *from, const char
*subject, long data1, long data2);
...more stuff
} kmeleonFunctions;

Now my immediate guess is that this is equivalent of a class rather
than a record and typedef creates an alias for that class.
I assume the "long..." line is a function declaration like any other,
but this function isn't defined anywhere (there are no other include
files other than the bog standard Windows include).

If it makes a difference these header files are part of a DLL. Any
guidance would be appreciated, i've only just started looking at C++
today although I think i've grasped the fundamentals of the syntax :-P
Marco van de Voort
2005-05-10 14:36:38 UTC
Permalink
Post by Andrew
Hi there,
I'm attempting to port some some rather small C++ header file to
Delphi. I know that simple structures are equivalent to Delphi record
structures but this one has me confused. Here it is
typedef struct {
long (*SendMessage)(const char *to, const char *from, const char
*subject, long data1, long data2);
...more stuff
} kmeleonFunctions;
Type clong = cardinal; // probably

{$packrecords C}

type sendmessagetype = function (to : pchar; from : pchar; subject : pchar; data1,data2:clong):clong; cdecl;

kmeleonfunction = record
SendMessage : SendMessageType;
end;
Post by Andrew
Now my immediate guess is that this is equivalent of a class rather
than a record and typedef creates an alias for that class.
No. It is like objects without actually using them, but it is not equivalent
to a real class, since then it would have self as first parameter.
Post by Andrew
I assume the "long..." line is a function declaration like any other,
but this function isn't defined anywhere (there are no other include
files other than the bog standard Windows include).
Look up function pointers/variables. The concept is analogous in both Pascal
and C, except that C requires emulates it using a pointer (the * in front of
sendmessage) but under Pascal, while in Borland dialects internally a
pointer, procedure variables are a type in their own right. (no * or ^)
Andrew
2005-05-10 15:01:58 UTC
Permalink
I see so essentially the equivalent is a record of pointers to various
functions? I use pointers to functions myself but I just never thought
of that :-/.

Just one thing, are packed records necessary for compatibility?

Thanks for the quick response.
Marco van de Voort
2005-05-10 19:14:03 UTC
Permalink
Post by Andrew
I see so essentially the equivalent is a record of pointers to various
functions? I use pointers to functions myself but I just never thought
of that :-/.
Just one thing, are packed records necessary for compatibility?
Record packing is one of those things you always have to watch. They differ
between all kinds of compilers (so also between two compilers for the same
language for the same platform).

I just added the directive that FPC uses to maintain compability with corresponidng C
compiler (gcc). I don't know how delphi does this.
Andrew
2005-05-17 12:25:45 UTC
Permalink
Congratulations on the release of FPC 2.0.0 :-) and a big thanks for
taking the time to frequent usenet and help out, with helpful
developers like you the project can't go wrong.

Loading...