【问题标题】:Load a DLL From a Resource Directly From Memory in Delphi 7在 Delphi 7 中直接从内存中加载资源中的 DLL
【发布时间】:2025-11-22 16:00:01
【问题描述】:

我将一个 dll 文件放入资源文件 (*.res)

ProjectTestLibrary.dll

library ProjectTestLibrary;

uses SysUtils, Classes, Dialogs;

{$R *.res}

procedure DllMessage; export;
begin
  ShowMessage('Hello world from a Delphi DLL');
end;

exports DllMessage;

begin
end.

MyTestDLL.rc

TestDLL RCDATA ProjectTestLibrary.dll

通过此命令行生成 MyTestDLL.res

BRCC32 ProjectTestLibrary.rc

在主窗体中

implementation
{$R *.dfm}
{$R MyTestDLL.RES}

procedure DllMessage;  stdcall; external 'ProjectTestLibrary.dll';

procedure TForm1.Button1Click(Sender: TObject);
begin
    DllMessage;
end;

然后出现错误
此应用程序无法启动,因为找不到 ProjectTestLibrary.dll。

我明白这个问题是因为 dll 文件还不存在。
所以我删除了'DLLMessage;' onButton1Click 中的代码。
然后onFormCreate,我加了:

procedure TForm1.FormCreate(Sender: TObject);
var ms : TMemoryStream;
    rs : TResourceStream;
begin
  if 0 <> FindResource(hInstance, 'TestDLL', RT_RCDATA) then
  begin
    rs := TResourceStream.Create(hInstance, 'TestDLL', RT_RCDATA);
    ms := TMemoryStream.Create;
    ShowMessage('Found');
  end else
  begin
    ShowMessage('Not Found');
  end;
end;

我再次运行它然后弹出消息说'找到'

然后我的问题:
1.如何将其保存在内存中(而不是PC硬盘),最后
2.使用它的程序/函数(procedure DLLMessage)

【问题讨论】:

  • 操作系统不支持。这样做需要像 BTMemoryModule 这样的东西,它可能在未来的 Windows 版本中失败。它依赖于实现细节。反恶意软件软件可能会将此活动视为可疑活动。将 DLL 与 exe 一起发布总是更好。

标签: delphi dll resources delphi-7


【解决方案1】:

虽然存在模拟 Windows PE 加载器的技术,并允许您直接从内存缓冲区加载 DLL,而无需将其存储在磁盘上,但没有官方方法可以做到这一点。

这是一个 Delphi 实现,例如,一个 DLL 内存加载器:https://github.com/DSPlayer/memorymodule

【讨论】: