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.

转载于:https://www.cnblogs.com/dbml/p/6511083.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值