麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > Delphi > 正文

插件管理框架 for Delphi(二)

2020-01-31 20:52:35
字體:
來源:轉載
供稿:網友
unit untDllManager;

interface

uses
  Windows, Classes, SysUtils, Forms;

type

  EDllError = Class(Exception);

  TDllClass = Class of TDll;
  TDll = Class;

  TDllEvent = procedure(Sender: TObject; ADll: TDll) of Object;

  { TDllManager
    o 提供對 Dll 的管理功能; 
    o Add 時自動創建 TDll 對象,但不嘗試裝載;
    o Delete 時自動銷毀 TDll 對象;
  }

  TDllManager = Class(TList)
  private
    FLock: TRTLCriticalSection;
    FDllClass: TDllClass;
    FOnDllLoad: TDllEvent;
    FOnDllBeforeUnLoaded: TDllEvent;
    function GetDlls(const Index: Integer): TDll;
    function GetDllsByName(const FileName: String): TDll;
  protected
    procedure Notify(Ptr: Pointer; Action: TListNotification); override;
  public
    constructor Create;
    destructor Destroy; override;
    function Add(const FileName: String): Integer; overload;
    function IndexOf(const FileName: String): Integer; overload;
    function Remove(const FileName: String): Integer; overload;
    procedure Lock;
    procedure UnLock;
    property DllClass: TDllClass read FDllClass write FDllClass;
    property Dlls[const Index: Integer]: TDll read GetDlls; default;
    property DllsByName[const FileName: String]: TDll read GetDllsByName;
    property OnDllLoaded: TDllEvent read FOnDllLoad write FOnDllLoad;
    property OnDllBeforeUnLoaded: TDllEvent read FOnDllBeforeUnLoaded write FOnDllBeforeUnLoaded;
  end;

  { TDll
    o 代表一個 Dll, Windows.HModule
    o 銷毀時自動在 Owner 中刪除自身;
    o 子類可通過覆蓋override DoDllLoaded, 以及DoDllUnLoaded進行功能擴展;
  }

  TDll = Class(TObject)
  private
    FOwner: TDllManager;
    FModule: HMODULE;
    FFileName: String;
    FPermit: Boolean;
    procedure SetFileName(const Value: String);
    function GetLoaded: Boolean;
    procedure SetLoaded(const Value: Boolean);
    procedure SetPermit(const Value: Boolean);
  protected
    procedure DoDllLoaded; virtual;
    procedure DoBeforeDllUnLoaded; virtual;
    procedure DoDllUnLoaded; virtual;
    procedure DoFileNameChange; virtual;
    procedure DoPermitChange; virtual;
  public
    constructor Create; virtual;
    destructor Destroy; override;
    function GetProcAddress(const Order: Longint): FARPROC; overload;
    function GetProcAddress(const ProcName: String): FARPROC; overload;
    property FileName: String read FFileName write SetFileName;
    property Loaded: Boolean read GetLoaded write SetLoaded;
    property Owner: TDllManager read FOwner;
    property Permit: Boolean read FPermit write SetPermit;
  end;

implementation

{ TDll }

constructor TDll.Create;
begin
  FOwner := nil;
  FFileName := ´´;
  FModule := 0;
  FPermit := True;
end;

destructor TDll.Destroy;
var
  Manager: TDllManager;
begin
  Loaded := False;
  if FOwner <> nil then
  begin
    //在擁有者中刪除自身
    Manager := FOwner;
    //未防止在 TDllManager中重復刪除,因此需要將
    //FOwner設置為 nil; <-- 此段代碼和 TDllManager.Notify 需要配合
    //才能確保正確。 
    FOwner := nil;
    Manager.Remove(Self);
  end;
  inherited;
end;

function TDll.GetLoaded: Boolean;
begin
  result := FModule <> 0;
end;

function TDll.GetProcAddress(const Order: Longint): FARPROC;
begin
  if Loaded then
    result := Windows.GetProcAddress(FModule, Pointer(Order))
  else
    raise EDllError.CreateFmt(´Do Load before GetProcAddress of "%u"´, [DWORD(Order)]);
end;

function TDll.GetProcAddress(const ProcName: String): FARPROC;
begin
  if Loaded then
    result := Windows.GetProcAddress(FModule, PChar(ProcName))
  else
    raise EDllError.CreateFmt(´Do Load before GetProcAddress of "%s"´, [ProcName]);
end;

procedure TDll.SetLoaded(const Value: Boolean);
begin
  if Loaded <> Value then
  begin
    if not Value then
    begin
      Assert(FModule <> 0);
      DoBeforeDllUnLoaded;
      try
        FreeLibrary(FModule);
        FModule := 0;
      except
        Application.HandleException(Self);
      end;
      DoDllUnLoaded;
    end
    else
    begin
      FModule := LoadLibrary(PChar(FFileName));
      try
        Win32Check(FModule <> 0);
        DoDllLoaded;
      except
        On E: Exception do
        begin
          if FModule <> 0 then
          begin
            FreeLibrary(FModule);
            FModule := 0;
          end;
          raise EDllError.CreateFmt(´LoadLibrary Error: %s´, [E.Message]);
        end;
      end;
    end;
  end;
end;

procedure TDll.SetFileName(const Value: String);
begin
  if Loaded then
    raise EDllError.CreateFmt(´Do Unload before load another Module named: "%s"´,
      [Value]);
  if FFileName <> Value then
  begin
    FFileName := Value;
    DoFileNameChange;
  end;
