The “Delphi in all its glory” book [3]

The second book in “Delphi in all its glory” series was released 05.2024.

This is the third book in the series.

Order the book in electronic of printed format.

Delphi book about libraries and packages

 


Intro

 

Because my original book was getting over 1000 pages, I had to split it into three tomes. The “Libraries” chapter from the old editions of the Delphi in all its glory was extracted and expanded into a new book (this book).  If you already own the old edition, you don’t really need to purchase this book.

This book is dedicated to those who wrote 1 million SLOC programs without splitting their code into libraries.

 

“Modularization isn’t about breaking code into pieces.
It’s about composing a masterpiece from harmonious parts.”

 

Important note!

By purchasing this book, you will get access to the source code of the Delphi Proteus commercial library. Proteus is a licensing system that helps converting (needs a single line of code) a dull Delphi program into a commercial product that we can deliver to our customers as time-limited or feature-limited trial/shareware.

 

1.  Overview

Welcome to a journey that will transform you into a masterful creator of well-designed, high-performance, rock-solid applications, visually appealing applications.

Intended Audience

The first book in the “Delphi in all its glory” trilogy concentrates on the exploration of the Delphi language. Whether you’re a curious novice venturing into the world of coding for the first time or already the user of a different programming language, this book is your gateway to Delphi coding.

 

Even if you are a Delphi programmer, you could still learn a few tricks (especially things related to code-safety) from the that book. The book is also perfect for old-school Pascal programmers that were always afraid to switch to objects.

 

The second book addresses the more seasoned developers aiming to enhance their craft. It focuses on the more advanced topics, such as 64-bit code upgrade, compiler and debugger inner working, speed improvements, memory management, proper exception handling, architecture design and code-safety in general.

 

This third book goes deep into the inner working of Delphi’s library system. I will concentrate more on how libraries are useful to organize your code, how to set up your own library system and how libraries can dramatically increase your compilation speed. Building custom visual components will not be in focus, even though we will have a chapter and some code about that. I intend to expand the book to cover more of that.

There will also be a chapter about how I turned a personal library called Proteus into a successful commercial product. By purchasing that book, you will get access to the code of that library.

 

If you have never worked with Delphi, the second and third book is NOT for you! Please read the first book, then return to this one to complete your skills.

Always focused on code-safety

This is not the regular “programmer’s manual” kind of book where I show you the basics of programming and keep circling around simple code examples. The main purpose of this book is to take you to the “meta” level of programming and beyond that. The “safety first” concept strengthened in the first books, is continued here.

We will learn in the background to achieve code modularization thorough libraries, how to avoid circular references, how to build stable programs through reliable code, how to avoid typical programming pitfalls, how to turn banal Delphi features into an error-prevention system, and why some programming concepts are safer than others. Oh boy… we will have fun!

How is this book written?

The pace of this book is allegro, because there are many things to learn and only a limited amount of paper to write them down.

The tone will be non-conformist, light and funny, with jokes here and there and quite some colorful language.

 

Most important, in my books I will never ask you just to believe me. I hate the “because I said so” mentality. Instead, I will back up my statements with hard evidence: links to white papers, scientific articles, proved concepts, code examples, benchmark tests.

 

I spent six years writing the “Delphi in all its glory” series. I hope I did it right. I am a self-taught English speaker, so my English is not perfect, but I don’t care much about this. What I truly care about is the correctitude of information provided in this book.

 

If you have feedback about this book, or you’re as geek and passionate about programming as I am, I invite you to not only to explore the contents of this book but also to actively discuss with me. I am always open to exchange of ideas.

So, find me at  www.GabrielMoraru.com.

Environment-responsible

In order to reduce the amount of paper used, I tried to increase the information density. Even though it might not look as aesthetic as other books which waste 25% of the page with headers and footers and 2-line spacings, I feel good because I know I did my best to preserve our forests. I love the planet that I live on, and I want to keep it green.

Part of the paper used in this book is recycled paper.

 

Most books are read once, then placed onto a book shelve and kept there until the pages turn yellow. Once you are done with this book, please consider lending it to your friends, donating it to a library or even reselling it.

 

Who am I?

Embark on a journey through Delphi programming with me, Gabriel Moraru, a senior software architect with almost three decades of experience in programming. I’ve been immersed in the world of Pascal and Delphi since 1995 but I actually started as a Z80 programmer, back in the day when the all 8-bit magic started. My academic background in applied electronics and computer science has laid the foundation for crafting innovative software and hardware solutions.

My expertise spans medical, big data, e-commerce, bioinformatics, and robotics domains, reflecting a passion for versatile creation.

An active member in the Stack Overflow community, I enjoy sharing knowledge and I am contributing to the Pascal ecosystem with free and commercial Delphi libraries.

 

This is my fourth book, with (at least) one more on the horizon.

Pre-requisites for this book

  1. Delphi 10.4 (or 11 or 12) Community edition, which is free.

Older Delphi versions will also do it, but you will have to make tiny-tiny changes to the code since in this book I use the new inline var declarations (introduced in Delphi 10.4).

  1. The first book did not require any Delphi programming skills. However, is book is written with the supposition that you have some basic Delphi skills or that you already read the first book!

Let’s start

I hope you will enjoy as much fun as I enjoyed writing this book. I had fun and I also learned along the way of this six-year journey.

