Solved

Including DLL in the EXE

Posted on 2001-09-02
9
299 Views
Last Modified: 2013-11-18
Hi!

Is it possible to include a DLL in to the EXE so that it extrakt it on run time? so it wont be missing if I just seend the exe to a friend


thanks
0
Comment
Question by:SULTAN
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 3

Expert Comment

by:tongalite
ID: 6449213
Hi,
I believe you might be able to do what you want by converting the DLL into a Resource file and adding the compiler directive into your unit - example -
{$R MyDll.Res}
T.
0
 
LVL 2

Expert Comment

by:alsantos
ID: 6449216
Yes... try this:

go to ms-dos prompt:
type:
edit blah.rc
{ on blah.rc type : }
nameofdll dll "nameofdll.dll"

------------------------

type on ms-dos prompt:

brcc32 blah.rc
{ it'll create the file nameofdll.res }

-------------------------
{ put the nameofdll.res on the same dir of your project }
On your delphi project source put this:

{$R nameofdll.res}

and put this source code to extract and save the dll where you want:

var
X: Tresourcestream;
begin;
X:=tresourcestream.create(Hinstance, 'nameofdll', Pchar('dll'));
x.savetofile('c:\windows\system\nameofdll.dll');
x.free;
end;

If I understood your question, I think this code works fine.

Alexandre Santos
0
 
LVL 5

Expert Comment

by:Gwena
ID: 6449331
Hi :-)

  Putting it into a res file and extracting it will work fine...you could also just stick it onto the end of the exe and read the exe file into a stream at runtime.. truncate the stream to contain only the dll and use stream.savetofile to put the dll onto disk
Or... use PEBUNDLE to tack the dll onto the end of your exe file and use the dll without putting it onto disk... you can find PEBUNDLE at

http://www.collakesoftware.com/
0
 
LVL 3

Expert Comment

by:tongalite
ID: 6456404
Hi again
(for anybody interested and/or doesn't know this:)

Heres a neat way to create Resource files without the need to mess about with opening a DOS prompt window.

Once this is set up, making RES files is eeeeaaaasy!

Create a folder on your desktop... (MyResources)
Open your Delphi bin folder and copy these two files:
Brcc32
Rw32core.dll.
Paste them into your newly created folder.
Next create your *.rc file in notepad (or favourite editor)
e.g.
Test RCDATA "MyDLL.DLL"  {'Test' is the name you will use to invoke the RESOURCE} Save the file as Test.rc
Now make sure the file MyDLL.DLL (or whatever you want to convert to a RES file BMP, JPEG, WAV, MID etc) is in the new Folder and then simply drag and drop the rc file onto the Brcc32 exe icon and Voila! the RES file is created!
For subsequent RES files just create the .rc file add the data file and drop the rc onto the Brcc32 icon... simple.
You will of course need to export the new RES file into your Project directory and add the compiler directive into your project below {$R *.DFM} thus:

{$R *.DFM}
{$R TEST.RES}

NOTE: multiple files can be added to the RESOURCE file. i.e.
create **.rc file...
Pic RCDATA "MyPic.bmp"
Sound RCDATA "MySound.Wav"
Tune RCDATA "MyTune.Mid"
Prog RCDATA "MyApp.exe"
Save as 'MyRES.rc' (without quotes)
Have the neccessary above files in the folder and drop the MyRES.rc file onto the Brcc32 icon. You will get a MyRES.RES file... export to your Project folder... ad compiler directive into unit {$R MyRES.RES}To call up the resource you want use the first word in the **.rc file Pic, Sound, Tune, Prog etc.
**************************************
Okay, that's all :-)
Have fun
T.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 5

Accepted Solution

by:
Gwena earned 30 total points
ID: 6459198
If you want you could use my ExeMod.pas unit and easily add a dll to the end of your exe... ejecting it to disk at runtime is very easy... here is the source code of a program that would put a dll called mydll.dll onto disk when it runs...


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs, ExeMod, Buttons;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
  Extract2File('testdll','MyDll.dll');
//substitute the name of your dll for 'MyDll.dll'
//'testdll' is just a unique marker string
end;

end.


All you need to do is add ExeMod to the uses clause and put the line Extract2File('testdll','MyDll.dll'); in the forms oncreate event.... very simple and it works well :-)

You add the dll by using a util program that tacks it on...
the util program is also very simple... here is an example


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Buttons, ExeMod, StdCtrls;

type
  TForm1 = class(TForm)
    OpenDialog1: TOpenDialog;
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    Edit1: TEdit;
    Label1: TLabel;
    procedure SpeedButton1Click(Sender: TObject);
    procedure SpeedButton2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  FileName: String;

implementation

{$R *.DFM}

procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
  Opendialog1.execute;
  FileName := Opendialog1.filename;
end;

procedure TForm1.SpeedButton2Click(Sender: TObject);
begin
If Filename <> '' then
begin
  OpenDialog1.Execute;
  If Opendialog1.FileName <> '' then TackOnFile(Edit1.Text,FileName,OpenDialog1.FileName);
end;
end;

end.


I will put ExeMod.pas and these 2 demos on my site...
I will put full source and the compiled exe's for you to try... demo1.zip has the source and an exe for the util that adds the dll to your exe
demo2 is a small exe that extracts a dll to the current dir..I put a small file into the compiled exe...it will extract it when you run it.... to try it with your dll just recompile the program and insert the correct dll name...then use the util to add your dll to the exe...
once you get the hang of it it takes about a minute to do all this :-)

http://delphi.does.it
0
 
LVL 3

Expert Comment

by:tongalite
ID: 6460347
Nice one Gwena!!!!

I'll also go on a raiding mission to your site... should be interesting :-)
Tongalite.
0
 

Author Comment

by:SULTAN
ID: 6474947
Ive got them now...just gona play with them a litle and then all be back to grad it :O)
0
 

Author Comment

by:SULTAN
ID: 6475377
THANK YOU !!!

Exakly what I wanted

SULTAN
0
 
LVL 5

Expert Comment

by:Gwena
ID: 6476438
Hi Sultan :-)

Thanks... and be sure to get the latest version of ExeMod at my site... I fixed a bug a couple of days ago that made the code fail under NT...
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now