Delphi – Loading a Delphi Object Run Time using BPL

bpldelphidllpackage

I have a class in a unit. Usually, when I changed the algorithm of its methods, I have to recompile it and deliver the patch as a whole.
I think to create the instance of the class using DLL. After searching in delphi.about.com, I found that instead of using DLL, I can use BPL. It is a DLL for Delphi. The problem is almost all examples I found is only telling how to export a function.
I want to dynamically load the BPL, and whenever I replace the BPL, I can get the latest algorithm of the class, not only the functions I export.

Article I have read:
http://delphi.about.com/od/objectpascalide/a/bpl_vs_dll.htm
Plugins system for Delphi application – bpl vs dll?
http://delphi.about.com/library/weekly/aa012301a.htm

Any URL or SAMPLE how to create a BPL from scratch to encapsulate a component or a class is greatly appreciated.


Dear Guru,

Suppose I have code like this:

unit unitA;

interface

type
  B = class(TObject)
  public
    procedure HelloB;
  end;

  A = class(TObject)
  public
    function GetB: B;
    function HelloA: String;
    procedure Help;
  end;

  implementation

  uses
      Dialogs;

  { B }

   procedure B.HelloB;
   begin
     ShowMessage('B');
   end;

  { A }

  function A.GetB: B;
  begin
    Result := B.Create;
  end;

  function A.HelloA: String;
  begin
    Result := 'Hello, this is A';
  end;

  procedure A.Help;
  begin
    //do something
  end;

  end.

I want to export all public methods of A. How to make it a DLL?
How to use it from another unit where to import it?
let's say:

 var a: A;

 a := A.Create;
 a.GetB;
 showMessage(a.HelloA);

A is not declared in the unit (it is in the DLL).
Please advise.


Hurray. I got it last night. All I have to do is make the object implement an interface which is used in the caller unit to catch the instance of object returned by the DLL.

Thank you all.

Best Answer

Mason nailed it already, but let me elaborate on why BPLs aren't what you are looking for. BPLs are a means for the Delphi IDE to load components that share the same memory manager and RTL. (Type identity works almost transparently using BPLs)

However, the dependencies you are getting tied up in are almost always unacceptable. Except for the IDE, which cannot handle different versions of RTL and VCL anyway.

When you pass only interface references between your application and its DLLs, then you don't have to share RTL, VCL or shared packages at all.

It also means that you could write some DLLs in another language (C++, C#, FPC, another Delphi version), and still use objects. Which can be tempting when you don't want to port your main app but still want to use existing libraries that are not available for Delphi, or your version of Delphi.

Related Topic