【问题标题】:Passing array of record to delphi dll将记录数组传递给delphi dll
【发布时间】:2012-03-14 07:44:00
【问题描述】:

是否可以将记录数组传递给 dll (delphi)?

我有一条记录放在一个共享(用于 dll 和主要应用程序)delphi 单元中

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

TarrOfMyRecord = array[1..1000] of TmyRecord

在dll中,我有一个函数:

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;

我无法获得 dll 返回的 someRecord 的正确值。

谢谢

更新: 这是我在主要应用中的代码:

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;

【问题讨论】:

  • 显示调用 dll 的代码。另外,您是否知道您当前的方法将 DLL 的所有用户都提交到用 Delphi 编写?你对此满意吗?
  • @DavidHeffernan:是的。我知道这一点,没关系。
  • 您的函数定义为 getNotes(var someRecord: TArrOfMyRecord) 但您传递了变量 myRecord: TmyRecord?这是错字吗?
  • 请给我们看真实的代码。向我们展示编造的代码并没有帮助。真正的代码编译。这没有。同时展示你如何导入函数。
  • @Justmade: Ups..对不起。我已经修好了。谢谢。

标签: arrays delphi dll record


【解决方案1】:

将大量数据传递给 DLL 的最佳方法是使用指针。

记录定义:

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

DLL:

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

程序:

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

【讨论】:

  • 这样我可以只使用一个Delphi应用程序还是用其他语言调用DLL?
  • @Martin 这个方法可以在任何编程语言编写的任何程序中使用。一些 Windows API 函数也使用这种方法。
  • 如果我尝试使用您的代码,我会在“if getNotes(@myRecord)”上收到错误,因为“函数必须具有某些参数”...
  • @Martin 您是否在程序界面中定义了 DLL 函数?函数 getNotes(someRecord: PArrOfMyRecord):boolean;标准调用;外部'dllfilename.dll';
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多