This article begins with a quick look at what the program started and what it did at the moment.
1 Program Starofficemain;2 3 uses4 starofficeapplication,5MainForminch 'Form\mainform.pas' {Frmmain},6Starmainforminterfaceinch 'Interface\starmainforminterface.pas';7 8 {$R *.res}9 Ten begin Onereportmemoryleaksonshutdown:=True; A application.initialize; -Application.mainformontaskbar: =True; - Application.createform (Tfrmmain, frmmain); the Application.Run; - End.
This is the project file of the main program, compared to the default project, is to replace the original forms with Starofficeapplication
1 Unit starofficeapplication; 2 3 var 4 Application:tstarofficeapplication;
Starofficeapplication defines a global variable application for a Tstarofficeapplication object, so the application of forms is replaced
1 Constructor Tstarofficeapplication. Create ; 2 begin 3 Fmodulemgr:=tstarmodulemanager. Create ; 4 End;
The Tstarofficeapplication constructor creates a Tstarmodulemanager object that is responsible for the module management of the framework
1 procedure Tstarofficeapplication.run; 2 begin 3 Forms.Application.Run; 4 fmodulemgr.final; 5 End;
When executing Application.Run in the project file, the Tstarofficeapplication run function is actually called
The run function first invokes the Application.Run in the forms file;
But the program exits fmodulemgr.final; is the unload of the module
1 Constructor Tstarmodulemanager. Create ; 2 begin 3 floadbatch:="; 4 Fmodulelist: = TObjectList. Create (True); 5 Tstarobjectfactoryext. Create ([Istarmoduleinfoenum,istarmoduleloader], self); 6 End;
A Istarmoduleloader interface instance object is registered in the module management class Tstarmodulemanager constructor
Tstarobjectfactoryext Instance extension factory inherits from Tstarbasefactoryext, in Tstarbasefactoryext's constructor
Created a class factory management object Ffactorymanager
1 ConstructorTstarbasefactoryext.Create(ConstIIDs:Array oftguid);2 varI:integer;3 begin4Fiidlist:=tstringlist.Create;5 6 forI:=low (IIDs) toHigh (IIDs) Do7 begin8 if starofficefactory. Exists (Iids[i]) Then9 Raise exception.createfmt (err_intfexists,[guidtostring (iids[i]));Ten One Fiidlist.add (guidtostring (iids[i)); A End; - starofficefactory.registerfactory (self); - End;
1 function Starofficefactory:tstarfactorymanager; 2 begin 3 if ffactorymanager=Nilthen4 Ffactorymanager:=tstarfactorymanager. Create; 5 6 result:=Ffactorymanager; 7 End;
As you can see from the code, whenever you call the Starofficefactory function, only the first call to create a class factory management object
Similarly, notification management, event management is also created at the time of program startup and added to the class factory management object
1 proceduretfrmmain.formcreate (sender:tobject);2 varIntf:istarmoduleloader;3 begin4Tstarobjectfactory.Create(istarmainform,self);;5 6intf:=StarOffice as istarmoduleloader;7 Intf.loadbegin;8 Intf.loadmodulesfromdir;9 Intf.loadfinish;Ten One(StarOffice asIstareventmanager). Enumevent (self); A End;
1 function Staroffice:iinterface; 2 begin 3 if not Then 4 Fsysservice: = Tstarofficeservice. Create ; 5 6 Result: = fsysservice; 7 End;
StarOffice returns a IInterface object that is created by the Tstarofficeservice class
The IInterface object returned by StarOffice does not have any inheritance relationship with the interface Istarmoduleloader, but why can it?
This is the interface query for the properties of the interface
1Tstarofficeservice =class(TObject, IInterface)2 Private3 Frefcount:integer;4 protected5 functionQueryInterface (ConstIid:tguid; outOBJ): HResult;stdcall;6 function_addref:integer;stdcall;7 function_release:integer;stdcall;8 Public9 End;
The Tstarofficeservice class implements the interface function of IInterface
1 functionTstarofficeservice.queryinterface (ConstIid:tguid; outObj): HResult;2 var3 afactory:tstarfactory;4 begin5Result: =E_nointerface;6 ifSelf. GetInterface (IID, OBJ) Then7Result: =S_OK8 Else9 beginTen afactory: = starofficefactory.findfactory (IID); One if Assigned (afactory) Then A begin - afactory.createinstance (IID, OBJ); - Result: = S_OK; the end; - End; - End;
The QueryInterface function is called when an as operation is performed on an interface object, which is the mechanism of the interface
First, the default GetInterface function is called for the interface query, and GetInterface looks for the interface from the inheritance relationship, and then looks for the interface factory from the interface factory when it is not found.
Create an interface instance object from the appropriate factory if it is found
This is how the unified interface call works.
Back to the main program's form creation function, called the Istarmoduleloader interface of the Loadmodulesfromdir function, the interface function implementation of the current program directory of the module is loaded all in
(StarOffice as Istareventmanager). Enumevent (self);
Event enumeration functions are called on the event management interface, and all events registered to event management are enumerated and dynamically created as a menu
This is the start time of the demo program.
Delphi Information Management System Open source Framework @ Start-up time