Click here to Skip to main content
15,885,914 members
Articles / Desktop Programming / MFC
Article

Creating a Self Extracting Executable

Rate me:
Please Sign up or sign in to vote.
4.98/5 (22 votes)
20 Aug 2002CPOL3 min read 308.5K   5.7K   108   75
A class that allows you to create self extracting executables for use in distribution or setup programs

Self Extractor Builder

Introduction

This class allows you to create self extracting executables for use in distribution or setup programs. Many people have helped me along the way to produce this class but here are a couple I would like to thank.

  • Levente Farkas - For the suggestion of how the class might be implemented
  • Roger Allen - For further advice on the reading and writing of the data
  • Jamie Thornback - For help with the callback procedures
  • Tim Johnson - For his CShellFileOp class which is used briefly in this class

A new addition to this class is compression courtesy of Zlib and the following contributors: -

  • Luca Piergentili - For his suggestions and source code contributions for the compression features.
  • Mark Nelson - For his Zlib wrapper class which I mercylessly butchered into my own code.

Description of the Self-Extracting (SFX) executable

The SFX file which this class creates consists of an extraction executable with the data files appended to the end of it. The data on the end of the file does not affect the executable image and so the file executes as if the data wasn't even there. So to extract the data, the executable must first detach the data from itself and then create the approriate files. The way I have chosen to do this is to write a 'Table of Contents' (TOC) after the data which can be read by the extractor to find out where the various files are stored in the data segment.

File Layout

The layout of the TOC is as follows:-

Starting from the end of the archive and working backwards :

Header Info

  • 10 bytes - Signature (Identifier for SFX archive)
  • 4 bytes - Version number of SFX archive
  • 4 bytes - Number of files in archive

Table of Contents

This section contains one record in the following format for each file

  • 4 bytes - Length of filename
  • variable length - Filename
  • 4 bytes - Length of File (compressed)
  • 4 bytes - Length of File (uncompressed)
  • 4 bytes - Offset in archive to data

Data Segment

Each file is the compressed in memory using zlib and then written in the order of the TOC. After this is the extractor executable.

How To Use it

Having said all that, you don't need to know any of that stuff above to use it. All you need to do is create an instance of CSelfExtractor and then call AddFile() to add in all the files that you want to include. Next call Create() to create the archive.

The demo project consists of two projects - 'Extractor' which is the executable which extracts the archive and Self Extractor which is the program for building Self Extracting archives. Self Extractor allows you to specify an external extractor program to use for the archive or alternatively you can use the extractor which has been compiled into the program inside the resources. Read the source code to find out more.

The Zlib source code is subject to the licence documented here. The demos make use of classes written by other people at both codeguru.com and here at codeproject.com so any bugs in those should be directed at their respective authors.

Updates

21st August 2002 - Updated code with fixes suggested by readers relating to file permissions and CFileDialog. Also updated Zlib to v1.14 which fixes an important security problem.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United Kingdom United Kingdom
James is currently working as a Software Engineer providing large scale Warehouse Management Systems and Airport Baggage Handling Systems. He is a Windows specialist but nowadays spends about 65% of his time fighting with VI in a vain attempt to get his UNIX C code to compile. He has been programming in C/C++ for 6 years and Visual C++/MFC for 4 years.

In his spare time James plays a variety of musical instruments including guitar and piano with varying degrees of success. He has been told he spends too much time and money in the pub but doesn't everyone have their own stool at the bar?

James is originally from Nottingham (no Robin Hood jokes please) but is now based in sunny Manchester, UK.

The attached photo shows James in his favourite position, drinking beer with a hand growing out of his neck.

Comments and Discussions

 
GeneralRe: Execute without Extract? Pin
Rogerio Silva16-Jun-04 11:29
Rogerio Silva16-Jun-04 11:29 
GeneralRe: Execute without Extract? Pin
Anonymous23-Dec-04 3:10
Anonymous23-Dec-04 3:10 
GeneralRe: Execute without Extract? Pin
Alexandre Ravey14-May-07 23:12
Alexandre Ravey14-May-07 23:12 
GeneralExecute unpacked file... Pin
Luke_en21-Feb-03 9:04
Luke_en21-Feb-03 9:04 
GeneralNice Class James Pin
Brian Delahunty21-Aug-02 12:02
Brian Delahunty21-Aug-02 12:02 
GeneralRe: Nice Class James Pin
James Spibey21-Aug-02 21:04
James Spibey21-Aug-02 21:04 
GeneralRe: Nice Class James Pin
Brian Delahunty22-Aug-02 9:10
Brian Delahunty22-Aug-02 9:10 
GeneralRe: Interesting but... Pin
James Spibey21-Aug-02 23:22
James Spibey21-Aug-02 23:22 
Hi Russ and thanks for the comments. I'd like to address your comments one by one if I may so that it's clear why I made certain decisions.

