Thema: Delphi einfaches plugin-system

Einzelnen Beitrag anzeigen

MathiasSimmack
(Gast)

n/a Beiträge
 
#9
  Alt 24. Jul 2002, 07:08
Ich habe mal die unendlichen Weiten meiner HD durchwühlt und folgendes Beispiel eines Plugins gefunden, was IMHO richtig interessant ist und als Ansatzpunkt für eigene Dinge benutzt werden kann. (Ich wusste doch, dass ich so was rumliegen habe.) Das Beispiel stammt von William Yang. Ich habe - soweit ich´s für erforderlich hielt - Kommentare eingesetzt. Ansonsten ist aber nichts verändert!


1. Das Programm, dass die Plugins lädt, exportiert selbst eine Funktion, die den ausgewählten Text eines Memos ändert. Das Folgende steht im Projektquelltext (*.dpr):
Code:
procedure ReplaceText(Text: PChar); far;
begin
  Form1.Memo1.SelText := StrPas(Text);
end;

exports ReplaceText;

2. Die Plugin-DLL besteht nur aus den paar Zeilen:
Code:
library First;

{ Important note about DLL memory management: ShareMem must be the
  first unit in your library's USES clause AND your project's (select
  View-Project Source) USES clause if your DLL exports any procedures or
  functions that pass strings as parameters or function results. This
  applies to all strings passed to and from your DLL--even those that
  are nested in records and classes. ShareMem is the interface unit to
  the DELPHIMM.DLL shared memory manager, which must be deployed along
  with your DLL. To avoid using DELPHIMM.DLL, pass string information
  using PChar or ShortString parameters. }

uses
  SysUtils,
  Classes,
  Windows;

type
  //Turn the optimize and huge string option off
  //if Delphi cannot compile your plugin
  // you will see when your have some compilicated typecasts
  iReplace = procedure (Text: PChar);

var
  OwnerApp: Integer;


function GetName: Pchar; far;
begin
  // Das ist der Eintrag, der später im Menü des Hauptprogramms
  // stehen wird
  Result := '&First Test';
end;


// hier ruft die DLL die "ReplaceText"-Funktion aus dem Hauptprogramm
// auf und ersetzt den ausgewählten Text im Memo durch einen fixen
// Text
procedure InsertText; far;
begin
  iReplace(GetProcAddress(OwnerApp, 'ReplaceText'))('Magic Plugins');
end;

procedure Init(Owner: Integer); far
begin
  OwnerAPP := Owner;
end;

exports
  GetName, InsertText, Init;

begin
end.

3. Im Formularcode wird jetzt ein solches Plugin geladen und in eine eigene Klasse geschrieben. Bevor ihr loslegt: ihr braucht noch ein Memo und ein Hauptmenü mit dem Eintrag "Plugin". Vorhanden? Dann kommt das:

[code:1:68de408964]var
Plugins: TList;

type
//plug in object
TTestPlugIn = class
Name: String;
Address: Integer;
Call: Pointer;
end;

//type cast of plug in functions
GetNameFunction = function : PChar;
InserText = procedure;
//HInstance is the application's histance, send it to the
//plugin so it can use the exports function from here.
PlugInInit = procedure (Owner: Integer);

var
StopSearch : boolean;

// btw: noch eine Variante zum Dateiensuchen ;o)
procedure SearchFileExt(const Dir, Ext: String; Files: TStrings);
var
Found: TSearchRec;
Sub: String;
i : Integer;
Dirs: TStrings; //Store sub-directories
Finished : Integer; //Result of Finding
begin
StopSearch := False;
Dirs := TStringList.Create;
Finished := FindFirst(Dir + '*.*', 63, Found);
while (Finished = 0) and not (StopSearch) do
begin
//Check if the name is valid.
if (Found.Name[1] <> '.') then
begin
//Check if file is a directory
  Mit Zitat antworten Zitat