I have Delphi application .exe built with Runtime packages. I need to store exe and other bpls/dlls in separate (relative path) folders. like, .exe will be in one folder and all other run time packages dll/bpls in another folder. How to achieve this? If I do this on another machine where delphi is not installed, it is giving usual errors of required packages.
-
Precise and informative titles help in searching questions and answering the questions quickly. Also please elaborate what efforts you did achieve your goal. – Tauseef May 03 '17 at 07:34
-
Put all the packages in the same directory as the executable – David Heffernan May 03 '17 at 11:00
-
In same directory it is working fine. But my requirement is to put all the packages in **one directory** and executable **.exe** in **another directory** i.e. if **.exe** is in dir **c:\ClientApp** packages will be in dir **c:\ClientApp\Packages** – Soham May 04 '17 at 09:57
1 Answers
When you compile the EXE with Runtime Packages enabled, you must deploy any used BPL files along with your EXE.
If the EXE is static-linked to the BPLs at compile-time, they must exist where the OS can find them when the EXE is being loaded, before it actually runs. You will have to put the BPLs in the same folder as the EXE, or in a folder that is included in the system's PATH
environment variable.
If the EXE is not static-linked to the BPLs, and they are loaded by the EXE code at runtime using the RTL's LoadPackage()
function, then the best option is to use absolute paths when loading them. If that is not an option, and you must use relative paths, then you can use SetDllDirectory()
or AddDllDirectory()
to add the BPL folder to the OS search path before then calling LoadPackage()
.
Read the MSDN documentation on how Windows locates DLLs at runtime (BPLs are just DLLs with built-in Delphi RTL support):

- 555,201
- 31
- 458
- 770
-
As you mentioned *If the EXE is not static-linked to the BPLs, and they are loaded by the EXE code at runtime using the RTL's LoadPackage() function, then the best option is to use absolute paths when loading them*.... How to use absolute paths when loading them? can you please elaborate? Is there any option to locate a path and put all required packages in that path. – Soham May 04 '17 at 07:15
-
@Soham if the BPL folder path is relative to the EXE folder path, then look at [Conversion between absolute and relative paths in Delphi](http://stackoverflow.com/questions/5329472/). You can get the EXE folder path using `ExtractFilePath(ParamStr(0))`. – Remy Lebeau May 04 '17 at 07:37
-
In same directory it is working fine. But my requirement is build application with option **Build with run time packages** and to put all the packages in one directory and executable **.exe** in another directory i.e. if .exe is in dir c:\ClientApp packages will be in dir c:\ClientApp\Packages – Soham May 04 '17 at 10:38
-
With this folder structure **(if .exe is in dir "c:\ClientApp" packages will be in dir "c:\ClientApp\Packages")** should run on the machine where delphi is not installed. – Soham May 04 '17 at 11:57
-
@Soham `ExePath := ExtractFilePath(ParamStr(0)); LoadPackage(ExePath+'Packages\filename.bpl');` But this only works for dynamic loading. For static linking, you will have to add `C:\ClientApp\Packages` to the system `PATH`. – Remy Lebeau May 04 '17 at 15:00
-
All together there are 430 bpl files. Adding system path is one of the last option. I wanted to check other than system path option as it is not feasible for client. Anyways, thanks a lot for quick response. – Soham May 05 '17 at 09:57
-
@Soham you only have to add the **folder** to the system `PATH`, not the **individual files**. If they are all in 1 folder, that is only 1 entry to add. – Remy Lebeau May 05 '17 at 14:04
-
Is it even possible to make an executable dynamically load all required packages? What about the rtl / vcl packages? Can they be loaded dynamically? – dummzeuch May 19 '17 at 12:59