函数 EXE 到 DLL (Delphi)

function EXE to DLL (Delphi)

我正在调整我的应用程序以使用单独的模块(插件)。

我已经成功地让我的 EXE 应用程序读取和加载插件,包括表单。

现在我需要执行相反的操作,将函数从可执行文件导出到 DLL。

示例: 在我的可执行文件中,它有一个 TMemo 组件。我想创建一个这样的函数

function GetMemo(): widestring;

在我看来,无论是谁编写了DLL插件,在调用函数GetMemo()时,都已经将DLL中的TMemo内容取出来了。

有可能吗?

处理这个问题的最简单方法是定义函数指针记录,然后让 EXE 在初始化时将记录的实例传递给每个插件。然后 EXE 可以根据需要实现这些功能并将它们传递给插件,而无需像 DLL 那样从其 PE 导出 table 实际导出它们。

例如:

type
  PPluginExeFunctions = ^PluginExeFunctions;
  PluginExeFunctions = record
    GetMemo: function: WideString; stdcall;
   ...
  end;

function MyGetMemoFunc: WideString; stdcall;
begin
  Result := Form1.Memo1.Text;
end;

...

var
  ExeFuncs: PluginExeFunctions;
  hPlugin: THandle;
  InitFunc: procedure(ExeFuncs: PPluginExeFunctions); stdcall;
begin
  ExeFuncs.GetMemo := @MyGetMemoFunc;
  ...
  hPlugin := LoadLibrary('plugin.dll');
  @InitFunc := GetProcAddress(hPlugin, 'InitializePlugin');
  InitFunc(@ExeFuncs);
  ...
end;

var
  ExeFuncs: PluginExeFunctions;

procedure InitializePlugin(pExeFuncs: PPluginExeFunctions); stdcall;
begin
  ExeFuncs := pExeFuncs^;
end;

procedure DoSomething;
var
  S: WideString;
begin
  S := ExeFuncs.GetMemo();
  ...
end;
unit uDoingExport;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

type
  TForm1 = class(TForm)
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

procedure testproc; stdcall;

implementation

{$R *.dfm}

procedure testproc;
begin
   ShowMessage('testproc');
End;

exports
   testproc;

end.

我只是在单元的界面中添加了我想从我的 EXE 中发布的方法,并在实现中添加了导出(方法名称)。我使用的是 stdcall 而不是 cdecl。

在我的 child 中,我可以加载 exe 文件...或者你可以像 Apache 那样疯狂一点,在之前的代码中,添加一个加载库,它加载一个 DLL,实习生可以加载调用者的库。

我的意思是要表明,您的 EXE 就像一个 DLL(只是一个不同的二进制头文件),反之亦然。只是拍出口。证明它有效,我 运行 tdump 针对 EXE:

Exports from ProjDoingExport.exe
  1 exported name(s), 1 export addresse(s).  Ordinal base is 1.
  Sorted by Name:
    RVA      Ord. Hint Name
    -------- ---- ---- ----
    0005294C    1 0000 testproc
  • 我知道,回答晚了,但是,一个很好的问题!