delphi 如何把一个exe做为res加入到dll中,并在运行时生成exe文件执行  
官方Delphi 学习QQ群: 682628230(三千人)
频道

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了。
推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.12280106544495 seconds