📄 pluginmanager.pas
字号:
(*
# (C) Copyright 2004
# Miha Vrhovnik, miha.vrhovnik@cordia.si
#
# The contents of this file are subject to the Mozilla Public License
# Version 1.1 (the "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/MPL-1.1.html
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either expressed or implied. See the License for
# the specific language governing rights and limitations under the License.
#
# The Initial Developer of the Original Code is Miha Vrhovnik (Slovenia).
# Portions created by Miha Vrhovnik are Copyright (c)2004.
# All Rights Reserved.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
#==============================================================================
# Contributor(s):
#==============================================================================
# Version History:
#==============================================================================
*)
unit pluginManager;
interface
uses
SysUtils, Classes, Windows, Forms, BasePluginInterface;
type TPluginManager = class
private
FPluginFolder: String;
FList: TList;
function GetCount: Integer;
function GetPlugin(Index: Integer): IsiMailBasePlugin;
public
constructor Create;
destructor Destroy; override;
function AddCustomPlugin(plugin: IsiMailBasePlugin): Boolean;
procedure LoadPlugins;
procedure LoadPlugin(FileName: string);
property Plugin[Index: Integer]: IsiMailBasePlugin read GetPlugin;
published
property Count: Integer read GetCount;
property PluginFolder: String read FPluginFolder write FPluginFolder;
end;
type TPluginInfo = class(TObject)
public Handle: HINST; PlugIn: IsiMailBasePlugin;end;
type ELoadPluginError = class(Exception);
implementation
{ TPluginManager }
function TPluginManager.AddCustomPlugin(plugin: IsiMailBasePlugin): Boolean;
var PI: TPluginInfo;
begin
PI := TPluginInfo.Create;
PI.PlugIn := plugin;
FList.Add(PI);
end;
constructor TPluginManager.Create;
begin
inherited Create;
FList := TList.Create;
end;
destructor TPluginManager.Destroy;
var i: Integer;
var PI: TPluginInfo;
begin
for i := 0 to FList.Count - 1 do begin
PI := FList.Items[i];
PI.PlugIn := nil;
FreeLibrary(PI.Handle);
FreeAndNil(PI);
end;
FreeAndNil(FList);
inherited Destroy;
end;
function TPluginManager.GetCount: Integer;
begin
Result := FList.Count;
end;
function TPluginManager.GetPlugin(Index: Integer): IsiMailBasePlugin;
begin
Result := TPluginInfo(FList.Items[Index]).PlugIn;
end;
procedure TPluginManager.LoadPlugin(FileName: string);
type
TRegisterPlugin = function: IsiMailBasePlugin; stdcall;
var LibHandle: Integer;
var RegisterProc: TRegisterPlugin;
var PlugIn: IsiMailBasePlugin;
begin
LibHandle := 0;
LibHandle := LoadLibrary(PChar(FileName));
if LibHandle = 0 then
raise ELoadPluginError.CreateFmt('Couldn''t load plug-in: %s', [FileName]);
try
RegisterProc := GetProcAddress(LibHandle, 'RegisterPlugin');
if not Assigned(RegisterProc) then raise ELoadPluginError.CreateFmt('Couldn''t find ''%s'' function in plug-in: %s', ['RegisterPlugin', FileName]); // initialize the plugin and add it to list PlugIn := RegisterProc; if AddCustomPlugin(PlugIn) then begin TPluginInfo(FList.Last).Handle := LibHandle; end; except RegisterProc := nil; FreeLibrary(LibHandle); end;
end;
procedure TPluginManager.LoadPlugins;
var FileName: string;
var Found: Integer;var Path: string;var Sr: TSearchRec;begin // if the PluginPath is blank, we load from the app's folder. if FPluginFolder = '' then Path := ExtractFilePath(Application.ExeName) else Path := FPluginFolder; Path := IncludeTrailingPathDelimiter(Path); try try Found := FindFirst(Path + '*.dll', faAnyFile, Sr); while Found = 0 do begin FileName := Sr.Name; //! If one plugin made problems -> no other plugins where loaded //! To avoid that the try-except block was wrapped around here... try LoadPlugin(Path + FileName); except end; Found := FindNext(Sr); end; except on E: Exception do raise; end; finally SysUtils.FindClose(Sr); end;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -