Install Delphi Packages Tutorial

Forms Inside Packages In Chapter 9, I discussed the use of component packages in Delphi applications. Now I'm discussing the use of packages and DLLs for partitioning an application, so I'll begin talking about the development of packages holding forms. I've mentioned earlier in this chapter that you can use forms inside DLLs, but doing so causes quite a few problems. If you are building both the library and the executable file in Delphi, using packages results in a much better and cleaner solution. At first sight, you might believe that Delphi packages are solely a way to distribute components to be installed in the environment.

  1. Delphi Package File
  2. Delphi Package Installer

However, you can also use packages as a way to structure your code but, unlike DLLs, retain the full power of Delphi's OOP. Consider this: A package is a collection of compiled units, and your program uses several units. The units the program refers to will be compiled inside the executable file, unless you ask Delphi to place them inside a package. As discussed earlier, this is one of the main reasons for using packages. To set up an application so that its code is split among one or more packages and a main executable file, you only need to compile some of the units in a package and then set up the options of the main program to dynamically link this package. For example, I made a copy of the 'usual' color selection form and renamed its unit PackScrollF; then I created a new package and added the unit to it, as you can see in Figure 10.5. Figure 10.5: The structure of the package hosting a form in Delphi's Package Editor Before compiling this package, you should change its default output directories to refer to the current folder, not the standard /Projects/Bpl subfolder of Delphi.

To do this, go to the Directories/Conditional page of the package Project Options, and set the current directory (a single dot, for short) for the Output directory (for the BPL) and DCP output directory. Then compile the package and do not install it in Delphi—there's no need to. Loading Packages at Run Time In the previous example, I indicated that the PackWithForm package is a run-time package to be used by the application. This means the package is required to run the application and is loaded when the program starts, just as with the typical use of DLLs. You can avoid both aspects by loading the package dynamically, as you've done with DLLs.

  • Abstract: A follow on paper on the 'Dynamic Packages' (#27178) paper on how to create and use dynamic packages in Delphi. Advanced Dynamic Packages A follow-on to the 'Dynamic Packages' whitepaper by Vino Rodrigues vino@vinorodrigues.com.
  • Delphi Tutorial Creating component for VCL using Delphi XE10 Seattle. Delphi Tutorial Creating component for VCL using Delphi XE10 Seattle. Skip navigation. How to Install & Configure printer.

The directory where you store the.dcp file -- and the.dcu files, if they are included with the distribution -- must be in the Delphi Library Path. Choose Component > Install Packages from the IDE menu, or choose Project > Options and click the Packages tab. A list of available packages appears in the Design.

The resulting program will be more flexible, start more quickly, and use less memory. An important element to keep in mind is that you'll need to call the LoadPackage and UnloadPackage Delphi functions rather than the LoadLibrary/SafeLoadLibrary and FreeLibrary Windows API functions. The functions provided by Delphi load the packages, but they also call their proper initialization and finalization code. Besides this important element—which is easy to accomplish once you know about it—the program will require some extra code, because you cannot refer from the main program to the unit hosting the form. You cannot use the form class directly, nor access its properties or components—at least, not with the standard Delphi code. Both issues, however, can be solved using class references, class registration, and RTTI (run-time type information).

Let me begin with the first approach. In the form unit, in the package, I've added this initialization code: initialization RegisterClass (TFormScroll); As the package is loaded, the main program can use Delphi's GetClass function to get the class reference of the registered class and then call the Create constructor for this class reference.

Packages

To solve the second problem, I've made the SelectedColor property of the form in the package a published property, so that it is accessible via RTTI. Then I've replaced the code accessing this property ( FormScroll.Color) with the following: SetPropValue (FormScroll, 'SelectedColor', Color); Summing up all these changes, here is the code used by the main program (the DynaPackForm application) to show the modal form from the dynamically loaded package. Using Interfaces in Packages Accessing forms' classes by means of methods and properties is much simpler than using RTTI all over the place.

To build a larger application, I definitely try to use interfaces and to have multiple forms, each implementing a few standard interfaces defined by the program. An example cannot really do justice to this type of architecture, which becomes relevant for a large program, but I've tried to build a program to show how this idea can be applied in practice. Note If you don't know much about interfaces, I suggest you refer to the related portion of Chapter 2, 'The Delphi Programming Language,' before reading this section. To build the IntfPack project, I've used three packages plus a demo application. Two of the three packages (IntfFormPack and IntfFormPack2) define alternative forms used to select a color. The third package (IntfPack) hosts a shared unit, used by both other packages. This unit includes the definition of the interface.

I couldn't add it to both other packages because you cannot load two packages that have the same name with a unit (even by run-time loading). The IntfPack package's only file is the IntfColSel unit, displayed in Listing 10.1. This unit defines the common interface (you'll probably have a number of them in real-world applications) plus a list of registered classes; it mimics Delphi's RegisterClass approach, but makes available the complete list so that you can easily scan it.

Delphi packages tutorial

Delphi Package File

Var AComponent: TComponent; ColorSelect: IColorSelect; begin AComponent:= TComponentClass (ClassesColorSelectLbClasses.ItemIndex).Create (Application); ColorSelect:= AComponent as IColorSelect; ColorSelect.SelColor:= Color; ColorSelect.Display (False); The program uses the Supports function to check that the form really does support the interface before using it, and also accounts for the modal version of the form; but its essence is properly depicted in the preceding four statements. By the way, notice that the code doesn't require a form. A nice exercise would be to add to the architecture a package with a component encapsulating the color selection dialog box or inheriting from it. Warning The main program refers to the unit hosting the interface definition but should not link this file in. Rather, it should use the run-time package containing this unit, as the dynamically loaded packages do. Otherwise the main program will use a different copy of the same code, including a different list of global classes.

Delphi setup

Delphi Package Installer

It is this list of global classes that should not be duplicated in memory.