unit Main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus, PluginDef;
type
TLibraryData = record
Handle: Cardinal;
Plugin: TPlugin;
MenuItem: TMenuItem;
end;
TForm1 = class(TForm)
MainMenu1: TMainMenu;
mnuPlugins: TMenuItem;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
FPlugins: array of TLibraryData;
procedure LoadPlugin(DLLName: string);
procedure LoadPlugins;
procedure ExecutePlugin (Sender: TObject);
procedure UnloadPlugins;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses
Inifiles;
procedure TForm1.LoadPlugins;
var
PluginRegistryFile: string;
i: integer;
iniFile: TInifile;
Plugins: TStringlist;
begin
PluginRegistryFile := ExtractFilePath(Application.Exename)+'Plugins.ini';
if not FileExists (PluginRegistryFile) then
Exit;
iniFile := TIniFile.Create(PluginRegistryFile);
try
Plugins := TStringlist.Create;
try
IniFile.ReadSections(Plugins);
for i:= 0 to Pred(Plugins.Count) do
begin
if AnsiCompareText(iniFile.ReadString(Plugins[i],'type',''),'plugin') = 0 then
begin
LoadPlugin(iniFile.ReadString(Plugins[i],'dllname',''));
end;
end;
finally
Plugins.Free;
end;
finally
iniFile.Free;
end;
end;
procedure TForm1.LoadPlugin(DLLName: string);
var
TempHandle: Cardinal;
ProcAddr: Pointer;
LoadPluginProc: TLoadPlugin;
TempPlugin: TPlugin;
begin
if not ((Copy(DLLName,2,1) = ':') or (Copy(DLLName, 1,2) = '\\')) then
DLLName := ExtractFilePath(Application.Exename)+DLLName;
TempHandle := LoadLibrary (PChar(DLLName));
if (TempHandle <> INVALID_HANDLE_VALUE) and (TempHandle <> 0) then
begin
ProcAddr := GetProcAddress (TempHandle, 'LoadPlugIn');
if ProcAddr = nil then begin
FreeLibrary (TempHandle);
Exit;
end;
LoadPluginProc := TLoadPlugin(ProcAddr);
try
TempPlugin := nil;
LoadPluginProc(Application.Handle, TempPlugin);
except
FreeLibrary(TempHandle);
exit;
end;
SetLength(FPlugIns, Succ(Length(FPlugins)));
FPlugins[High(Fplugins)].Handle := TempHandle;
//Add the Plugin to the menu
with FPlugins[High(FPlugins)] do begin
Plugin := TempPlugin;
MenuItem := TMenuItem.Create(Self);
MenuItem.Caption := TempPlugin.GetMenuText;
MenuItem.Enabled := TempPlugin.GetEnabled;
MenuItem.Tag := High(FPlugins);
MenuItem.OnClick := ExecutePlugin;
end;
mnuPlugins.Add(FPlugins[High(Fplugins)].MenuItem);
end;
end;
procedure TForm1.ExecutePlugin (Sender: TObject);
begin
if Sender = nil then
exit;
if not (Sender is TMenuItem) then
exit;
(* if FPlugins[TMenuItem(Sender).Tag].Plugin <> nil then
FPlugins[TMenuItem(Sender).Tag].Plugin.Execute; *)
if FPlugins[(Sender as TMenuItem).Tag].Plugin <> nil then
FPlugins[(Sender as TMenuItem).Tag].Plugin.Execute;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
SetLength(FPlugins, 0);
LoadPlugins;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
UnloadPlugins;
end;
procedure TForm1.UnloadPlugins;
var i: integer;
begin
if Length(FPlugins) > 0 then
begin
for i:= 0 to Pred(Length(FPlugins)) do
try
if FPlugins[i].MenuItem <> nil then
FPlugins[i].MenuItem.Free;
if FPlugins[i].Plugin <> nil then
FPlugins[i].Plugin.Free;
FreeLibrary(FPlugins[i].Handle);
except
end;
end;
end;
end.