Link to home
Start Free TrialLog in
Avatar of sageryd
sagerydFlag for Sweden

asked on

Binary resources!! [URGENT!]

I need to make a program which can extract an exe file from within itself to a specified location. I'm making an update patch for a very non-technical client of ours and I want him to be able to run the update without complications. I need to know how to append the exefile to my program as resource, and then how to extract it.


thx!

johan
Avatar of AvonWyss
AvonWyss
Flag of Switzerland image

Just a quick workaround solution: use WinZip's self-extractor; you can set it up to ask no questions and extract all files needed to a specified dir plus execute a file afterwards. I'm sure you've seen this in action already. This will prevent your customer from doing anything wrong but still you can have multiple files. BTW even MS uses this for their hotfixes sometimes.
Avatar of edey
edey

I've used the technique below before, though this code is untested & provides no exception handling.  It was written 'on the fly':

program Project2;
{$APPTYPE CONSOLE}
uses
    SysUtils,classes;

procedure addFile;
var
   sign : array[0..3] of char;
   src,dst : TFileStream;
   fileName : string
   size : integer;
begin
     sign := 'ARCH';
     if not fileExists(paramStr(2)) then
     begin
          writeLn('Could Not Add '+paramStr(2)+' - File Does Not Exist');
          exit;
     end;
     fileName := copy(application.exeName,0,length(application.exename)-length(extractFileExt(application.exeName)));
     dst := TFileStream.create(fileName+'.new',fmOpenWrite or fmShareExclusive);
     src := TFileStream.create(application.exename,fmopenRead or fmShareDenyNone);
     dst.copyFrom(src,src.size);
     src.free;
     src := TFileStream.create(paramStr(2),fmOpenRead or fmShareDenyNone);
     dst.copyFrom(src,src.size);
     size := src.size;
     src.free;
     dst.write(size,4);
     dst.write(sign,4);
     dst.free;
end;

procedure extractFile;
var
   sign : array[0..3] of char;
   fs,dst : TFileStream;
   size : integer;
   fileName : string;
begin
     fs := TFileStream.create(application.exeName,fmOpenRead or fmShareDenyNone);
     fs.seek(-4,soFromEnd);
     fs.read(sign,4);
     if lowerCase(sign) <> 'ARCH' then
     begin
          writeLn('Invalid or Empty Archive - Missing Signature');
          exit;
     end;
     fs.seek(-8,soFromEnd);
     fs.read(size,4);
     dst := TFileStream.create(paramsStr(2),fmOpenWrite or fmShareExclusive);
     fs.seek(-8-size,soFromEnd);
     dst.copyFrom(fs,size);
     dst.free;
     fileName := copy(application.exeName,0,length(application.exename)-length(extractFileExt(application.exeName)));
     dst := TFileStream.create(fileName+'.new',fmOpenWrite or fmShareExclusive);
     fs.seek(0,soFromBeginning);
     dst.copyFrom(fs,fs.size-8-size);
     dst.free;
     fs.free;
end;

begin
     if paramCount <= 0 then
        exit;
     case lowerCase(paramStr(1))[2] of
          'a' : addFile;
          'e' : extractFile;
     else
     begin
          writeLn('Usage: '+application.exeName+' [/A | /E] [SOURCE | DESTINATION]');
          writeln('/A Adds SOURCE to the Archive');
          writeln('/E Extracts Last Archived File to DESTINATION');
     end;
end.


It'll (or should anyway) be capable of adding to & extracting from itself multiple external files.  You just need to rename the resulting archive from *.new to *.exe .

GL
Mike
I have a little tool that does exactly this. You specify the files you want to include then it makes a .Pas file which you include. Then in your program you just use :

Uses
  IncludeEXE;

IncludeEXE.DumpFile(0,'c:\winnt\system32\R2CMXL.exe');


you can have as many files in there as you want and you can call them what you want when you dump them.
if you need it then email me @

DannyKellett@Hotmail.com

Regards
Smurff
Hi

Iff You have the "Update-EXE" compiled as resource into Your Setup EXE You can use this:

