How to return a record with data from Delphi?

Hello,

I hvae a record type that is registered to PascalScript during startup and i want to return that record type (with data) from a Delphi function or procedure.

What i have tried so far (and the results):

procedure ReadData(aData: TDataRecord); // as Parameter
begin
   FillChar(aData, SizeOf(TDataRecord), #0);
   aData.IntVar := 4711;
   aData.StringVar := 'Test';
   // Results in an empty record in PascalScript...
end;

procedure ReadData(var aData: TDataRecord); // as var paramerte
begin
   FillChar(aData, SizeOf(TDataRecord), #0);
   aData.IntVar := 4711;
   aData.StringVar := 'Test';
   // Results in an access violation
end;

function ReadData: TDataRecord; // as Result
begin
   FillChar(Result, SizeOf(TDataRecord), #0);
   Result.IntVar := 4711;
   Result.StringVar := 'Test';
   // Results in an empty record in Pascalscript
end;

The procedure (or function) is called from a PascalScript but i cannot get the data that was assigned in Delphi.

What goes wrong here? The procedures or the functions are registered according to the version i have tried out and they compile without error.

Thanks in advance,
Ralf

define it as “packed” on the Delphi side. Then the middle one should work.

I have now declared the record as “packed record” in the Delphi definition but i have the same situation. When using the record as VAR parameter i run into an access violation, Using the record as normal parameter results in an empty record (the record have a lot of more fields than in the samples).

Could fields from type “string” cause such errors, maybe changing them to “string[xxx]” will help? - Will try that tomorrow…

The access violation could be many things, depends on how you call it.

What do you men with “how you call it”?

Here ist the test code (PascalScript) :

program PScript;

procedure RunTest;
var
   dr: TDataRecord;
begin
   dr.StringVar := 'TEST';
   ReadData(dr);
   WriteLn( 'StringVar = ' + dr.StringVar );
   // StringVar ist still "TEST" here!
end;

begin
  RunTest;
end.

If i run the version that does not generate an access violation then it looks like the result is not written to the record because i still find the value i assigned before the call.

Do i need to return the result in another way from Delphi to Pascalscript? (rather than simply assign the value)

Well readdata needs to be defined as “var” on the Delphi side too.

I know. And it is.

Meanwhile i think the cause of the errors is not the method that returns the record. It is the record itself.

The record contains several strings, some byte-Variables and some char-variables. If i comment out the char variables the returning of the values is working as expected., Only when the record contains char-variables i get those access violations.

Can you make a guess why char variables in the record cause an access violation?

Thanks in advance,
Ralf

They shouldn’t. Char is a very basic type.

But they do! I have just changed all char variables into strings and the access violation does not longer happen. Changing one of them back to char results in the access violation again.
[EDIT]: When i change the char-fields to WideChar there is no access violation either. I think i will work with WideChar for the moment…