求教在delphi中,如何把一个exe做为res加入到dll中,并在运行时生成exe文件执行
要具体的操作,详细的,完整,可以调试成功的程序。我是一个机械模具专业的delphi初学者。谢谢大家。...
要具体的操作,详细的,完整,可以调试成功的程序。
我是一个机械模具专业的delphi初学者。
谢谢大家。 展开
我是一个机械模具专业的delphi初学者。
谢谢大家。 展开
2个回答
展开全部
第一:准备exe程序 a.exe
第二:准备资源:
建立一个资源文件myRes.rc,内容如下:
MyExe EXE a.exe
第三:编译资源
进入DOS,运行 brcc32 myRes.rc;
这时编译出资源:myRes.RES
第四:
创建dll文件,代码如下:
library Project1;
uses
SysUtils,
Classes,dialogs,
common in 'common.pas';
{$R *.res}
{$R myRes.RES}
exports
loadDll;
begin
releaseExe();
end.
unit common;
interface
uses
Classes,Windows,SysUtils,dialogs;
function FileResourceToLocalFile(Instance:Cardinal;ResName,ResType,sFileToSaved:String):boolean;
procedure releaseExe();
procedure loadDll();
implementation
function FileResourceToLocalFile(Instance:Cardinal;ResName,ResType,sFileToSaved:String):boolean;
Var
FileStream:TFileStream;
ResourceStream:TResourceStream;
Begin
result:=true;
Try
Try
ResourceStream:=TResourceStream.Create(Instance,ResName,Pchar(ResType));
ResourceStream.SaveToFile(sFileToSaved);
Except
On E:Exception Do
Begin
result:=false;
showMessage('生成本地资源文件失败:'+#13+#10+E.Message);
Exit;
end;
end;
Finally
ResourceStream.Free;
End;
End;
procedure releaseExe();
begin
if common.FileResourceToLocalFile(hInstance,'MyExe','EXE','target.exe') then
showMessage('exe程序已经释放:target.exe');
end;
procedure loadDll();
begin
showMessage('dll已经被调用');
end;
end.
第五:新建另一个工程,调用dll:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
procedure loadDll();external 'project1.dll';
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
loadDll();
end;
end.
ok了。
第二:准备资源:
建立一个资源文件myRes.rc,内容如下:
MyExe EXE a.exe
第三:编译资源
进入DOS,运行 brcc32 myRes.rc;
这时编译出资源:myRes.RES
第四:
创建dll文件,代码如下:
library Project1;
uses
SysUtils,
Classes,dialogs,
common in 'common.pas';
{$R *.res}
{$R myRes.RES}
exports
loadDll;
begin
releaseExe();
end.
unit common;
interface
uses
Classes,Windows,SysUtils,dialogs;
function FileResourceToLocalFile(Instance:Cardinal;ResName,ResType,sFileToSaved:String):boolean;
procedure releaseExe();
procedure loadDll();
implementation
function FileResourceToLocalFile(Instance:Cardinal;ResName,ResType,sFileToSaved:String):boolean;
Var
FileStream:TFileStream;
ResourceStream:TResourceStream;
Begin
result:=true;
Try
Try
ResourceStream:=TResourceStream.Create(Instance,ResName,Pchar(ResType));
ResourceStream.SaveToFile(sFileToSaved);
Except
On E:Exception Do
Begin
result:=false;
showMessage('生成本地资源文件失败:'+#13+#10+E.Message);
Exit;
end;
end;
Finally
ResourceStream.Free;
End;
End;
procedure releaseExe();
begin
if common.FileResourceToLocalFile(hInstance,'MyExe','EXE','target.exe') then
showMessage('exe程序已经释放:target.exe');
end;
procedure loadDll();
begin
showMessage('dll已经被调用');
end;
end.
第五:新建另一个工程,调用dll:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
procedure loadDll();external 'project1.dll';
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
loadDll();
end;
end.
ok了。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询