with TResourceStream.Create(HInstance, 'EXE', RT_RCDATA') do
try
  SaveToFile('App.exe');
finally
  Free;
end;

The Update-EXE is named as "EXE" and linked in as RT_RCDATA.

Hagen
You might want to look at the TBackupFile component and the Self extracting EXE - programming example at:
http://www.ec-software.com/comppage.htm
http://www.ec-software.com/delphi/backfile.zip
http://www.ec-software.com/delphi/sfx.zip 
HEY!!

 I wrote a freeware thingie that creates stand-alone file extractors...
I think it may be just what you want... I can send it to you if you want it.

All you do is run it and type the file name you wish it to save to in an edit box... like so  C:\anew.exe

then an openfile dialog opens up and you choose the file you want to be self extracted
then it creates a new exe file named extractor_new.exe in the same dir that extractor is
being run from... this new exe has just a single button 'Extract File'  and when pressed it
puts the file into the dir on the users machine.....

let me know where to email this program if you want it :-)
Hi smurff :-)
  your little tool sounds interesting.. would you send it to me so I can look it over :-)
gacarpenter386@yahoo.com
Avatar of sageryd

ASKER

Sure does sound interesting! Mail me immediately!! =) Edey, haven't tried your solution yet, haven't really had the time, but I will. Eventually.

And Gwena, I don't think your app would be of much help as I also need to change some other settings, e.g. edit the registry, remove a bunch of files, etc. I also need to be able to search the harddrive to find a certain file before extracting, and I suppose you app can't handle all this.


//
johan@ws.se
Hi sageryd :-)

I will post the source here for a complete program that automates the creation
of a self extractor for any file type... you (or someone else) may find it to be of
some small use... it is a very simple and small program... seems to work well.
Please pardon my crummy formatting and code.. I wrote it in a hurry :-)



unit Unit1;

interface

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

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

var
  Form1: TForm1;
  ExeString: String;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
Var
ExeStream: TFileStream;
begin
  edit1.text := 'c:\default.exe';
  ExeStream:=TFileStream.Create(Application.ExeName,fmOpenRead or fmShareDenyNone);
  Try
    SetLength(ExeString, ExeStream.Size);
    ExeStream.ReadBuffer(Pointer(ExeString)^, ExeStream.Size);
  Finally
  ExeStream.Free;
end;
if exestring[3] = 'P' then speedbutton1.caption := 'Create Extractor';
if ExeString[3] <> 'P' then
begin
edit1.visible := false;
speedbutton1.caption := 'Extract File';
end;
end;


procedure TForm1.SpeedButton1Click(Sender: TObject);
var
MyStream: TMemoryStream;
MyFile,newname: string;
A,B: Integer;
begin
if ExeString[3] = 'P' then
  begin
    if edit1.text = '' then
    begin
    showmessage('Please enter a saveto filename and location into editbox   i.e. C:\new.exe');
    exit;
    end;
    opendialog1.execute;
    MyStream := TMemoryStream.Create;
    try
    MyStream.LoadFromFile(opendialog1.filename);
    SetLength(MyFile, MyStream.Size);
    MyStream.ReadBuffer(Pointer(MyFile)^, MyStream.Size);
    ExeString[3] := 'A';
    ExeString := ExeString + uppercase('startofmyfile') + MyFile + uppercase('endofmyfile') +
          uppercase('startofname') + edit1.text + uppercase('endofname');
    MyStream.Clear;
    MyStream.WriteBuffer(Pointer(ExeString)^, Length(ExeString));
    newname := application.exename;
    Insert('_new',newname,length(application.exename)-3);
    MyStream.savetofile(newname);
    finally
    MyStream.Free;
    exestring[3] := 'P';
    showmessage('New Extractor has been created as Extractor_new.exe');
    end;
  end;
 if exestring[3] <> 'P' then
 begin
 if Pos(uppercase('startofmyfile'),ExeString) > 0 then
  begin
    A := Pos(uppercase('startofmyfile'),ExeString)+13;
    B := Pos(uppercase('endofmyfile'),ExeString);
    MyFile := Copy(ExeString,A,B-A);
 end;

if Pos(uppercase('startofname'),ExeString) > 0 then
  begin
    A := Pos(uppercase('startofname'),ExeString)+11;
    B := Pos(uppercase('endofname'),ExeString);
    newname := Copy(ExeString,A,B-A);
  end;
 MyStream := TMemoryStream.Create;
 try
 MyStream.WriteBuffer(Pointer(myfile)^, Length(myfile));
 mystream.savetofile(newname);
 finally
 mystream.free;
 showmessage('File Has been extracted as '+ newname);
 application.terminate;
 end;

end;
end;
end.

