News   Articles   Sources   Code libraries    RSS

Language:

English   Russia  

Current filter:

All   C#   C++   C  



Log in:

Name
Pass

Register

Security Software for Administrator

Get rid of MFC for the smaller file size


Language:C
Category:System
Date: 2008-09-22
Author:Security Software for Administrator

The most effective method for decreasing the program size in hidden in the answer to the question, "Why are Visual C++ programs so large?" This is because C++ is an object-oriented language. In this language, each program component is an object with its own properties, methods, and events. Each object is self-sufficient and can do much without your instructions. This means you just need to associate it with your form, change its properties appropriately, and the application is ready! It will work without your detalization of its activity.

There are a few disadvantages in object programming. Objects implement many actions that a programmer or user can execute. However, in any actual program only two or three of these properties are used. The others are lumber, and nobody needs it.

How can you create a compact code so that a program takes up as little space as possible on the hard disk and in the memory? There are two ways for doing this.

  • Don't use MFC library (VCL library in Borland Delphi) that simplifies programming. In this case you'll have to type all your code manually and use only WinAPI. The program will be very small and fast. The resulting code will take up less space than when you use MFC and the maximum compression. On the other hand, you'll miss the simplicity of visual programming and feel all inconvenience of programming with "pure" WinAPI. For further decreasing the file size, you can use the assembly language. However, it is too complicated, and it would take much more time to write in it than in the pure C. This is why this topic isn't discussed in this book.
  • Compress completed programs with packers. The object code will be compressed by several times, and an application created with MFC can turn from a 300 K monster to a little "animal" that takes up just from 30 K to 50 K on a disk. The main advantage of this approach is that you enjoy object programming and forget the inconvenience of WinAPI.

We already discussed the second method, so let's look at the first one.

If you want to create a really small program, you should abandon comfort. You won't be able to use visual forms and other useful modules created by Microsoft to simplify a programmer's life. You won't be able to use classes or ActiveX components. You'll confine yourself to Windows API functions.

Now let's look at the things in practice. Start Visual C++ and create a new project. To do this, select File/New/Project. A new project window will open (Fig. 1.1). The project type hierarchy is at the left. We are writing in C++, so select the Visual C++ Projects item. This item should be selected for all examples in this book. The Templates pane at the right will display icons for creating various projects with wizards. Select MFC Application.


Fig. 1.1. Program settings

There are two input boxes below the panes. In the first box, specify a name for the project being created. It will be the name of the executable file and of the file you'll edit. Let it be TestMFC.

In the Location box, you should specify the path to a folder, in which the development environment will create necessary files. I recommend you to create a folder, say, My C++ Projects, in which you'll store your projects. Select this folder and click OK. When the wizard completes, your My C++ Projects folder will contain the TestMFC folder that will contain the files.


Fig. 1.2. New project wizard window

As soon as you click the OK button in the new project window, the MFC application wizard will open. You can click the Finish button to create an application with default parameters, or you can specify custom settings. Our current task is to create a small application, so let's try to optimize the application the wizard creates. <.p>

There are a few sections at the left. By selecting them, you'll get options for adjusting particular parameters. Let's select these sections in turn to delete unnecessary features and decide which settings will be used when creating applications for the subsequent examples.

  • Application Type. This section defines the type of the application. Specify the following:
    • Single Document. One window will be enough. We won't write multi-document applications, and most of the examples using MFC will be based on this type of application or on dialogs (Dialog based).
    • Project style. We'll use the default style, the MFC standard, in all our applications.
    • Document/View architecture support. We don't care about this setting, therefore, leave the default.

In the other sections, leave the default settings because we won't use databases or documents. In most cases we'll be satisfied with one window and a menu. In the first examples, we'll try to do without MFC.

Click the Finish button to finish the wizard. After this you'll see a window like shown in Fig. 1.3. You'll use this window quite a lot, and I am going to describe it little by little. I would make a mistake if I told you everything about this window right now. You simply won't be able to remember this. However, during practical work, you'll easily understand elements of the window.


Fig. 1.3. MS Visual C++ development environment, main window

