2
votes

Is it possible to pass an array of record to dll (delphi)?

I have a record that I put in a shared (used in dll and main apps) delphi unit

TmyRecord = record
  tgl  : Double;
  notes: shortstring;
end

TarrOfMyRecord = array[1..1000] of TmyRecord

In the dll, I have a function:

function getNotes(var someRecord: TArrOfMyRecord):boolean; stdcall;
begin
  someRecord[1].tgl:= now;
  someRecord[1].notes:= 'percobaan';

  someRecord[2].tgl:= now + 1;
  someRecord[2].notes:= 'percobaan1';

  return:= true;
end;

I can't get the right values of someRecord returned by dll.

Thanks

UPDATE: This is my code in main apps:

interface

function getNotes(var someRecord: TArrOfMyRecord):boolean; stdcall; external 'some.dll'

implementation

procedure somefunction;
var myRecord: TarrOfMyRecord;
    i: integer;
begin
  if getNotes(myRecord) then
      for i:= 1 to 1000 do memo1.lines.add(myRecord[i].notes);

end;
1
Show the code that calls the dll. Also, are you aware that you current approach commits all users of the DLL to be written in Delphi? Are you happy with that? - David Heffernan
@DavidHeffernan: Yes.. I'm aware about that and that's okay. - abanas
Your function define as getNotes(var someRecord: TArrOfMyRecord) but you pass the variable myRecord: TmyRecord? Is this a Typo? - Justmade
Please show us the real code. Showing us made up code does not help. The real code compiles. This does not. Also show how you import the function. - David Heffernan
@Justmade: Ups.. sorry. I've fix it. thanks. - abanas

1 Answers

0
votes

The best way to pass huge amount of data to a DLL is using pointer.

The record definitions:

...
TarrOfMyRecord = array[1..1000] of TmyRecord
ParrOfMyRecord = ^TarrOfMyRecord;

DLL:

function getNotes(someRecord: PArrOfMyRecord):boolean; stdcall;
begin
  someRecord^[1].tgl:= now;
...

Program:

...
begin
  if getNotes(@myRecord) then
      for i:= 1 to 1000 do memo1.lines.add(myRecord[i].notes);
...