这是我以前写的一个MSN插件类,实现dll导出类的功能。你看一下
我去掉了不必要的代码只留下一个框架。
unit Plug_In;
interface
uses
SysUtils,Classes,Contnrs,
Windows;
type
PPlug_Info=^TPlug_Info;
TPlug_Info=packed record
Author
Char;
PlugVer
Char;
Other
Char;
KeyWord
Char;//;
PlugName
Char;
OtherProc
Char;
ServerType
Char;
end;
IMsnPlug=interface
procedure ShowAbout;
function GetPlugInfo
Plug_Info;
function GetContents(KeyWord
Char)
Char;
end;
TGetPlug=function:IMsnPlug;stdcall;
TAnothermethod=procedure(FOwner:HWND);stdcall;
TPlugManager=class
Private
FPlugList:TObjectList;
FPlugName:TStrings;
FGetPlug:TGetPlug;
procedure GetPlugName;
Protected
Public
function PlugInfo(PlugNo:Integer):TPlug_Info;
function Plug(PlugNo:Integer):IMsnPlug;
procedure LoadPlug(const Plug_Dir:string);
property PlugList:TObjectList Read FPlugList Write FPlugList;
procedure PlugProc(const PlugNo:Integer;const ProcName
Char;H:HWND);
property PlugName:TStrings read FPlugName write FPlugName;
constructor Create;
destructor Destroy;
Override;
end;
implementation
{ PlugManager }
constructor TPlugManager.Create;
begin
FPlugList:=TObjectList.Create(False);
FPlugName:=TStringList.Create;
end;
destructor TPlugManager.Destroy;
var
i:Integer;
begin
if FPlugList.Count<>0 then
for i:=0 to FPlugList.Count-1do
FreeLibrary(Cardinal(FPlugList
));
FreeAndNil(FPlugList);
FreeAndNil(FPlugName);
inherited Destroy;
end;
procedure TPlugManager.GetPlugName;
var
i,j:Integer;
begin
FPlugName.Clear;
i:=FPlugList.Count;
if i<>0 then
begin
Dec(i);
for j:=0 to ido
begin
FPlugName.Add(PlugInfo(j).PlugName);
end;
end;
end;
procedure TPlugManager.LoadPlug(const Plug_Dir:string);
var
FRec:TSearchRec;
Rec:Integer;
PN:string;
begin
FPlugList.Clear;
Rec:=FindFirst(Plug_Dir+'/*.pus',faAnyFile,FRec);
while not Boolean(Rec)do
begin
PN:=Plug_Dir+'/'+FRec.Name;
FPlugList.Add(TObject(LoadLibrary(PChar(PN))));
Rec:=FindNext(FRec);
end;
GetPlugName;
end;
//这个fun导出就可实现导出类 <-请看这里
function TPlugManager.Plug(PlugNo:Integer):IMsnPlug;
begin
Result:=TGetPlug(GetProcAddress(Cardinal(FPlugList[PlugNo]),'GetPlug'));
end;
function TPlugManager.PlugInfo(PlugNo:Integer):TPlug_Info;
begin
try
Result:=TGetPlug(GetProcAddress(Cardinal(FPlugList[PlugNo]),'GetPlug')).GetPlugInfo^;
except
//
end;
end;
procedure TPlugManager.PlugProc(const PlugNo:Integer;const ProcNameChar;H:HWND);
begin
try
TAnothermethod(GetProcAddress(Cardinal(FPlugList[PlugNo]),ProcName))(H);
except
//
end;
end;
end.