end;

procedure TDll.DoFileNameChange;
begin
  // do nonthing.
end;

procedure TDll.DoDllLoaded;
begin
  if Assigned(FOwner) and Assigned(FOwner.OnDllLoaded) then
    FOwner.OnDllLoaded(FOwner, Self);
end;

procedure TDll.DoDllUnLoaded;
begin
  //do nonthing.
end;

procedure TDll.DoPermitChange;
begin
  //do nonthing.
end;

procedure TDll.SetPermit(const Value: Boolean);
begin
  if FPermit <> Value then
  begin
    FPermit := Value;
    DoPermitChange;
  end;
end;

procedure TDll.DoBeforeDllUnLoaded;
begin
  if Assigned(FOwner) and Assigned(FOwner.OnDllBeforeUnLoaded) then
    FOwner.OnDllBeforeUnLoaded(FOwner, Self);
end;

{ TDllManager }

function TDllManager.Add(const FileName: String): Integer;
var
  Dll: TDll;
begin
  result := -1;
  Lock;
  try
    if DllsByName[FileName] = nil then
    begin
      Dll := FDllClass.Create;
      Dll.FileName := FileName;
      result := Add(Dll);
    end
    else
      result := -1;
  finally
    UnLock;
  end;
end;

constructor TDllManager.Create;
begin
  FDllClass := TDll;
  InitializeCriticalSection(FLock);
end;

destructor TDllManager.Destroy;
begin
  DeleteCriticalSection(FLock);
  inherited;
end;

function TDllManager.GetDlls(const Index: Integer): TDll;
begin
  Lock;
  try
    if (Index >=0) and (Index <= Count - 1) then
      result := Items[Index]
    else
      raise EDllError.CreateFmt(´Error Index of GetDlls, Value: %d, Total Count: %d´, [Index, Count]);
  finally
    UnLock;
  end;
end;

function TDllManager.GetDllsByName(const FileName: String): TDll;
var
  I: Integer;
begin
  Lock;
  try
    I := IndexOf(FileName);
    if I >= 0 then
      result := Dlls[I]
    else
      result := nil;
  finally
    UnLock;
  end;
end;

function TDllManager.IndexOf(const FileName: String): Integer;
var
  I: Integer;
begin
  result := -1;
  Lock;
  try
    for I := 0 to Count - 1 do
      if CompareText(FileName, Dlls[I].FileName) = 0 then
      begin
        result := I;
        break;
      end;
  finally
    UnLock;
  end;
end;

procedure TDllManager.Lock;
begin
  OutputDebugString(Pchar(´TRLock DM´ + IntToStr(GetCurrentThreadId) + ´:´ + IntToStr(DWORD(Self))));
  EnterCriticalSection(FLock);
  OutputDebugString(Pchar(´Locked DM´ + IntToStr(GetCurrentThreadId) + ´:´ + IntToStr(DWORD(Self))));
end;

procedure TDllManager.Notify(Ptr: Pointer; Action: TListNotification);
begin
  if Action = lnDeleted then
  begin
    //若TDll(Ptr).Owner和Self不同,則
    //表明由 TDll.Destroy 觸發;
    if TDll(Ptr).Owner = Self then
    begin
      //防止FOwner設置為nil之后相關事件不能觸發
      TDll(Ptr).DoBeforeDllUnLoaded;
      TDll(Ptr).FOwner := nil;
      TDll(Ptr).Free;
    end;
  end
  else
  if Action = lnAdded then
    TDll(Ptr).FOwner := Self;
  inherited;
end;

function TDllManager.Remove(const FileName: String): Integer;
var
  I: Integer;
begin
  result := -1;
  Lock;
  try
    I := IndexOf(FileName);
    if I >= 0 then
      result := Remove(Dlls[I])
    else
      result := -1;
  finally
    UnLock;
  end;
end;

procedure TDllManager.UnLock;
begin
  LeaveCriticalSection(FLock);
  OutputDebugString(Pchar(´UnLock DM´ + IntToStr(GetCurrentThreadId) + ´:´ + IntToStr(DWORD(Self))));
end;

end.
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 日韩欧美精品电影 | 一本色道久久综合亚洲精品图片 | 亚洲国产综合在线观看 | 欧美一级视频免费看 | 精品呦女| 国产精品成人一区二区三区电影毛片 | 最近中文字幕一区二区 | 中文字幕在线播放视频 | 19禁国产精品福利视频 | 午夜视频导航 | 国产精品成人久久 | 一区二区久久久久草草 | 九色com| 久久6国产| 毛片免费视频播放 | 欧美亚洲一区二区三区四区 | 青青草成人影视 | 中国老女人一级毛片视频 | 国产一区二区亚洲 | 香蕉黄色网 | 成人福利在线看 | 国产精品99久久久久久宅女 | 国产成人在线视频播放 | 欧美一级黄色免费 | 特级毛片免费 | 婷婷久久影院 | 一区二区久久久久草草 | 国产视频在线观看免费 | 成年人福利视频 | 国产毛毛片一区二区三区四区 | 国产做爰全免费的视频黑人 | 久久精品中文字幕一区二区 | 色综合久久久久久久久久久 | 国产99视频在线观看 | 国产精品区一区二区三区 | 国产一区二区高清在线 | 亚洲一区二区在线 | 麻豆视频国产在线观看 | 亚洲精品久久久久久 | 成人一区二区三区四区 | 看黄在线 |