BPL vs. dll (Original http://www.delphi3000.com/Translation: tenants) 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 dividedRun-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 and Dynamic Links. Static Link means that when a Delphi project is compiled, all the code required by the project will beDirectLink 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. UseStatic LinkOur application is a relatively independent program and does not require any additional support files or dynamic link libraries (BDE and ActiveX components are not considered for the moment ). In DelphiDefaultStatic links are used. 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 in the development of Delphi.UniqueThat 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. We usually create a dynamic link library (DLLs) in Delphi to storeDifferent EnvironmentsThe processes and functions required by the application. The package can not only contain code units (untits), components, and forms, but also contain classes (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, in terms of program code reduction, DLLs and bpls play equally important roles. The main reason is that after using the package or dynamic link library technologyReduces 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 packageApplications also save system memoryBecause the result of the dynamic link is that 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), allAll 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 ..., on the "new" page in the "new items" dialog box, double-click the package icon () to display the package Editor (): the package Editor contains 2 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 have to determine the other packages you must add (such as vclx50 ).In the requires fileFolder. 9. Complete. The Borland package library file has been successfully created and will be ready for use. There isThe Install button is used to install the current package into a design 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 identifier 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 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. |