如果我正确理解了您的问题,那么您可以这样做,这是一种方法:
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;
procedure TMyFancyRecord.DoSomething;
begin
ShowMessageFmt( 'Field1: %d'#$D#$A'Field2: %f', [ Field1, Field2 ] );
end;
end.
最后是一个测试应用程序,文件 -> 新建 -> VCL窗体应用程序,在窗体上拖放一个按钮,将uCommon.pas包含在uses子句中,添加对外部方法的引用。
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;
免责声明:
享受吧!
David Heffernan的编辑
仅为了100%清晰,这个代码中执行的DoSomething方法是在DLL中定义的方法。在EXE中定义的DoSomething方法从未被执行。