例如,现在有一个现在的用C语言写成的可执行文件,如DOS下的ping.exe,他可以接收到一个参数的,如ping 127.0.0.1 ,能不能把这个ping.exe集成在一个dll文件中,并做成ping(ip)函数,然后通过调用这个dll中的ping(ip) 函数,来实现ping功能呢?我只是举个例子说明我的意图,我并不是想做ping 这个功能的,希望哪位高手能给出个例子参考一下。
---------------------------------------
做为资源编译到DLL里,调用函数的时候释放出来。
--------------------------------------
一、编写:
library PingDLL;
uses
Forms,
Unit1 in 'Unit1.pas' {Form1};
{$R *.res}
exports
DoPing;
begin
end.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
Procedure DoPing; cdecl;
implementation
{$R *.dfm}
Procedure DoPing;
begin
Application.CreateForm(TForm1, Form1);
Form1.ShowModal;
Form1.Free;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
WinExec(Pchar('Ping.exe '+Edit1.text),9);
end;
end.
二、调用:
unit Unit_TestDLL;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TTestDLL = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
TestDLL: TTestDLL;
implementation
{$R *.dfm}
procedure DoPing;stdcall;external 'PingDll.dll';
procedure TTestDLL.Button1Click(Sender: TObject);
begin
DoPing;
end;
end.
-------------------------------------------
dll和exe有很多相似之处,你甚至可以干脆先把dll想象成exe。那么你的问题就变成了怎么在一个exe文件里调用另外一个exe文件。说来说去,就变成winexec api了。
至于打包不打包只不过是为了保证客户机器上也有那个要运行的exe罢了。
不释放出来怎么运行呢?完全的合二为一吗?那问题可能就变成破解重写那个软件了。
--------------------------------------------
dll和exe是一个东西,exe合并的办法同样适用于dll
一个简单的办法是用文件流或copy命令,在dll文件后面追加另一个exe,
执行的时候读取另一个exe的字节流,然后释放到一个临时文件夹下,
再winexec/shellexecute/createprocess运行这个临时exe
要想不释放临时exe的话,得先研究一下PE格式,使dll在执行的时候跳转到exe文件入口地址,
很多病毒、木马就是这样做的