Without further ado, let us embark on this expedition destined to hone our mastery in the art of Delphi application development. Our odyssey commences forthwit


Table of content

1. Overview 3
Intended Audience 3
Always focused on code-safety 4
How is this book written? 4
Environment-responsible 5
Who am I? 6
Pre-requisites for this book 6
Let’s start 6
2. Table of content 7
Symbols and conventions 15
3. Introduction 16
Why do we need libraries? 16
You could survive without libraries… if you develop a single, small program 17
4. What is a library 18
5. About libraries and licenses 20
Meaning of the fields 21
Discussions 23
GPL 23
LGPL – Dynamic vs static linking 23
Apache 23
Mozilla Public License 24
CC, MIT, BSD 24
Conclusions 24
6. The four rules of packages 25
Rule #1: Always separate your business-logic code from the GUI code. 25
Rule #2: There should be no floating units. 25
Rule #3: A unit can belong to a project (dpr, dpk) and only one project. 25
Rule #4: The unit of a low-level library cannot access a unit in a higher-level library. 25
7. Library architecture 27
Circular references 30
It is all about aliens and pyramids 31
When to start being a pharaoh? 31
8. The anatomy of a Delphi package 32
DPK 33
The “Contains” section 33
The “Requires” section 34
A unit can be contained by only one package 36
How do we fix this? 38
BPL/DCP 40
Where are the DCU, DCP & BPL files needed? 40
Design time BPLs 40
Chained packages 41
Changing the BPL folder 42
Project groups 44
Compiler Directives 45
9. Delphi’s framework
The RTL library 46
The VCL library 47
10. Compiler paths
Path precedence 49
Paths are unique for each platform 50
Environment variables 51
$(BDS) 51
$(ProductVersion) 51
$(Platform) 51
$(Config) 52
$(Auto) 52
Library paths 55
1. Delphi libraries 55
2. Third party libraries 56
3. Our own libraries 58
Do we really need Library Paths? 58
Search Paths 60
Abusing the search paths in packages 60
Abusing the search paths in projects 60
Under-development libraries 61
Global vs local paths 62
Recap 62
Other paths 64
Browsing path 64
Release vs Debug DCUs 65
Project paths 67
11. RTL/VCL is precompiled
Precompiling our own files 71
When to precompile our files? 71
The “Package output directory” fields 72
12. How to install a library?
Installing a library manually 74
1. Open the package 74
2. Install the package 74
3. Let the compiler find the library 75
4. Confirm that the library was installed correctly 77
Installing a library delivered without PAS files 78
Single-file libraries 79
What to do when things go south? 80
Library folder structure 81
Installing libraries via Setup 82
Creating our own installer 82
IDE plugins 83
Trivia 83
Installing libraries via GetIt 84
How to uninstall a library 85
13. Runtime/Designtime libraries
Run-time libraries 87
Design time libraries 88
Design time vs Runtime 89
Including a library in our project 91
14. Linking with runtime packages
Drawbacks 93
15. How does the linker optimize unused code?
Experiment calibration 98
Proceedings 99
Experiment 1 99
Experiment 2 99
Experiment 3 100
Experiment 4 100
Experiment 5 103
Results 104
Extension to the experiment 105
The design time package 105
Some definitions and clarifications 106
16. Recompiling parts of the VCL
1. Class hack 107
2. Recompile the patched VCL file 107
17. Why libraries are mandatory for large projects?
Logic separation 111
Business logic 111
The GUI code 111
Code separation 111
Parentheses – Image recognition algorithm 113
Applicability 113
Reusability of the code 115
Avoiding circular references 115
18. The chaos of monolithic projects
The Breaking Point 117
The Refractory Ignition Spark 117
A Better Way: Modular Code via Libraries 118
Overcoming Preconceptions 118
19. Using C/++ libraries in Delphi
When you have the source code 120
When you have the DLL 120
20. Package initialization
The Register procedure 126
21. Popular libraries
FastMM4 127
Graphics32 127
Delphi LightSaber 127
Jedi 128
TMS 129
LightSaber 130
22. Proteus Licensing System
What is Proteus? 140
Trial license 140
Rent license (aka subscription) 141
Permanent license 141
Modules 141
Free Proteus license for you 141
What can Proteus do for you? 142
Automates the process of selling your app. 142
Prevents fraud 142
HardwareID-based keys 14
Proteus can even do marketing 143
How to use it? (Overview) 144
Typical usage scenario 144
Is it difficult to integrate Proteus into my app? 144
Definitions 146
Certificate 146
Unlock key 147
Library structure 152
Classes 153
TProteus 153
RCertificate 153
Support code 155
Self protection 156
Under the hood 156
Anti-tampering system 156
Stolen keys 156
How to add a ‘Stolen keys’ list to your project? 157
Amnesty key 157
Certificate ID 158
Entering the same key twice 158
Active certificate 160
Initialization 160
Setting up your app – Step by step guide 161
Demo program 163
23. Building a visual component
General steps for creating a visual component 164
Writing our first visual component 165
24. All good things must come to an end
Where do you go from here? 174
Delphi sample projects 174
Your next big project could be a game 175
Ask the artificial intelligence 175
Books 178
Notes and credits 180

Scroll to Top