Delphi XE10 RTTI

program lanchuer;

uses
System.SysUtils, System.Rtti, System.Classes,
System.Generics.Collections,
Winapi.Windows,
vcl.Forms;

{$R *.res}


var
curpath: string;
ctx: TRttiContext;
logs: TStringBuilder;
mainform: TCustomForm;

function loadTypes(): Boolean;
var
pkgs: tarray<TRttiPackage>;
pkg: TRttiPackage;
typs: tarray<TRttiType>;
typ: TRttiType;
i, j: Integer;
begin
pkgs := ctx.GetPackages;
for j := Low(pkgs) to High(pkgs) do
begin
pkg := pkgs[j];
logs.Append(pkg.Name + #9 + pkg.QualifiedClassName + #9 + pkg.Handle.ToString).AppendLine;
typs := pkg.GetTypes;
for i := Low(typs) to High(typs) do
begin
typ := typs[i];
if (tkClass = typ.TypeKind) then
logs.Append(#9 + typ.AsInstance.MetaclassType.ClassName).AppendLine;
end;
end;
end;

function loadMainform(): Boolean;
var
pkgs: tarray<TRttiPackage>;
pkg: TRttiPackage;
typs: tarray<TRttiType>;
typ: TRttiType;
i, j: Integer;
begin
pkgs := ctx.GetPackages;
for j := Low(pkgs) to High(pkgs) do
begin
pkg := pkgs[j];
typs := pkg.GetTypes;
for i := Low(typs) to High(typs) do
begin
typ := typs[i];

if (tkClass = typ.TypeKind) then
begin
if typ.AsInstance.MetaclassType.InheritsFrom(TCustomForm) then
begin
if (typ.GetProperty('isMainForm') <> nil) then
begin
Application.CreateForm(TComponentClass(typ.AsInstance.MetaclassType), mainform);
Exit(True);
end;
end;
end;
end;
end;
end;

function LoadPackage(const Name: string): HMODULE;
begin
Result := SafeLoadLibrary(Name);
if Result = 0 then
begin
logs.Append(Name + ' ' + SysErrorMessage(GetLastError)).AppendLine;
end
else
begin
try
InitializePackage(Result, nil);
except
FreeLibrary(Result);
end;
end;
end;

procedure saveLogsToFile;
var
strs: TStringList;
begin
try
strs := TStringList.Create;
strs.Add(logs.ToString);
if (logs.ToString <> '') then
strs.SaveToFile(curpath + 'log.txt');
finally
strs.Free;
end;
end;

begin
ReportMemoryLeaksOnShutdown := True;
Application.Initialize;
Application.MainFormOnTaskbar := True;
curpath := ExtractFilePath(ParamStr(0));
logs := TStringBuilder.Create;

LoadPackage(curpath + 'ywl.mvc.bpl');
LoadPackage(curpath + 'ywl.mvc.mssql.bpl');
LoadPackage(curpath + 'ywl.mvc.sqlite.bpl');
LoadPackage(curpath + 'ywl.mvc.httpserver.bpl');
LoadPackage(curpath + 'ywl.mvc.portal.bpl');
logs.AppendLine;
try
ctx := TRttiContext.Create;
ctx.KeepContext;

// loadTypes();

loadMainform();

saveLogsToFile;

Application.Run;
finally
ctx.DropContext;

logs.Free;
end;

end.

posted on 2017-03-06 17:37  ILLL  阅读(242)  评论(0编辑  收藏  举报

导航