2) 插件信息數據結構
定義一個記錄插件相關信息的數據接口可以集中控制DLL插件。在Interface部分加入如下代碼:
type
//定義插件信息類
TMyPlugins = class
Caption:String; //DLL窗體標題
DllFileName:String; //DLL文件路徑
WndClass:String; //窗體標識
UserID:string; //用戶名
ProcAddr:THandle; //LOADLIBRARY載入的庫句柄
FuncAddr:Pointer; //SHOWDLLFORM函數指針
FuncFreeAddr:Pointer; //FREEDLLFORM函數指針
end;
……
為每個插件創建一個TMyPlugins的實例,下文會討論對這些實例的初始化方法。
3) 插件載入函數
在本示例中DLL窗口是在HALL中觸發打開子窗口的事件中載入並顯示的。按鈕事件觸發後,先根據插件結構體實例判斷DLL是否已經加載,如果已經加載,則控制窗口的顯示或關閉;如果沒有加載則訪問數據表將字段賦值到插件結構體中,然後執行載入、獲得指針的工作。
局部代碼如下
……
//-----------------------------------------
//Name: OpenPlugin
//Func: 插件信息類控制過程: 初始化==》設置權限==》載入DLL窗口
//Para: APlugin-TMyPlugins; sAlias別名; iFuncValue權限值
//Rtrn: N/A
//Auth: CST
//Date: 2005-6-2
//-----------------------------------------
procedure TFormHall.OpenPlugin(AFroMactn: TAction ;APlugin:TMyPlugins; sAlias:string; sUserID:string);
var hWndPlugin:HWnd;
begin
//判斷插件窗口是否已經載入 hWndPlugin:=FindWindow(PChar(APlugin.WndClass),nil);
if hWndPlugin <> 0 then //插件窗口已經載入
begin
if not IsWindowVisible(hWndPlugin) then
begin
AFroMactn.Checked := True;
ShowWindow(hWndPlugin,SW_SHOWDEFAULT); //顯示
end
else
begin
AFroMactn.checked := False;
ShowWindow(hWndPlugin,SW_HIDE) ;
end;
Exit; //離開創建插件過程
end;
//初始化插件類實例
if not InitializeMyPlugins(APlugin,sAlias) then
begin
showmessage(’初始化插件類錯誤。’);
exit;
end;
//獲得當前權限值
APlugin.UserID := sUserID;
//載入DLL窗口
if not LoadShowPluginForm(APlugin) then
begin
showmessage(’載入中心插件出錯。’);
exit;
end;
end;
//-----------------------------------------
//Name: InitializeMyPlugins
//Func: 初始化MYPLUGIN實例 (Caption | DllFileName | IsLoaded)
//Para: APlugin-TMyPlugins
//Rtrn: N/A
//Auth: CST
//Date: 2005-6-2
//-----------------------------------------
function TFormHall.InitializeMyPlugins(APlugin:TMyPlugins; sAlias:String):Boolean;
var
strSQL:string;
myDA:TMyDataAdapter;
begin
Result:=False;
myDA:=TMyDataAdapter.Create;
strSQL:=’SELECT * FROM SystemModuleList WHERE modAlias=’+QuotedStr(sAlias);
try
myDA.RetrIEveData(strSQL);
except
on E:Exception do
begin
result:=false;
myDA.Free ;
exit;
end;
end;
try
begin
with myDA.MyDataSet do
begin
if Not IsEmpty then
begin
APlugin.Caption:= FIEldByName(’modName’).Value;
APlugin.DllFileName := FIEldByName(’modFile’).Value;
APlugin.WndClass := FIEldByName(’modWndClass’).Value ;
result:=True;
end;
Close;
end; //end of with...do...
end; //end of try
except
on E:Exception do
begin
Result:=False;
myDA.Free ;
Exit;
end; //end of exception
end; //end of try...except
myDA.Free ;
end;
//-----------------------------------------
//Name: LoadShowPluginForm
//Func: 載入DLL插件並顯示窗口
//Para: APlugin-TMyPlugins
//Rtrn: true-創建成功
//Auth: CST
//Date: 2005-6-2
//-----------------------------------------
function TFormHall.LoadShowPluginForm (const APlugin:TMyPlugins):boolean;
var
ShowDLLForm: TShowDLLForm; //DLL接口函數實例
FreeDLLForm: TFreeDLLForm;
sPath:string; //DLL文件的完整路徑
begin
try
begin
sPath:=ExtractFilepath(Application.ExeName)+ ’plugins\’ + APlugin.DllFileName ;
APlugin.ProcAddr := LoadLibrary(PChar(sPath));
APlugin.FuncFreeAddr := GetProcAddress(APlugin.ProcAddr,’FreeDLLForm’);
APlugin.FuncAddr := GetProcAddress(APlugin.ProcAddr ,’ShowDLLForm’);
@ShowDLLForm:=APlugin.FuncAddr ;
@FreeDLLForm:=APlugin.FuncFreeAddr;
if ShowDllForm(Self.Handle, APlugin.Caption , APlugin.UserID) then
Result:=True
else
Result:=False;
end;
except
on E:Exception do
begin
Result:=False;
ShowMessage(’載入插件模塊錯誤,請檢查PLUGINS目錄裡的文件是否完整。’);
end;
end;
end;
……