Creating a Self Extracting Executable

Environment: VC5 SP3, NT4 SP4


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

Please note that this class does not contain any compression algorithms so the resulting executable with be the size of the sum of it's parts. The class could easily be extended to add compression in the future if anyone wishes.

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 - 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
4 bytes - Offset in archive to data

Data Segment
Each file is written (uncompressed) here 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.

As always, expect bugs. Don't shout if you can't get it to work. The demos are just as they sound - demos. They are not fully implemented and fully tested release software, they are an example of how the classes should be used. The demos make use of classes written by other people at CodeGuru so any bugs in those should be directed at their respective authors.


Download demo project - 76 Kb


  • GetModuleFileName & Win ME

    Posted by Legacy on 12/07/2001 12:00am

    Originally posted by: Steve Serrell

    Thanks for this great work. I've used it now for well over a year. Now that I have gotten a Win ME machine I have a problem in the Extractor. It seems that when the extractor calls GetThisFileName(), GetModuleFileName() returns ..\path\EXTRACTOR.EXE instead of the file name. I don't know a fix for this. I have tried variations on GetModuleFileName() to no avail. Any Ideas?

  • GetFileCount() broken?

    Posted by Legacy on 09/25/2000 12:00am

    Originally posted by: Tuncer M. Ayaz

    The Accessor-Function GetFileCount always returns 0.
    So it seems to me that either m_nFiles isn't stored or GetFileCount can't properly read this value out of the SFX.
    Any ideas?

  • VC++6.0 - WARNING IN int CSelfExtractor::CreateArchive(....

    Posted by Legacy on 05/10/2000 12:00am

    Originally posted by: David Lafay


    int CSelfExtractor::CreateArchive(CFile* pFile, funcPtr pFn, void* userData)

    don't return a value all the time,
    then the compiler generate a Warning.

    BUT it's not a big problem.


  • PowerArchiver SFX Maker

    Posted by Legacy on 03/24/2000 12:00am

    Originally posted by: David Cornish

    For those interested in creating more powerful SFX archives for FREE, visit:

  • Cool!!!

    Posted by Legacy on 02/17/2000 12:00am

    Originally posted by: Mike

    Very cool programm!!!

    I didn't know that you needn't patch the exefile if you
    create a new one!
    Keep on coding!!!

    • can't get demos to work?

      Posted by mok01 on 08/03/2004 05:07am

      When I tried compiling the Self Extractor program, i got hundreds of errors in the .h files....did you get this also at the beginning?

  • Thanks Guys

    Posted by Legacy on 02/14/2000 12:00am

    Originally posted by: James Spibey

    Thanks for the bug fixes, guys.

    These problems never surfaced during my (extremely sketchy) testing. Thanks for pointing them out.


  • two bugs and fixes

    Posted by Legacy on 02/12/2000 12:00am

    Originally posted by: eric

    James,at first,thanks for your cool code.
    but i've found two bugs and here are the fixes:
    1.file include error.
    after extract the source code,and recompile the project,
    the compiler report that can not found header both
    self extractordlg.h and extractordlg.h,change:
    #include "SelfExtractor.h"
    #include "DirDialog.h"
    #include "TextProgressCtrl.h"
    #include "..\shared\SelfExtractor.h"
    #include "..\shared\DirDialog.h"
    #include "..\shared\TextProgressCtrl.h"
    the build will be ok.
    2.when you extract the data file,there is an internal
    error.To fix this bug,you must change two places:

    (1).in selfextractor.cpp,modify the ReadTOC function from
    int CSelfExtractor::ReadTOC(CString Filename)

    (2).also in selfextractor.cpp,moddify the ExtractAll function from
    int CSelfExtractor::ExtractAll(CString Dir, funcPtr pFn /*= NULL*/, void * userData /*= NULL*/)
    if(!Thisfile.Open(GetThisFileName(), CFile::modeRead))
    if(!Thisfile.Open(GetThisFileName(), CFile::modeRead|CFile::shareDenyNone))
    and rebuild it,you can enjoy it now.
    this tech is very useful and very cool!!thanks,James.

  • Demo projects do not compile

    Posted by Legacy on 02/10/2000 12:00am

    Originally posted by: Rick

    I downloaded the demo project and extracted the 2 projects out of it. Neither of the projects will compile as is. They all have missing header files etc...

    • can't get demos to work....

      Posted by mok01 on 08/03/2004 05:08am

      I can't get the demos working either??? I get lots of errors in the .h files? any ideas whats wrong??

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Anthony Christie, the Chief Marketing Officer for Level Communications, is responsible for customer experience, worldwide marketing and product management. In this informative asset, he shares his insights into why a private network connection to cloud-bases applications is the right decision for your enterprise. Download now to find out more.

  • Moving from an on-premises environment to Office 365 does not remove the need to plan for disruptions or reduce the business risk requirements for protecting email services. If anything, some risks increase with a move to the cloud. Read how to ease the transition every business faces if considering or already migrating to cloud email. This white paper discusses: Setting expectations when migrating to Office 365 Understanding the implications of relying solely on Exchange Online security Necessary archiving …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date