YOU CAN CODE!

 

With The Case Of UCanCode.net  Release The Power OF  Visual C++ !   HomeProducts | PurchaseSupport | Downloads  
Download Evaluation
Pricing & Purchase?
E-XD++Visual C++/ MFC Products
Overview
Features Tour 
Electronic Form Solution
Visualization & HMI Solution
Power system HMI Solution
CAD Drawing and Printing Solution

Bar code labeling Solution
Workflow Solution

Coal industry HMI Solution
Instrumentation Gauge Solution

Report Printing Solution
Graphical modeling Solution
GIS mapping solution

Visio graphics solution
Industrial control SCADA &HMI Solution
BPM business process Solution

Industrial monitoring Solution
Flowchart and diagramming Solution
Organization Diagram Solution

Graphic editor Source Code
UML drawing editor Source Code
Map Diagramming Solution

Architectural Graphic Drawing Solution
Request Evaluation
Purchase
ActiveX COM Products
Overview
Download
Purchase
Technical Support
  General Q & A
Discussion Board
Contact Us

Links

Get Ready to Unleash the Power of UCanCode .NET

 


UCanCode Software focuses on general application software development. We provide complete solution for developers. No matter you want to develop a simple database workflow application, or an large flow/diagram based system, our product will provide a complete solution for you. Our product had been used by hundreds of top companies around the world!

"100% source code provided! Free you from not daring to use components because of unable to master the key technology of components!"


MFC Article: Creating Self-Extracted Executable file with open-source compression libraries

 
 

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 tucancode.net 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):

Collapse Copy Code
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.

 

 

 

Copyright ?1998-2022 UCanCode.Net Software , all rights reserved.
Other product and company names herein may be the trademarks of their respective owners.

Please direct your questions or comments to webmaster@ucancode.net