Board index » delphi » Dynamic Package linking in order to write Plug-Ins

Dynamic Package linking in order to write Plug-Ins

Hello,
I would like link my application with the .bpl file dynamically in order
that someone can write a plugin. If the plugin would be a dll I would call
it with the main windows handle. But I heared that you can even make the
plugins as .bpls so you can program your form as always and dont even need
to give the handle.

Anyone knows where to get information, tutorials or an example of this?

Alex

 

Re:Dynamic Package linking in order to write Plug-Ins


Hi !

.bpl's have to be known designtime, because the compiler generates all
the code that you usually write yourself, in addition it generates the
definition of the entry functions/procedures.

--
Bjoerge Saether
Consultant / Developer
Asker, Norway
bsaether.removet...@online.no (remove the obvious)

Alex Sab skrev i meldingen <85448m$kp...@news.cs.tu-berlin.de>...

Quote
>Hello,
>I would like link my application with the .bpl file dynamically in
order
>that someone can write a plugin. If the plugin would be a dll I would
call
>it with the main windows handle. But I heared that you can even make
the
>plugins as .bpls so you can program your form as always and dont even
need
>to give the handle.

>Anyone knows where to get information, tutorials or an example of this?

>Alex

Re:Dynamic Package linking in order to write Plug-Ins


(sorry, I can't see the original posting)

"Bj?rge S?ther" schrieb:

Quote

> Hi !

> .bpl's have to be known designtime, because the compiler generates all
> the code that you usually write yourself, in addition it generates the
> definition of the entry functions/procedures.

No, packages can be linked at runtime just like normal DLLs. See
LoadPackage() and UnloadPackage() in the OH. I used this already to
implement a kind of plugin mechanism.

-Michael

Re:Dynamic Package linking in order to write Plug-Ins


When you're talking about plugins, do you mean "applets" written in
component form ?
It wouldn't be very useful for a standard function library, would it ?

Thanks for correcting me and sorry for misleading, now you got me really
curious, too...;-)

--
Bjoerge Saether
Consultant / Developer
Asker, Norway
bsaether.removet...@online.no (remove the obvious)

Michael Winter skrev i meldingen <3875C12D.54A7...@aquila.de>...

Quote
>(sorry, I can't see the original posting)

>"Bj?rge S?ther" schrieb:

>> Hi !

>> .bpl's have to be known designtime, because the compiler generates
all
>> the code that you usually write yourself, in addition it generates
the
>> definition of the entry functions/procedures.

>No, packages can be linked at runtime just like normal DLLs. See
>LoadPackage() and UnloadPackage() in the OH. I used this already to
>implement a kind of plugin mechanism.

>-Michael

Re:Dynamic Package linking in order to write Plug-Ins


"Bj?rge S?ther" schrieb:

Quote

> When you're talking about plugins, do you mean "applets" written in
> component form ?
> It wouldn't be very useful for a standard function library, would it ?

> Thanks for correcting me and sorry for misleading, now you got me really
> curious, too...;-)

In the current project I'm working on we are dealing with a number of
financial mathematic operations. Imagine a database view (altough our
data are in the memory) and some aggregate functions which calculate,
say, sums, averages, and so on. For some reasons we do not use SQL
calculations. The whole calculation process is based on scipts, so the
customers can insert their own changes. Now, we are not sure that we
meet all *future* requirements of our customers. Maybe one day they want
a calculatation we can not imagine today. That calculation would be
possible using Pascal, but not using our predefined selections and
aggregates. That's why and because of we didn't want to change our code
and recompile every time we implemented an interface for 'custom
calculations'. We (or the customer self) write a simple .bpl (using a
defined set of data access methods already in a runtime package [which
is used by our .exe, too]). That .bpl must be placed in a special
directory, which is scanned by the .exe on startup. It has to export one
procedure only called Register_MIG_Plugin. This procedure is called by
the .exe after loading and declares the new calculation function(s) to
the .exe. After that, the function(s) can be used by the script.

In Plugin.pas:

uses MIGDataAccess;

function Test(Positions: TPositionList; aCol: Integer): TVar;
begin
  // do calculation here
  Result:=CreateVar(123);
end;

procedure Register_MIG_Plugin;
begin
  AddAggFunc('Test', Test, nil, nil, false);
end;

exports
  Register_MIG_Plugin;

In the main program:

procedure AddAggFunc(const Name: String; CalcFunc: TAggCalcFunc;
  ExplFunc: TAggExplFunc; ColListFunc: TAggColListFunc; RqCol: Boolean);
var i: Integer;
begin
  i:=AggFuncDictionary.Search(@Name);
  if i>=0 then raise EMIGData.CreateFmt(SAggFuncDuplicateDefinition,
[Name]);
  AggFuncDictionary.Add(Name, CalcFunc, ExplFunc, ColListFunc, RqCol);
end;

on startup,
  for each *.bpl in do LoadPlugin(FileName)

procedure LoadPlugin(const Path: String);
var
  H: HMODULE;
  R: procedure;
begin
  try
    H:=LoadPackage(Path);
    R:=GetProcAddress(H, 'Register_MIG_Plugin');
    try
      if not Assigned(R) then raise Exception.Create(SPluginInvalid);
      R;
      PluginList.Add(H);
    except
      UnloadPackage(H);
      raise;
    end;
  except
    on E:Exception do raise EMIGData.CreateFmt(SPluginError, [Path,
E.Message]);
  end;
end;

-Michael

Re:Dynamic Package linking in order to write Plug-Ins


In article <85448m$kp...@news.cs.tu-berlin.de>,
  "Alex Sab" <mraa...@gmx.de> wrote:

Quote
> Hello,
> I would like link my application with the .bpl file dynamically in
order
> that someone can write a plugin. If the plugin would be a dll I would
call
> it with the main windows handle. But I heared that you can even make
the
> plugins as .bpls so you can program your form as always and dont
even need
> to give the handle.

> Anyone knows where to get information, tutorials or an example of
this?

> Alex

There's a great freeware component that will help you accomplish that
behavior with ease. It was created by Alexander Mehlhorn and you can
find it at:

http://www.tritontools.com

Look under "Tools and components" if you scroll down you'll see:

"The AHM Package Solution" that link will take you to the intro doc.
in the download section you'll find the free download called:

"AHM Package Solution for Delphi 3.0 and Delphi 4.0"

Don't hesitate to try it! I've used it and its very easy and powerful!

Also take a look at the other components there. They're very useful!

------------------------
H.Gomez
QuadraSoft International

Sent via Deja.com http://www.deja.com/
Before you buy.

Other Threads