在Delphi 7中直接从内存中加载来自资源的DLL

时间:2017-01-25 05:21:09

标签: delphi dll resources delphi-7

我将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;

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

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

1 个答案:

答案 0 :(得分:0)

虽然存在模拟Windows PE加载程序的技术,但您可以直接从内存缓冲区加载DLL,而无需将其存储在磁盘上。

没有正式的方法。

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

相关问题