Click here to Skip to main content
15,886,258 members
Articles / Desktop Programming / MFC
Article

Creating Self-Extracted Executable

Rate me:
Please Sign up or sign in to vote.
4.00/5 (6 votes)
15 Sep 20022 min read 98.1K   2.2K   42   20
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 lengthX
Filename 
File Content 
Pointer to Filename LengthX
SignatureX

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


Written By
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? Pin
IslamianFalcon24-Nov-05 20:08
IslamianFalcon24-Nov-05 20:08 
GeneralDetach and remove Pin
Bas Steijvers28-Jun-05 1:35
Bas Steijvers28-Jun-05 1:35 
GeneralRe: Detach and remove Pin
Nir Dremer28-Jun-05 2:01
Nir Dremer28-Jun-05 2:01 
GeneralRe: Detach and remove Pin
Bas Steijvers28-Jun-05 2:59
Bas Steijvers28-Jun-05 2:59 
Generalnot a true Self-Extracted Executable one Pin
includeh1018-Oct-04 23:58
includeh1018-Oct-04 23:58 
GeneralRe: not a true Self-Extracted Executable one Pin
Nir Dremer19-Oct-04 5:57
Nir Dremer19-Oct-04 5:57 
Questionextract to memory? Pin
Rogerio Silva20-Apr-04 17:13
Rogerio Silva20-Apr-04 17:13 
GeneralThrowing password Pin
Anonymous14-Feb-04 2:10
Anonymous14-Feb-04 2:10 
GeneralRe: Throwing password Pin
Nir Dremer14-Feb-04 23:06
Nir Dremer14-Feb-04 23:06 
GeneralRe: Throwing password Pin
Anonymous15-Feb-04 1:05
Anonymous15-Feb-04 1:05 
Questioncompile error? Pin
Member 2182036-Apr-03 20:24
Member 2182036-Apr-03 20:24 
GeneralGoing further... Pin
Dan Madden24-Sep-02 8:40
Dan Madden24-Sep-02 8:40 
GeneralRe: Going further... Pin
Nir Dremer24-Sep-02 12:18
Nir Dremer24-Sep-02 12:18 
Hi!

thanks for the comment.
I'm currently working for a virtual file system for other project i'm working on (the file system is actually implemented as a single file).
I might use this implementation in the file encryption (if it won't be an overkill) or i might use the the other comment advice which is to use the resources as the implementation of the file merging (which, personally i dont like much) Smile | :) .


thanks,
nir

Nir Dremer.
QuestionWhy not a resource? Pin
Henk Devos23-Sep-02 6:33
Henk Devos23-Sep-02 6:33 
AnswerRe: Why not a resource? Pin
Nir Dremer23-Sep-02 22:08
Nir Dremer23-Sep-02 22:08 
GeneralRe: Why not a resource? Pin
Henk Devos24-Sep-02 5:40
Henk Devos24-Sep-02 5:40 
GeneralRe: Why not a resource? Pin
Dan Madden24-Sep-02 8:29
Dan Madden24-Sep-02 8:29 
GeneralRe: Why not a resource? Pin
Henk Devos24-Sep-02 9:54
Henk Devos24-Sep-02 9:54 
GeneralRe: Why not a resource? Pin
Nir Dremer24-Sep-02 12:25
Nir Dremer24-Sep-02 12:25 
GeneralRe: Why not a resource? Pin
leandrobecker12-Feb-03 3:18
leandrobecker12-Feb-03 3: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.