Delphi - DLL

 
 
-- 注册ActiveX控件
const CN_EXECUTE_STR = ' Regsvr32.exe /s %s '; var StrPath: string; begin StrPath := ' C:\aa.dll '; WinExec(PChar(Format(CN_EXECUTE_STR, [StrPath])), SW_NORMAL); end;
----------------------------------------------------------
-调用DLLform
   
   
library Project1; uses SysUtils, Classes,Forms,windows,dialogs, Unit1 in ' Unit1.pas ' { Form1 }; { $R *.res } function showform(formname: string):boolean; stdcall; var TheClass: TPersistentClass; aForm: TForm; begin result:=false; { 如果您的Dll中有很多FORM,请在这儿注册哦 RegisterClasses([TForm1,TForm2,TForm3,...]); } RegisterClasses([TForm1]); TheClass := GetClass( ' T ' + FormName); if (TheClass = nil) then exit; if TheClass.InheritsFrom(TForm) then begin aForm := Tform(TheClass.Create).Create( nil); try aForm.ShowModal; result:=true; finally FreeAndNil(aForm); end; end; end; exports showform; begin end.       .... procedure RunDllForm( const DllFileName,DllFormName:String; const methodName: string); type TRunForm= function(formname: string):boolean; stdcall; var RunForm: TRunForm; GetDllHWND: HWND; begin GetDllHWND := LoadLibrary(PChar(DllFileName)); try if GetDllHWND < 32 then begin MessageBox( 0, Pchar( ' 没有找到 '+DllFileName+ ' DLL文件! '), ' 加载DLL失败 ', MB_OK); Exit; end; @RunForm := GetProcAddress(GetDllHWND,pchar(methodName)); if @RunForm <> nil then try RunForm(DllFormName); except raise Exception.Create( ' 对不起,找不到T ' + DllFormName+ ' 窗体! '); end else raise Exception.Create( ' 无效的方法名调用 '); finally FreeLibrary(GetDllHWND); end; end; procedure TForm1.Button1Click(Sender: TObject); begin RunDllForm( ' project1.dll ', ' form1 ', ' showform '); end;
========================================================
第一步: 建一个 DLL 工程, 如图: 然后保存, 我这里使用的名称都是默认的.
第二步: 建一个资源原文件, 如图: 编辑内容如下(路径中的文件一定要存在): img1 BITMAP "c:\temp\test.bmp" 然后, 取个名(后缀须是 rc, 我这里取名为 Res.rc), 保存在工程目录下.
第三步: 在 DLL 工程中添加这个资源原文件, 如图: 此时, 工程源文件中会添加一句: {$R 'Res.res' 'Res.rc'}, 我们需要的源文件这样即可:
library Project1;{$R 'Res.res' 'Res.rc'}beginend.
然后 Ctrl+F9 编译; 此时在工程目录下会生成我们需要的 Project1.dll.
第四步: 把刚才的 Project1.dll 文件复制到 c:\temp 下. 这样过会我们可以通过 c:\temp\Project1.dll 找到这个文件. 至此 DLL 制作完毕.
第五步: 重新建一个 VCL Forms Application 工程, 代码如下:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    procedure FormPaint(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormPaint(Sender: TObject);
var
  h: THandle;
  bit: TBitmap;
begin
  h := LoadLibrary('c:\temp\Project1.dll'); {载入 DLL}

  bit := TBitmap.Create;
  bit.LoadFromResourceName(h, 'img1');      {提取资源}

  Canvas.Draw(10, 10, bit);                 {在窗体上绘制图片}

  FreeLibrary(h);                           {载卸 DLL}
  bit.Free;
end;

end.

      
      

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Delphi是一种编程语言,而DLL(Dynamic-Link Library)是一种模块化的文件格式,用于存储代码和数据,可以被多个应用程序共享。DLL注入是一种技术,它允许将DLL文件加载到正在运行的进程中,并使得该进程能够调用DLL中的函数和使用其中的数据。 在Delphi中实现DLL注入的方法有很多种。一种常见的方法是使用Windows API函数LoadLibrary和GetProcAddress。通过调用LoadLibrary函数,将DLL文件加载到进程的虚拟地址空间中。然后使用GetProcAddress函数获取DLL中导出函数的地址,并将其传递给需要调用的函数。通过这种方式,可以在运行时将DLL注入到目标进程中,并且通过调用DLL中的函数来扩展进程的功能。 DLL注入在实际应用中有多种用途。例如,可以使用DLL注入来为某个程序添加额外的功能或修改程序的行为。DLL注入还可以用于实现一些调试和监控的功能。通过注入DLL,可以截获程序的输入和输出,或者在程序执行某些指定的操作时进行额外的处理。 在Delphi中实现DLL注入需要一定的编程知识和技巧。需要考虑目标进程的架构和权限限制,以及如何管理注入的DLL的生命周期和资源管理。同时,还需要处理一些安全性和稳定性方面的问题,以确保注入过程不会对目标进程造成损害或崩溃。 总之,Delphi可以通过调用Windows API函数来实现DLL注入,从而扩展和修改进程的功能。但在实际应用中,需要考虑各种方面的问题,并且遵守相关的法律和规定,以确保注入操作的安全性和合法性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值