Click here to Skip to main content
13,288,104 members (62,580 online)
Click here to Skip to main content
Add your own
alternative version


41 bookmarked
Posted 15 Sep 2002

Creating Self-Extracted Executable

, 15 Sep 2002
Rate this:
Please Sign up or sign in to vote.
How to create a Self-Extracted executable with minimal overhead


In this article, I will give a brief information on how to create self-extracted executable.

Understanding executables

When the operating system is executing an executable file, it knows how to execute it due to the PE header which is a big table that holds all the information regarding the executable and what it holds (the different sections) in the beginning of the file.

Because we're not allowed to modify executable itself (we don't want to cause harm), our starting point will be the end of the file.

In order that both the attaching method and the detaching method will talk the same language we need to set a format:

 Constant Size
Filename lengthX
File Content 
Pointer to Filename LengthX

Using this format we're not constrained to a specific size of the filename or the file content.

To be clear, when we're detaching a file from the merged file ( executable + attached) the end of file is the new end of file, that's why it's very important that the end will be in constant size and will give us information about the attached file. In the above table the constant section is the "Pointer to Filename Length" & "Signature" sections.


In order to implement such a task we need two basic methods:

  • attachFile - appending file to a self-extracted executable.
  • detachFile - taking appended file and writing it to disk.

Additional method can be implemented for convenience:

  • checkSignature - checking if the file has a file attached to it.

Using the above methods, it's very easy to create a self-extracted executable.

Example (Using SelfExtract class)

In order to create one executable for attaching and detaching we need the help of checkSignature method.

Using this method we can decide the mode of operation. If we've file attached we're in detaching mode and if don't have file attached we're in attaching mode.

In order to use the tool more then one time, every time we're attaching a file we'll not attach file to the calling executable, but we'll duplicate it using the CopyFile API.

The easiest way to understand is, to look at the following example (can be downloaded at the top of the page):

int _tmain(int argc, _TCHAR* argv[])
    SelfExtract self;

    if (self.checkSignature())
            puts("Detaching internal file, please wait...");
        // detecting output diretory.
        // if no parameter then current directory.
        char *outDir = 0;
        if (argc > 1)
            outDir = argv[1];
        char detached[MAX_PATH];
        // detaching internal file.
        self.detachFile(outDir, detached, MAX_PATH);
               printf("file %s detached successfully.\n", detached);
        return 0;
    // if no file is attached.
        // missing parameter(s) - showing command line parameters.
        if (argc < 3)
            puts("SelfExtract class example utility, by Nir Dremer");
            printf("Usage: %s resultFile attacheFile\n", argv[0]);
            puts("resultFile is executable you 
                     want to create that will be self extracted.");
            puts("attacheFile is the file 
                     you want to attach to resultFile.");
            return 0;

        printf("Creating %s, please wait..", argv[1]);
        // copying this file as a header for the self extracted executable.
        CopyFile(argv[0], argv[1], true);
        // attaching the attache file.
        printf("Process completed, execute %s to detach.", argv[1]);
    return 0;

Advanced Issues

The SelfExtract class support only attaching/detaching of one file. I believe that this is enough due to the fact that, if you want more then one file it will be good idea to compress the files first (there are many open-source compression libraries) and to attach the compressed file.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Nir Dremer
Product Manager
Israel Israel
Nir is a Product Manager from Israel with past Software Engineering Experience.

You're welcome to visit his photography site:

You may also be interested in...


Comments and Discussions

QuestionPE Graphical Area Access? Pin
IslamianFalcon24-Nov-05 21:08
memberIslamianFalcon24-Nov-05 21:08 
GeneralDetach and remove Pin
Bas Steijvers28-Jun-05 2:35
memberBas Steijvers28-Jun-05 2:35 
GeneralRe: Detach and remove Pin
Nir Dremer28-Jun-05 3:01
memberNir Dremer28-Jun-05 3:01 
GeneralRe: Detach and remove Pin
Bas Steijvers28-Jun-05 3:59
memberBas Steijvers28-Jun-05 3:59 
Generalnot a true Self-Extracted Executable one Pin
includeh1019-Oct-04 0:58
memberincludeh1019-Oct-04 0:58 
GeneralRe: not a true Self-Extracted Executable one Pin
Nir Dremer19-Oct-04 6:57
memberNir Dremer19-Oct-04 6:57 
Questionextract to memory? Pin
Rogerio Silva20-Apr-04 18:13
memberRogerio Silva20-Apr-04 18:13 
GeneralThrowing password Pin
Anonymous14-Feb-04 3:10
sussAnonymous14-Feb-04 3:10 
GeneralRe: Throwing password Pin
Nir Dremer15-Feb-04 0:06
memberNir Dremer15-Feb-04 0:06 
GeneralRe: Throwing password Pin
Anonymous15-Feb-04 2:05
sussAnonymous15-Feb-04 2:05 
Questioncompile error? Pin
panruti6-Apr-03 21:24
memberpanruti6-Apr-03 21:24 
GeneralGoing further... Pin
Daniel Madden24-Sep-02 9:40
memberDaniel Madden24-Sep-02 9:40 
GeneralRe: Going further... Pin
Nir Dremer24-Sep-02 13:18
memberNir Dremer24-Sep-02 13:18 
QuestionWhy not a resource? Pin
Henk Devos23-Sep-02 7:33
memberHenk Devos23-Sep-02 7:33 
AnswerRe: Why not a resource? Pin
Nir Dremer23-Sep-02 23:08
memberNir Dremer23-Sep-02 23:08 
GeneralRe: Why not a resource? Pin
Henk Devos24-Sep-02 6:40
memberHenk Devos24-Sep-02 6:40 
GeneralRe: Why not a resource? Pin
Daniel Madden24-Sep-02 9:29
memberDaniel Madden24-Sep-02 9:29 
GeneralRe: Why not a resource? Pin
Henk Devos24-Sep-02 10:54
memberHenk Devos24-Sep-02 10:54 
GeneralRe: Why not a resource? Pin
Nir Dremer24-Sep-02 13:25
memberNir Dremer24-Sep-02 13:25 
GeneralRe: Why not a resource? Pin
leandrobecker12-Feb-03 4:18
memberleandrobecker12-Feb-03 4:18 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171207.1 | Last Updated 16 Sep 2002
Article Copyright 2002 by Nir Dremer
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid