Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I call Delphi functions in a bpl from an executable?

Tags:

delphi

bpl

I have a Delphi application that I have written a fairly simple wrapper .exe for.

Basically, there was a dll that had a bunch of functions, one of which I would call iteratively once my wrapper did what it needed to. I am not in control of this dll file, and will never be.

Well, now this DLL is a BPL, and I'm not sure how to call functions within that file. Thanks in advance.

like image 852
Dan Avatar asked Sep 08 '09 14:09

Dan


People also ask

What is a BPL file Delphi?

Dynamic-link library (DLL) used by Delphi, a software development application; may contain a run-time package for compiled applications, or may store a component that extends the Delphi IDE; can be shared across multiple projects.

What are packages in Delphi?

A package is a specially compiled library used by applications, the IDE, or both. Packages allow you to rearrange where code resides without affecting the source code. This is sometimes referred to as application partitioning. Runtime packages provide functionality when a user runs an application.


1 Answers

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:

  1. Your project must use the same Delphi version as was used to compile the package.
  2. You must have access to the DCU file for the unit, or at least the DCP file for the package.
  3. 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.

like image 153
Rob Kennedy Avatar answered Oct 03 '22 10:10

Rob Kennedy