Package learning 4: Use of the Delphi package

Source: Internet
Author: User

Reproduced to: http://www.xxlinux.com/linux/e/DoPrint? Classid = 66 & id = 12739

Understanding the relationship between BPL and DLL will help us better understand how DELPHI works in component production, application, dynamic and static compilation. It is helpful for beginners who are still not clear about DELPHI development.
BPL vs. DLL

Part 1: Introduction to packages

Generally, when compiling a DELPHI application, an EXE file is generated, that is, an independent WINDOWS application. Important: Unlike Visual Basic, DELPHI generates pre-packaged applications that do not require a large number of runtime libraries (DLL's ).

Suppose: Open the default project of Delphi (only one blank form), F9 will compile and generate an executable file of about 295 KB (Delphi 5. Open Project | Options, select 'build with runtime packages ', and compile the file. The size of the EXE file is about 15 KB.

When compiling a DELPHI application, 'Build with runtime packages 'is not selected by default. The compiler will directly write the code required by 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.

First, what is a package?
In short, a package is a special dynamic link library shared by DELPHI 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.

Packages can be roughly divided into Run-time packages and Design-time packages ):
Runtime package-VCL and library functions are supported when running a program, and operations are similar to standard dynamic link libraries.
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.
Knowing how these packages are used, we also know how the packages are processed during the runtime and how they help DELPHI programmers.

One thing to note: to use the package well does not require you to become a mature control writer first. Beginners of DELPHI programming can also try to get in touch with the concept of package, which will help you better understand the working relationship between package and DELPHI.

Part 2: timely use of packages and DLL

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 ). For more information about how to use the dynamic link library in DELPHI, see related books.

Before comparing DLLs and BPLs, we need to know the two Code links of executable files: static links 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 (for example, Windows, Messages, SysUtils ,...), despite this, DELPHI can intelligently and automatically link the Code actually being 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 (DLLs. 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.
Method: After you select the 'build with runtime packages 'check box in the Project | Options dialog box, compile your 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.

Choose BPL or DLL? What is the difference?
You may wonder why you choose to use a runtime package instead of a DLL, or what other methods are there.

Compared with DLL, the concept of package is unique in DELPHI development, that is, applications written in other languages cannot reference packages created by DELPHI. Even if the package is a dynamic link library used by applications written by DELPHI, it also provides more Library Function Support for DELPHI programmers.

In Delphi, we usually create a dynamic link library (DLLs) to store the processes and functions required by applications in different environments. The package can not only contain untits), components, and forms can also contain the class (classes) in Delphi-this allows us to reference the object wizard encoding (Object Oriented Code) in it ). In the package, we can save the complete universal Delphi component, and the dynamic Runtime library (DLL's) is powerless.

In addition, DLLs and bpls play an equally important role in reducing program code, mainly because after using the package or dynamic link library technology, reduces the file size of the program. Of course, it should be noted that the DLLs or bpls to be loaded by the execution program may also be very large. For example, if you want to distribute your package files (mainly VCL packages, vcl50.bpl) at least 2 MB.
However, if you want to distribute multiple applications that share the same package, you can save a lot of trouble. After some files (such as standard Delphi bpls) already exist in the user's system, you only need to download the minimum execution file of the program. If your program is mainly distributed and carried out through the Internet, the efficiency is obviously greatly improved.
At the same time, the application of the package also saves the system memory, because the dynamic link result is: Only one VCL is read into the memory for all the dlephi applications that use the runtime package.

Package version Problems
When you want to upgrade your dynamic link library (change some of the execution functions), you can compile and generate new program files and upload new versions of files, all applications that are using the dynamic link library will still work (unless you have removed the existing old version ).
In other words, when upgrading the new package file, do not forget to upgrade the program execution file. As you know, a package file is a collection of unit files (units). All compiled unit files (DCU) contain version information. Therefore, unless we have the source code of the unit file, we cannot use the compiled unit in DELPHI4 or 5, so once we change any unit file listed in the uses clause of the Interface part of the unit file, the file needs to be re-compiled. The compiler checks the version information of the DCU file and determines whether the unit needs to be re-compiled. Therefore, we cannot use the packages compiled under DELPHI6 in DELPHI5 compiled applications. Any packages that serve your applications and your applications must be compiled in the same environment.

Therefore, when naming a package, you must keep the package name containing the DELPHI version information (for example, 'aboutdp50', where 50 represents Delphi 5 ). This can effectively prevent file version conflicts and avoid unnecessary troubles. The package user can know which DELPHI compiler the package version and package applies.

If you want to distribute the runtime or design period packages to other DELPHI programmers, we recommend that you provide them at the same time. DCP (contains the package header information and each unit file) and. BPL file and all unit files contained in the package. DCU file.

Part 3: create and use a runtime package

Create a package
Creating a package is simple, but some preparation work should be done before creating the package. First, you need to know which type of package file you are going to create: runtime package, design package, or both. Second, build and debug, repeatedly test the unit file you want to place in the package. Finally, name the created package file and select a suitable place to store the file.

To create a new runtime package, follow these steps:

1. Start DELPHI and SELECT File | Close All to Close the default project.

2. select File | New ..., double-click the Package icon () on the "New" page in the "new items" dialog box. The Package Editor () is displayed. The Package Editor Contains two folders: Contains and Requires.

3. Click the Add button 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. Added the FindFile and PictureClip unit files.

4. Open the Requires folder and expand the list to indicate the DCP file of the package required by the package. The most basic package file is to reference the vcl50.dcp file containing most of the Standard Visual controls.

5. After adding the unit file, click the Options button. 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 cannot install the two components shown in the figure to the IDE environment.

6. Use File | Save to Save the package project File (DPK), and then Save the package File, such as AboutDP50. It is important to name the package File.

7. Click the Compile button in the package to Compile the package.

8. if nothing happens, a package file (BPL file) will be created after the package is compiled, during which you may also need to determine the other packages you must add (such as VCLX50 ), these packages will be listed in the Requires folder.

9. Complete. The Borland package library file has been successfully created and will be ready for use.

There is an Install button in the package editor to Install the current package into a design phase package. If the package is run-time only, the Install button is unavailable.

About the created files
In addition to the DPK file and those unit source code files, 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.

Use runtime Package Design Program
When you start to use dynamic links to write applications, you do not need to write code to load runtime packages. Instead, you only need to make relevant settings in Project | Options, select the package list in the Runtime Packages edit box on the Packages page. The package is automatically linked when the program is compiled. When an application uses a runtime package, use the ADD button to ADD the package file.
Note: even if an application is linked to a runtime package, the unit files listed in the USES clause of the program must also exist. The compilation runtime package only tells the application where the component code can be found.

When configuring an application that uses a runtime package, determine that the user has executable files and library files (. BPL or. DLL) required by the program ). If the library file is in a different directory than the EXE file, you must specify the directory that the system can reach. Therefore, the best choice is the Windows \ System directory.

All in all
Packages enable you to flexibly select the distribution method of the application and simplify the installation of components. In addition, using packages reduces the file size of the application. Therefore, the Development Method of Using packages is of great significance.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.