由于程序中有很多需要公用的类,加上现在要将一些程序独立,这样调用这些公用类的地方就多了,我想把这些类写成dll,不知道该怎么做?写完后delphi程序怎么调用?谢谢了!
以下是两个需要写的例子:
TParmsBD = record
...................
................
end; TRunlist=class(TBaseRunlist)
.........
.........
end;
以下是两个需要写的例子:
TParmsBD = record
...................
................
end; TRunlist=class(TBaseRunlist)
.........
.........
end;
======
PCompCardItem = ^TCompCardItem;
TCompCardItem = record
DllHandle:Hwnd;
FileName:String;
CardGUID:TGUID;
CardName:String;
GetCompEditor:GetCompEditorProc;
GetCompController:GetCompControllerProc;
GetCompLoader:GetCompLoaderProc;
end;//将某个dll中的内容信息保存在队列内
procedure TCompCardManager.AddCompCard(AFileName:String);
var
Item:PCompCardItem;
DllHandle:Hwnd;
GetCardGUID:GetCardGUIDProc;
GetCardName:GetCardNameProc;
FileName:String;
begin
FileName:=ExtractFileName(AFileName);
if Assigned(FOnLoading) then begin
FOnLoading(self,Format('Loading %s...',[FileName]),300);
end;
DllHandle:=LoadLibrary(PChar(AFileName));
if DllHandle<>0 then begin
GetCardGUID:=GetProcAddress(DllHandle,'GetCardGUID');
GetCardName:=GetProcAddress(DllHandle,'GetCardName');
if (@GetCardGUID<>nil) and (@GetCardName<>nil) then begin
new(Item);
Item^.DllHandle:=DllHandle;
Item^.FileName:=AFileName;
Item^.CardGUID:=GetCardGUID();
Item^.CardName:=GetCardName();
Item^.GetCompEditor:=GetProcAddress(DllHandle,'GetCompEditor');
Item^.GetCompController:=GetProcAddress(DllHandle,'GetCompController');
Item^.GetCompLoader:=GetProcAddress(DllHandle,'GetCompLoader');
FCompCardList.Add(Item);
end
else begin
FreeLibrary(DllHandle);
raise CompCardException.CreateFmt('Card "%s" is not a valid card file!',[FileName]);
end;
end
else begin
raise CompCardException.CreateFmt('Can not load card "%s"!',[FileName]);
end;
end;//从已存在的Dll列表中找到要找的那个,并且调用其中的函数,让其返回某个类(是类不是对象)
function TCompCardManager.FindCompLoader(ACardGUID:TGUID):TCompLoaderClass;
var
Item:PCompCardItem;
I:Integer;
begin
Result:=nil;
for I:=0 to FCompCardList.Count-1 do begin
Item:=FCompCardList.Items[I];
if IsEqualGUID(Item^.CardGUID,ACardGUID) then begin
if Assigned(Item^.GetCompLoader) then begin
Result:=Item^.GetCompLoader();
end;
exit;
end;
end;
end;//程序最终通过该找到的类来创建出真正的对象。
procedure TRoundManager.LoadCompetition(ATypeGUID:String;AFileName:String;ARoundID:Integer);
var
CompLoaderClass:TCompLoaderClass;
CompLoader:TCompLoader;
begin
CompLoaderClass:=GCardManager.FindCompLoader(StringToGUID(ATypeGUID));
if not Assigned(CompLoaderClass) then begin
raise RoundMgrException.Create('Can not found CompLoaderClass!');
end;
CompLoader:=CompLoaderClass.Create();
if not Assigned(CompLoader) then begin
raise RoundMgrException.Create('Can not create CompLoader!');
end;
CompLoader.LoadCompetition(AFileName,ARoundID,FDataStream);
end;dll中:
======
function GetCompLoader():TCompLoaderClass;
begin
Result:=TBasketballLoader;
end;exports
GetCompLoader;//TCompLoader的具体实现
type
TBasketballLoader = class(TCompLoader)
public
procedure LoadCompetition(AFileName:String;ARoundID:Integer;ADataStream:TMemoryStream);override;
end;//主程序和dll都共有的基类声明,这样主程序才能知道到底哪些方法是该类所支持的。
type
TCompLoaderClass = class of TCompLoader;
TCompLoader = class(TObject)
public
procedure LoadCompetition(AFileName:String;ARoundID:Integer;ADataStream:TMemoryStream);virtual;abstract;
end;
[email protected]
www.tonixsoft.com