Russ Freeman wrote:
CSelfExtractor - I think I'd prefer a reader and a writer.
When this class was first written, it didn't contain enough code to warrent splitting into two classes so that's why it was contained in one. You are more than welcome to split it now if you wish.

Russ Freeman wrote:
the example, and the class for this article use the same filenames
You are correct. I have changed the example application to be called 'Extractor Builder' to avoid any confusion.

Russ Freeman wrote:
CSelfExtractor::CreateArchive - takes a CFile*.
You are correct here. There is no error checking done on the pointer which could indeed cause a crash. I have updated this in version 1.12 of the class.

Russ Freeman wrote:
CSelfExtractor::GetThisFileName()
You are correct - there is a certain amount of redundancy here. I have included your suggestions in the next build.

Russ Freeman wrote:
CSelfExtractor::m_nFiles - this seems like it's not needed
No, this variable keeps track of how many files are to be written into the archive when Create() is called and so is required.

Russ Freeman wrote:
The projects use automatic precompiled headers
Hmm, you are correct but I've never noticed this before. I've updated the project file accordingly.

Russ Freeman wrote:
Lastly, the project does not compile with level 4 warnings
Only if you set 'warnings as errors' and as you say most of this is in the zlib stuff which is not part of this article.

Just a final few points. The code presented here was first written about 2 years ago because I was interested in discovering how to create files which were executable but which carried their data with them. As a result of this, I wrote this class and this sample application. It should be noted though that I have never used this code for anything other than this article, hence the code has never been through any formal testing as such. However, I know for a fact that it has been used in various commercial apps with no problems.

I'm sure there are hundreds of ways to achieve the functionality demonstrated here and this solution is just one of those ways. It was never meant to be a defintive answer to the problem but more of a 'you could do it this way'.

Anyway, thanks for your comments, I appreciate them as always. The code has been updated as of now Smile | :)

Cheers

James
Generalinternal error at another machine Pin
Anonymous20-Aug-02 19:13
Anonymous20-Aug-02 19:13 
GeneralRe: internal error at another machine Pin
James Spibey20-Aug-02 21:47
James Spibey20-Aug-02 21:47 
GeneralVery Good!!! - One bug fix. Pin
Matthew R. Miller17-Aug-02 11:52
Matthew R. Miller17-Aug-02 11:52 
GeneralCancel Problem Pin
19-Jun-02 12:59
suss19-Jun-02 12:59 
QuestionExtract on startup? Pin
28-May-02 12:22
suss28-May-02 12:22 
AnswerRe: Extract on startup? Pin
James Spibey28-May-02 22:17
James Spibey28-May-02 22:17 
GeneralRe: Extract on startup? Pin
29-May-02 19:32
suss29-May-02 19:32 
GeneralRe: Extract on startup? Pin
James Spibey29-May-02 21:10
James Spibey29-May-02 21:10 
GeneralRe: Extract on startup? Pin
29-May-02 21:26
suss29-May-02 21:26 
Generalerrors Pin
Shotgun23-Mar-02 2:46
Shotgun23-Mar-02 2:46 
GeneralRe: errors Pin
James Spibey25-Mar-02 1:05
James Spibey25-Mar-02 1:05 
Generalinteresting doubt Pin
1-Mar-02 1:30
suss1-Mar-02 1:30 
GeneralRe: interesting doubt Pin
James Spibey12-Mar-02 23:54
James Spibey12-Mar-02 23:54 
GeneralGood work Pin
Jerry Evans4-Aug-01 3:53
Jerry Evans4-Aug-01 3:53 
GeneralExcellent Pin
Rod Hamilton16-Sep-01 9:23
Rod Hamilton16-Sep-01 9:23 
GeneralRe: Good work Pin
7-Jan-02 19:31
suss7-Jan-02 19:31 
GeneralCan't add Files Pin
3-Aug-01 12:41
suss3-Aug-01 12:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.