1. Introduction of various files in Delphi and their relationships.
OXC: ActiveX control. It will be installed in a component package dpk. in imports, three files are created: Pas, DCU, and DCR.
DLL: the dynamic link library file. Its exports can be written in either the project file or the unit file. There is no difference.
Dpk: component package file, which is composed of multiple components (PAS.
BPL: the package file is actually a DLL file, but some features are added on the basis of the DLL, such as checking duplicate units. It is required by the integrated development environment of Delphi IDE and is a file generated by dpk compilation. The run-time type is used by the running EXE, and the design-time type is used by Delphi. Different versions have different formats. It can write exports like a DLL.
DCU: the compilation file is generated by PAS compilation (equivalent to the OBJ file). The connector can use DCU to connect the code to the target output file. The DCU format generated by each Delphi version is different.
Ii. Package Design Ideas
A package is a special dynamic link library shared by applications in the Delphi IDE environment. Package allows us to share part of our program as a separate module for other applications through multi-tier applications. Because the compiled DPL is a special DLL, you can write the Export Statement in the unit of a certain dpk,
1. Static and Dynamic Links.
Static link: When a Delphi project is compiled, all the code required by the project will be directly linked to your program execution file. The result is that the execution file contains all the units (units) required by the program. You may say that the Code is a bit lengthy, because by default, the uses clause of a form unit lists at least five basic units (such as Windows, messages, and sysutils, delphi can intelligently and automatically link the Code actually used in the unit to the project code, thus minimizing the execution file size. Using static links, our applications are relatively independent and do not require any additional support files or dynamic link libraries (BDE and ActiveX components are not considered for the time being ). In Delphi, static links are used by default.
Dynamic Link means that the application runs together with the standard dynamic link library (DLL. The dynamic link method does not need to directly establish the code to each application, and provides multi-threaded library function support for multiple applications separately. packages required during any program running will be loaded, it is worth mentioning that the packages that the program needs to call in a dynamic way are automatically loaded, so you do not need to write the code to load the packages. The setting method is as follows: select project> Options> packages and check the build with runtime packages check box to compile the program again, your program code is automatically linked to the Dynamic Running package, instead of Statically linking all referenced units to your project execution file. If you have [Build with runtime packages], DCU/PAS is not required during compilation.
For example:
Open Delphi's default Project (with only one blank form), and F9 will compile an executable file about 365 KB (Delphi7. Open project> Options> packages, select build with runtime packages, and compile the file. The size of the EXE file is about 17 kb.
When compiling a Delphi application, [Build with runtime packages] is not selected by default. The compiler will directly write the code required for the program to your EXE file, therefore, the generated program is a relatively independent program and does not need any ancillary support files (such as dynamic Runtime Library File DLL ), this shows why all the applications produced by Delphi are so large. To build a Delphi Program as small as possible, one method should give full play to the role of Borland package libraries, BPL for short.
2. Classification of packages:
It can be roughly divided into run-time packages and design-time packages ):
Runtime package: supports VCL and database functions when running a program. The operation is similar to the standard dynamic link library.
Design Phase package: used to install controls in the IDE environment of Delphi and create a special Attribute Editor for the controls. The design period package allows the inclusion of controls, attributes, and control editors. In the IDE environment, such packages are necessary for program design and are only used by Delphi and not distributed together with the developed applications.
3. necessity and benefits of separating runtime package and design package:
The code during design is not executed at runtime, And the compiler cannot know it. All the code is linked in. Therefore, separate design can prevent code expansion. A runtime package and a design phase package make the application smaller, so that all the relevant units are not linked to the execution Program (delphi5 and earlier versions), so that the Code expands.
Another benefit is to reduce compilation errors. Since DELPHI6, The VCL structure has been adjusted for delphi5 and earlier versions. Borland replaced dsgnintf with designintf, in addition, the Attribute Editor is also put into design files such as designeditors, designmenus, designwindows, and others. Specifically, designeditors uses another IDE file named proxies. Proxies is compiled into the designide. BPL file. Designide. BPL is no longer a file that can be distributed. We can only use it in the development environment. Needless to say, when compiling an application, the problem of "file not found" occurs. In this case, we separate the runtime package from the design package, and add the design package to the designide. DCP file to compile the package file separately to ensure that the package file is correct and compiled successfully!
Package package is similar to DLL, but it is only used in the Delphi and cbuilder environments. A good use package can make the program module clear and can reuse code to the maximum extent, so that the size of the program can be reduced as much as possible, and can be used in the two language environments.
4. To optimize the code structure, the design plan should include:
1> all registration statements (preferably in a separate unit );
2> All attribute editors;
3> All component editors;
4> Replace the packages (the designide. DCP file, written in requires: designide) that must be supported by designide ).
Note: The component editor displays shortcut menus on components, such as tables. It is similar in production and inherited from tcomponenteditor.
5. To optimize the code structure, the runtime package should include:
1> component itself.
2> the form that the component may call at runtime (used by the property editor ).
Note: The package is designed based on different versions. The editor version of Delphi 6.0 is ver140, and the Editor Version of Delphi 5.0 is ver130, if we want to adapt to different versions, use {$ ifdef ver140 }... {$ Else }... {$ Endif}; Control in uses domain.
3. Use packages BPL and DLL in a timely manner
It is generally considered that joining the dynamic Runtime Library in the Windows operating system is the most useful and efficient application. In Windows, many applications run simultaneously, which may cause memory problems. Many programs execute similar operation tasks, but they are controlled and completed by different codes. The function of the dynamic Runtime Library is to put the code in your execution program into a DLL in a system shared environment. The most intuitive dynamic link library example is the Windows operating system itself and its own APIs.
The dynamic link library is usually used to set procedures and functions for the program to call. Of course, while writing a dynamic link library, we can also put a Delphi Form in a DLL (for example, a aboutbox form ), in addition, we can also store resources required by the program in the DLL ).
Compared with DLL, the concept of package is unique in Delphi development. Applications written in other languages cannot reference packages created by Delphi. Package is a dynamic link library that can be used by applications compiled by Delphi. It also provides more Library Function Support for Delphi programmers.
Usually, we create a dynamic link library (DLL) in Delphi to store the processes and functions required by applications in different environments. The package can not only contain Unit), components, and forms can also contain classes in Delphi (such as classes, in which we can reference object oriented code ). In the package, we can save the complete universal Delphi Component, while the dynamic Runtime library (DLL) is powerless.
In addition, DLL and BPL play an equally important role in program code reduction, mainly because after using the package or dynamic link library technology, reduces the file size of the program. Of course, the DLL or BPL to be loaded by the execution program may also be very large. However, if you want to distribute multiple applications that share the same package, you can save a lot of trouble. When some files (such as standard Delphi BPL) already exist in the user's system, you only need to download the minimum execution file of the program. If the program is mainly distributed and carried out through the Internet, the efficiency is obviously greatly improved.
Package applications also save system memory, because the dynamic link result is: Only one VCL is read into the memory for all Delphi applications that use the runtime package.
4. Basic Steps for creating a package:
1. Start Delphi and choose File> close all to close the default project.
2. Select File> New... to bring up the new items dialog box. Then, choose new> package. Double-click the package editor. The package Editor contains two folders: contains and requires.
3. Click Add to add a unit file (component or simple code unit file ).
(Note: You have added the PAS source code file instead of the compiled DCU file. When you add a unit file, the unit name in the package is displayed in the contains folder of the package editor. Open the requires folder and expand the list to indicate the DCP file of the package required by the package. The most basic of the package file is to reference the VCL. DCP file containing most of the Standard Visual controls. You can also see the required
Other packages to be added)
4. Click options. In the usage Options Group on the description panel, select the package type.
(Design period package, runtime package, or both. If you select runtime only (only the runtime package), users of other packages will not
Install components in the IDE environment)
5. Use File> Save to save the package project file (dpk), and then save the package file, such as aboutdp70.
6. In the package Editor, click the compile button to compile the package. If no error occurs, a package file (BPL file) will be created after the package is compiled ).
7. After the BPL file is successfully created, it will be ready for use.
8. There is an install button in the package editor to install the current package into a design package. If the package is
Run-time only (only the runtime package), The Install button will not be available.
In addition to the dpk file and the unit source code file, Delphi also uses the dynamic link version of the package to generate a BPL file and a DCP file containing the package ID information, the DCP file is a collection of the identity information of the compilation file (DCU) that contains the unit file in the package.
The release package must include:DCP (containing the package header information and each unit file) and. BPL files, all unit files. DCU files.
The design concept of Delphi package and its relationship with PAS, BPL, DCU, DLL, OXC.