Newsgroups : Borland : borland.public.delphi.rtl.win32 : 2005 Aug : Using Delphi records in C programs

www.cryer.info
Managed Newsgroup Archive

Using Delphi records in C programs

Subject:Using Delphi records in C programs
Posted by:"Adrien Reboisson" (adrien-reboissonatastasedotcom@nospam.com)
Date:29 Aug 2005 15:14:29

Hi

I'm trying to link a Delphi lib against a C program. Indeed
the library has been done with Kylix and the program is compiled
with GCC but I think the problem concerns the linker/compiler
and is not specific to a platform.

Let's say I've a Delphi record declared like that :

type
  TFooStruct = packed record
    A: Integer;
    B: PChar;
    C: Char;
    D: Int64;
    E: Boolean;
    F: Boolean;
    G: Boolean;
    H: Cardinal;
    I: Byte;
  end;

The library just displays the values of the record sent :

function Foo(S: TFooStruct): Boolean; cdecl;
begin
  WriteLn('Valeur de A (Integer): ', S.A);
  WriteLn('Valeur de B (PChar): ', S.B);
  WriteLn('Valeur de C (Char): ', S.C);
  WriteLn('Valeur de D (Int64): ', S.D);
  WriteLn('Valeur de E (Boolean): ', BoolToStr(S.E, True));
  WriteLn('Valeur de F (Boolean): ', BoolToStr(S.F, True));
  WriteLn('Valeur de G (Boolean): ', BoolToStr(S.G, True));
  WriteLn('Valeur de H (Cardinal): ', S.H);
  WriteLn('Valeur de I (Byte): ', S.I);
  Result := True;
end;

exports
  Foo;

I translated the Kylix header file into C :

#ifndef __SOINTF__
#define __SOINTF__

struct TFooStruct {
        int    A ;
        char * B ;
        char   C ;
        long long D ;
        int    E ;
        int    F ;
        int    G ;
        unsigned int H ;
        unsigned char   I ;
} ;

int Foo(struct TFooStruct S);

#endif

...and made foo.c which use it :

#include <stdio.h>
#include <stdlib.h>

#include "sointf.h"

int main(void) {
        struct TFooStruct R;
        R.A = 42;
        R.B = "Haha";
        R.C = '*';
        R.D = 4242;
        R.E = 0;
        R.F = 1;
        R.G = 0;
        R.H = 1985;
        R.I = 42;
        Foo(R);
        return EXIT_SUCCESS ;
}

Now, I compile and run the program :

gcc -L/home/astase/testso -lProject1 -o foo foo.c
./foo
Valeur de A (Integer): 42
Valeur de B (PChar): Haha
Valeur de C (Char): *
Valeur de D (Int64): 0
Valeur de E (Boolean): True
Valeur de F (Boolean): False
Valeur de G (Boolean): False
Valeur de H (Cardinal): 0
Valeur de I (Byte): 193

After having displayed the value of the "C" field (the char), it bugs. Read values are wrong from the Int64 field to the end.

Some points I would like to clarify :

- Should I declare my struct "packed" in Delphi ? It was my first
thought after reading Windows.pas (at lot of records are declared
packed)... But it doesn't seems to work !
- Should I change the compiler settings in my Delphi project ?
I think especially to the field alignment parameter, set to 8 by
default...
- Should I introduct some special directives like #pragma pack(X)
in the C side ?... I tested pragma pack() (C side) with packed
record (Object Pascal side), but it does not work...

In a nutshell, how can this Delphi struct be used in C ??

TIA

Best Regards

Adrien

Replies:

www.cryer.info
Managed Newsgroup Archive