delphi 如何把一个exe做为res加入到dll中,并在运行时生成exe文件执行


本文整理自网络,侵删。

 第一:准备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了。

相关阅读 >>

Delphi tstreamwriter tstreamreader 流操作3

Delphi trimleft 删除字符串左边的空格

Delphi 加载大文件显示进度条

Delphi 用拼音首字符检索汉字的源代码

Delphi 播放声音 采用 异步方式,比较流畅

Delphi xe 安卓 memo设置字体颜色

Delphi 捕捉全局异常错误的方法

Delphi中动态加载image控件图片的方法

Delphi 中主设置静音的方式

Delphi repeat until 运用

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...