We are interested in the project parameters. We should disable everything that hampers. When linking a Visual C++ project, two types of settings can be used by default: debug and release. The first is necessary at the development stage. In this mode, Visual C++ creates an executable file that contains too much additional information. You'll need it later when debugging your program. In the second mode, this information isn't included, and the executable file will be smaller.

On the toolbar, there is a drop-down list that reads Debug. Change this setting to Release.

Visual C++ development environment can create executable files that use two types of MFC libraries: static and dynamic. The dynamic linking is used by default. The executable file will have a smaller size, but it won't work without dynamic libraries such as mfcXXX.dll where XXX is the version number of the development environment.

As a result, we'll have to send our customer the libraries in addition to the executable file so that he or she can run our program. This is both impolite and inconvenient. It is best to use static compiling. The executable file will be much larger, but it will contain everything that is necessary. In this case no additional libraries will be needed.

To change the type of MFC use, select the name of your project in the Solution Explorer window and then select the Project/Properties menu. You'll see the property pages window looking like shown in Fig. 1.4.


Fig. 1.4. Property pages window

There is a list of property categories in the left part of the window. We are interested in the General item. Select it, and you'll see a list of corresponding properties in the main pane. Find the Use of MFC property and change its value to Use MFC in a Static Library. Click OK to close the window and save the changes.

Let's build our project to an executable file. To do this, select the Build/Build solution menu. At the bottom of the main window, the Output pane will display the progress of building. Wait until you see a message like this:


---------------------- Done ----------------------

    Build: 1 succeeded, 0 failed, 0 skipped

Open the folder you allocated for your projects and find the TestMFC folder. It should contain source files of your project generated by the wizard. In addition, there should be the Release folder containing intermediate files and the executable file created during compilation. Select the TestMFC.exe file and look at its properties (right-click the file and select Properties in the pop-up menu). The size of our empty project is 386 K. This is very much.

Try to compress it with ASPack. In my experiment, the compressed file was 187 K long. The compression was almost 50%, and this is a more or less reasonable size for a joke program.

To make the program even smaller, you have to abandon MFC and write in pure C. This is a little more difficult and rather inconvenient. However, this is acceptable for small projects.

To create a small program without MFC, select File/New/Project again and then select the Win32 Project type. Let's name it CTest and leave the path unchanged.

If your previous project is still open, there are two radio buttons below the location text box: Add to solution and Close solution. If you select the first one, the new project will be added to the opened project. If you choose to close the previous project, it will be closed, and a new work area will be created for you.

After you click the ОК button, a wizard window will open. The first step is informative, so select the Application Settings section. You'll see a window like that shown in Fig. 1.5.


Fig. 1.5. Application settings window

We wish to create a simple Windows application, therefore, select the Windows application item in the Application type section. Don't check any checkboxes to keep the wizard from adding extra components. We need just a minimal application. Click the Finish button, and the new project will be created.

Here you should also change Debug to Release to obtain a project without additional information. You don't need to change anything in the project settings because the sample project created by the wizard doesn't use MFC and doesn't need dynamic libraries. To check this, open the project properties and make sure that the Use of MFC property has the Standard Windows Libraries value. This means that there are no MFC, and the program doesn't need anything in addition to the standard Windows libraries.

Compile the project. To do this, select Build/Build solution. When building completes, open the ctest/release folder in your project folder and look at the size of the resulting file. As for my file, it was 81 K long. When compressed, it will take up less than 70 K. Such a program will pass via the net very quickly.

Of course, it can be optimized even further by removing a few components that aren't used but take up valuable space. However, we won't do this.


Comments:

Bhushan : 1 Question

If MFC is not used, then how to write GUI based programs


Flenov : MFC vs Windows API

MFC is a OOP wrapper for Windows API. You have to use Windows API directly to build your GUI and do not use any OOP wrapper. OOP makes our life easier, but generate bigger code size.

In most cases you don't need in minimal size, but need in faster development. That's why we use MFC.


Send your comment

Your name:
Vote:
Title:
Comment:
Protection code:





Submit an article   Submit a file

Copyright © HackishCode.com 2008. All rights reserved
WEB Design and WEB Development by WEB consulting company ProfWebDev.com
www.hackishcode.com