Click here to Skip to main content
Click here to Skip to main content
Go to top

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

Introduction

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
Executable  
Filename length X
Filename  
File Content  
Pointer to Filename Length X
Signature X

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.

Implementation

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.
    else
    {
        // 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);
        self.setFilename(argv[1]);
        // attaching the attache file.
        self.attachFile(argv[2]);
        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.

License

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

Share

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:
http://www.dremer.net/

Comments and Discussions

 
QuestionPE Graphical Area Access? PinmemberIslamianFalcon24-Nov-05 20:08 
GeneralDetach and remove PinmemberBas Steijvers28-Jun-05 1:35 
GeneralRe: Detach and remove PinmemberNir Dremer28-Jun-05 2:01 
GeneralRe: Detach and remove PinmemberBas Steijvers28-Jun-05 2:59 
Generalnot a true Self-Extracted Executable one Pinmemberincludeh1018-Oct-04 23:58 
GeneralRe: not a true Self-Extracted Executable one PinmemberNir Dremer19-Oct-04 5:57 
Questionextract to memory? PinmemberRogerio Silva20-Apr-04 17:13 
GeneralThrowing password PinsussAnonymous14-Feb-04 2:10 
GeneralRe: Throwing password PinmemberNir Dremer14-Feb-04 23:06 
GeneralRe: Throwing password PinsussAnonymous15-Feb-04 1:05 
Questioncompile error? Pinmemberpanruti6-Apr-03 20:24 
GeneralGoing further... PinmemberDaniel Madden24-Sep-02 8:40 
GeneralRe: Going further... PinmemberNir Dremer24-Sep-02 12:18 
QuestionWhy not a resource? PinmemberHenk Devos23-Sep-02 6:33 
AnswerRe: Why not a resource? PinmemberNir Dremer23-Sep-02 22:08 
GeneralRe: Why not a resource? PinmemberHenk Devos24-Sep-02 5:40 
GeneralRe: Why not a resource? PinmemberDaniel Madden24-Sep-02 8:29 
GeneralRe: Why not a resource? PinmemberHenk Devos24-Sep-02 9:54 
GeneralRe: Why not a resource? PinmemberNir Dremer24-Sep-02 12:25 
GeneralRe: Why not a resource? Pinmemberleandrobecker12-Feb-03 3:18 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 16 Sep 2002
Article Copyright 2002 by Nir Dremer
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid