The easy way to use functions from a package is to "use" the unit that contains the function, call it as usual, and put the package on the list of your project's runtime packages. For that to work, there are a few requirements:
- Your project must use the same Delphi version as was used to compile the package.
- You must have access to the DCU file for the unit, or at least the DCP file for the package.
- The package must exist in the operating system's search path when your program starts.
If you can't satisfy the third requirement, or if you don't want to have the package loaded all the time, then you can call LoadPackage
for it instead. The way to make that work is to have another package that is loaded all the time. It will be used by both your project and the package you wish to load. The intermediate package will expose an interface (such as some registration functions, a variable, or a class) that the main package can use to tell the application what its functions are. You won't be able to "use" the main package's unit in your application directly.
If you can't satisfy the first two requirements, then there is the much harder way, which is also what you'd need to do if your application isn't written in Delphi or C++ Builder. Treat the package like an ordinary DLL. Load it with LoadLibrary
. Use GetProcAddress
to load its Initialize
function, and then call it. (Remember that the calling convention is register
, not stdcall
.) Then load the address of the function you wish to call, keeping in mind that the name of the function has been mangled to include some unit and type information. Call the Finalize
function before you call FreeLibrary
. Check the source for LoadPackage
and UnloadPackage
; whether you need to call CheckForDuplicateUnits
probably depends on whether you can satisfy requirement number 1.