ASKER CERTIFIED SOLUTION
Avatar of smurff
smurff

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi smurff :-)
  Thanks for emailing the file... it is a very clever snippet of code!
No probs mate, as you can see its not doing anything special but its really handy to have. Im thinking of re-writing it because its so slow. I think the coder didnt use BlockRead / write for the file. Infact, yes Im gonna do it over the weekend. :)

Regards
Smurff
Hi again smurff

Great idea to rewrite the utility... I think it's best to use a stream to read the file in all at once
..seems faster and easier... and when the utility writes the unit I think that the stream code
should be placed inside a try...finally block to assure that no matter what the stream will
always be freed

var
  t: TFileStream;

Begin
  t:=TFileStream.Create(Target,fmCreate);
  case Index of
  0: t.Write(BDump0,6);
  end;
  t.Free;
end;

maybe redo as

var
  t: TFileStream;

Begin
  t:=TFileStream.Create(Target,fmCreate);]
  try
    case Index of
    0: t.Write(BDump0,6);
    end;
  finally
    t.Free;
  end;
end;


Please let me know if you recode this..I want a copy of your new version :-)
Hi all :-)

  I compiled a small demo of my program generator that creates a
stand alone file extractor. I put it on my web site http://www.geocities.com/gacarpenter386/
it is a nice small example of how to use delphi to easily generate new customized
exe files and extractors......I also put the source code at http://www.geocities.com/gacarpenter386/source1.htm

<G>wen
ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101 or Netminder will return to finalize these if still open in seven days.  Please post closing recommendations before that time.

Question(s) below appears to have been abandoned. Your options are:
 
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> You cannot delete a question with comments, special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
https://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click the Help Desk link on the left for Member Guidelines, Member Agreement and the Question/Answer process for further information, if needed.  https://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Click you Member Profile to view your question history and keep them all current with updates as the collaboration effort continues, to track all your open and locked questions at this site.  If you are an EE Pro user, use the Power Search option to find them.  Anytime you have questions which are LOCKED with a Proposed Answer but does not serve your needs, please reject it and add comments as to why.  In addition, when you do grade the question, if the grade is less than an A, please add a comment as to why.  This helps all involved, as well as future persons who may access this item in the future to seek help.

To view your open questions, please click the following link(s) and keep them all current with updates.
https://www.experts-exchange.com/questions/Q.11756739.html
https://www.experts-exchange.com/questions/Q.20012038.html
https://www.experts-exchange.com/questions/Q.20015074.html
https://www.experts-exchange.com/questions/Q.20071728.html
https://www.experts-exchange.com/questions/Q.20075938.html
https://www.experts-exchange.com/questions/Q.20086694.html
https://www.experts-exchange.com/questions/Q.20091498.html
https://www.experts-exchange.com/questions/Q.20110041.html
https://www.experts-exchange.com/questions/Q.20114710.html
https://www.experts-exchange.com/questions/Q.20127095.html
https://www.experts-exchange.com/questions/Q.11360917.html
https://www.experts-exchange.com/questions/Q.20136586.html
https://www.experts-exchange.com/questions/Q.20161465.html
https://www.experts-exchange.com/questions/Q.20170435.html
https://www.experts-exchange.com/questions/Q.20177789.html
https://www.experts-exchange.com/questions/Q.20251794.html
https://www.experts-exchange.com/questions/Q.20197428.html


To view your locked questions, please click the following link(s) and evaluate the proposed answer.
https://www.experts-exchange.com/questions/Q.20090412.html
https://www.experts-exchange.com/questions/Q.20109565.html

PLEASE DO NOT AWARD THE POINTS TO ME.  
 
------------>  EXPERTS:  Please leave any comments regarding your closing recommendations if this item remains inactive another seven (7) days.  Also, if you are interested in the cleanup effort, please click this link https://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643

Moderators will finalize this question if still open in 7 days, by either moving this to the PAQ (Previously Asked Questions) at zero points, deleting it or awarding expert(s) when recommendations are made, or an independent determination can be made.  Expert input is always appreciated to determine the fair outcome.
 
Thank you everyone.
 
Moondancer
Moderator @ Experts Exchange

P.S.  For any year 2000 questions, special attention is needed to ensure the first correct response is awarded, since they are not in the comment date order, but rather in Member ID order.
Avatar of sageryd

ASKER

Oops. Thanks. Whatever



Happy holidays