Compiled: Compiler included.
Writing Small Applications in Delphi.
For Windows 7 compatibility, read here:
Screenshot: compiled contents of archive.
VCL - Visual Components Library, is mainly responsible for the large size of delphi applications. VCL is used because it enables applications to be developed faster at the expense of inefficient code and therefore, bloated size. VCL will not be used in this example.
With Delphi, create a console application and replace all the code in the project with code below:
Removing VCL approximately results in a 12Kb application. This method is already known by Delphi programmers who want their application to be small. However, 12 Kb is quite a large size for an application which shows a MessageBox. To make applications even smaller many coders uses KOL unit replacement http://kolmck.net/. KOL - Key Objects Library is a set of objects to develop powerful (but small) 32 bit Windows GUI applications using Delphi but without VCL. It is distributed free of charge, with source code.
The second item that is responsible for the large size of Delphi applications is RTL - Run Time Library. RTL has specific pascal functions such as: AssignFile, Reset, Rewrite, ReadLn, WriteLn, etc. To minimise RTL, you can use reduced System.pas and SysInit.pas units [attached and below].
Minimum required structure of system and sysinit units:
You can create new System.pas and SysInit.pas by copying this code [attached], put them in the project directory and compile them with Borland Comandline Compiler.
You can view the options and descriptions for dcc32.exe by running it with no parameters.
Since you are no longer using RTL, you need to write out the WinAPI imports for each function. You can find the structure of the imports in the Borland import files. The WINAPI import for the MessageBox function is:
You can see '_MessageBoxA@16' string by opening user32.lib (imports from Visual C + +) with a HEX editor and looking for 'MessageBox'. The names of library functions have the form: "_MessageBoxA@16", where after the @ is the size of transmitted parameters.
After recompiling, the Hello application is 3.5 Kb in size.
To reduce the size of the application further, instead of Borland Linker [which adds six sections of 512 bytes to our file], we can use Microsoft Linker to bind the objects into an executable file. "A linker is a program that takes one or more objects generated by a compiler and combines them into a single executable program." http://en.wikipedia.org/wiki/Linker_%28computing%29.
At the release of Delphi 4, Borland changed their OMF [Object Module Format http://en.wikipedia.org/wiki/Relocatable_Object_Module_Format] from Intel OMF [which can be linked by Microsoft Linker] to Borland OMF [which can only be linked with Borland Linker. Since you have compiled the objects [Hello.obj] with Delphi 6 [included compiler version], they have a Borland OMF. To link the objects with Microsoft linker, you can use OMF2D written by EliCZ which converts Borland OMF to Intel OMF.
To convert your object, you can run OMF2D with parameters:
/U- Do not undecorate EXT/COM/EXP/PUBDEF names
You can use a HEX editor to retreive the entry point of Hello.obj to pass it as a parameter to Microsoft linker. In this case, it is Start$qqrv.
Now, you can run Microsoft linker with parameters:
Linker Options: http://msdn.microsoft.com/en-us/library/y0zzbyt4.aspx
The /ALIGN option specifies the alignment of each section within the linear address space of the program. The number argument is in bytes and must be a power of two. The default is 4K (4096). The linker issues a warning if the alignment produces an invalid image.
Use /FORCE:UNRESOLVED to create an output file whether or not LINK finds an undefined symbol.
Create a new .pas file in delphi and use this code:
Compile it with parameters:
Convert OMF by running OMF2D with parameters:
Retreive entry point with HEX editor from Hello.obj to pass it as parameter for Microsoft linker. In this case it is Start$qqrv.
Now run Microsoft linker with parameters:
Now you have an application of approximately 1Kb written in Delphi.
Now nobody can say that Delphi applications are big.
The archive contains all the required executable files and delphi source code to follow this tutorial completely and create your own small application in Delphi.
Delphi Basics - Free Delphi Source Code - Ultimate Programming Resource > Delphi Basics Articles >