Chapter 5 of wuti Delphi

Source: Internet
Author: User
Chapter 5 Package

In our daily life, we will use all kinds of bags, including wallet, briefcase, backpack, and schoolbag ......, The package contains frequently used items. Once you leave the house that day, you can bring the bag with it. You don't need to consider the specific items in the package, even though some items in the package are needed. With these packages, you can travel easily. Of course, poor packaging or too many packages will become cumbersome.
Similarly, in Delphi, you can put commonly used programs and data in the package so that these packages can be released and run along with your program. At this time, the program can become very short, light, and easy. Especially when packages can be used by multiple application modules, application distribution and management can be made more convenient.
What is the first package?
A package is a special dynamic Connection Library. It is an executable module with the. BPL (Borland package Library) extension. This dynamic Connection Library is applied to Delphi applications or Delphi IDE. In Delphi, there are packages used only for Runtime (runtime only packages) and only for design (designtime only packages) and runtime and designtime packages that can be used at runtime and design time ).
The runtime package provides specific functions for the application. It is the Runtime Library on which the application depends. The design package is used to install components to the IDE environment of Delphi and create auxiliary design elements such as attribute editors used only in IDE, and design experts and Wizard programs that extend the IDE functions. A package can have both the design and runtime functions, and the runtime package is generally called.
Like other runtime libraries, the package contains code and data that can be shared by applications. In DELPHI6, the most common package may be vcl60.bpl. Applications compiled in package mode generally use vcl60.bpl unless your applications do not use the Delphi Component Library. When compiling in package mode, the executable modules that reference a package, such as EXE, DLL, or its BPL module, only contain references to the data and code in the package, it does not contain these code bodies and data bodies.
It is very easy to compile the program into a package in Delphi. Open the build with runtime packages option on the packages page of the Project Options dialog box, enter the desired package in the edit box, and compile the program.
 
The size of the execution file compiled in package compilation mode is usually an order of magnitude smaller than that of the normally compiled execution file. This is because the normally compiled execution file contains a lot of data and Code such as the Delphi control, process, and function used by the program. Normally compiled execution files can run independently without other support. The execution files compiled in the package compilation mode do not contain data and Code such as Delphi controls, processes, and functions. However, the running of the package requires support from the *. BPL file (Borland package library.
Writing a package is no different from writing a program without a package. Are all use-related units, and then use the data, objects, processes, functions, and methods in the unit. Finally, you only need to determine whether to use the package mode during compilation. This is the convenience and simplicity of Delphi.
In Delphi, packages are also the core of IDE's visual program design. Almost all delphi components exist in the package. The package used in the design and development environment of Delphi is the design package, and the various components on the component panel are provided by it.
A package can be used in a visual design environment (design package) or a running environment (running package ), or both environments can be used (Design and Operation package ). Everything can be put in the bag, constants, variables, processes, functions, classes, components ......, It can be said that it is all-encompassing.
You can also create your own package and put common program units in the package.

Section 2 Package principles
We know that the BPL file generated after the package compilation is actually a DLL file. However, when we use a process or function in a package in a program, we do not need to extract (exports) These Procedures and functions in the package, you do not need to declare these procedures and functions as external (external) in the referenced program ). The process or function in dll must be used. Although, like DLL, these processes and codes are shared in the process space, the process or function in the package is more convenient and intuitive to use. Even more surprising, the global variables declared in the package can be directly accessed, and a global variable defined in the package has only one in the whole program space. It does not directly access the variable as the variable in the DLL, nor does it have a copy of the variable as the executable modules of non-package compilation.
Why?
Originally, this was the magic of Delphi.
Section 3 debugging package
In the past, I had read a good book about developing delphi components, which was brilliant and benefited me a lot. However, it said: As there is no way to debug components in the package on the basis of the source program, the development components must carefully write the code of the components. Indeed, you can use Delphi to open a *. dpk file separately. All debug commands such as run (F9), step over (F8), and track into (F7) of Delphi are invalid. I think about it myself. Delphi's Ide is closely related to the package files during design. Debugging the package itself in the IDE running environment where the package is loaded seems a bit like the self-inverse paradox in logic. Later, I always believed that the package could not be debugged on the basis of the source program.
However, the package can directly debug the original code in the Delphi IDE environment! You can not only debug the runtime package, but also debug the design time package )!
By accident, I forcibly added my package to the Project Group of Delphi. At this time, I was surprised to find that the tool button for running the program is valid. At that time, I could not believe my eyes! In particular, there is a deep-rooted impression of this authoritative book in my mind.
Subsequent practices prove that as long as the package file is in a project group, it can be debugged just like a DLL project. You only need to set the corresponding host application in the run paramenters to start debugging. Of course, the main application must be built with runtime packages.
Why?
I think that after compilation of a project file in Delphi, an executable module will be formed, either an EXE file or a DLL file. In fact, the BPL file after package compilation is also a DLL file and an executable module. Therefore, the dpk file is equivalent to a project file. However, when you use Delphi to open the dpk file, it is not opened as a project. Somehow. In the project team, Delphi activates each added item as a project. A typical application system is usually composed of EXE, DLL, and BPL. Therefore, it is reasonable to add a package as a project to the project team, and Delphi should handle it like this.
To debug a separate package, perform this operation. First, close all projects opened by Delphi, that is, use the file/close all menu command. Then, run the view/Project Manager menu command to open the project manager. Right-click the project manager and select the add existing project command. In the open file window that appears, select Open *. dpk file. Find the package file you want to debug and add it to the current project group. In this case, debug commands such as run (F9), step over (F8), and track into (F7) of Delphi are all effective. Finally, you only need to set the main application to start debugging the package.
What should I do if I want to debug the designtime package or debug the component errors in the IDE environment?
You just need to set the host applicationto delphi32.exe. Of course, the specific location of delphi32.exe depends on the specific directory where you install delphi. Typical examples are: C:/program files/Borland/delphi5/bin. In this way, you can debug the behavior of the package in the IDE environment as you like. You must first install your package in the ideenvironment. If not, the uninstalled package will not be loaded when delphi32.exe is started.

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.