加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – 在主机应用程序和DLL之间传递包含方法的记录

发布时间:2020-12-15 04:18:21 所属栏目:大数据 来源:网络整理
导读:是否可以(不使用运行时包或共享内存DLL)在主机应用程序和DLL模块之间传递Record类型,其中Record类型包含函数/过程(Delphi 2006及更高版本)? 让我们假设为了简单起见我们的Record类型不包含任何String字段(因为这当然需要Sharemem DLL),这里是一个例子: TMy
是否可以(不使用运行时包或共享内存DLL)在主机应用程序和DLL模块之间传递Record类型,其中Record类型包含函数/过程(Delphi 2006及更高版本)?

让我们假设为了简单起见我们的Record类型不包含任何String字段(因为这当然需要Sharemem DLL),这里是一个例子:

TMyRecord = record
  Field1: Integer;
  Field2: Double;
  function DoSomething(AValue1: Integer; AValue2: Double): Boolean;
end;

因此,简单地说明一下:我可以在主机应用程序和DLL(在任一方向)之间传递TMyRecord的“实例”,而无需使用运行时包或共享内存DLL,并从主机EXE执行DoSomething功能和DLL?

解决方法

如果我理解你的问题,那么你就可以做到,这是一种方法:

testdll.dll

library TestDll;

uses
  SysUtils,Classes,uCommon in 'uCommon.pas';

{$R *.res}

procedure TakeMyFancyRecord(AMyFancyRecord: PMyFancyRecord); stdcall;
begin
  AMyFancyRecord^.DoSomething;
end;

exports
  TakeMyFancyRecord name 'TakeMyFancyRecord';

begin
end.

uCommon.pas< - 由应用程序和dll使用,用于定义您的花哨记录的单位

unit uCommon;

interface

type
  PMyFancyRecord = ^TMyFancyRecord;
  TMyFancyRecord = record
    Field1: Integer;
    Field2: Double;
    procedure DoSomething;
  end;

implementation

uses
  Dialogs;

{ TMyFancyRecord }

procedure TMyFancyRecord.DoSomething;
begin
  ShowMessageFmt( 'Field1: %d'#$D#$A'Field2: %f',[ Field1,Field2 ] );
end;

end.

最后一个测试应用程序,文件 – >新的 – > vcl表单应用程序,在表单上放一个按钮,在uses子句中包含uCommon.pas,添加对外部方法的引用

procedure TakeMyFancyRecord(AMyFancyRecord: PMyFancyRecord); stdcall;
  external 'testdll.dll' name 'TakeMyFancyRecord';

并在按钮的点击事件中添加

procedure TForm1.Button1Click(Sender: TObject);
var
  LMyFancyRecord: TMyFancyRecord;
begin
  LMyFancyRecord.Field1 := 2012;
  LMyFancyRecord.Field2 := Pi;
  TakeMyFancyRecord( @LMyFancyRecord );
end;

免责声明:

>在D2010工作;
>在我的机器上编译!

请享用!

大卫赫弗南的编辑

为了100%清除,执行的DoSomething方法是DLL中定义的方法. EXE中定义的DoSomething方法永远不会在此代码中执行.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读