Question

suppose i have a unit like this

unit sample;

interface

function Test1:Integer;
procedure Test2;

implementation

function Test1:Integer;
begin
 result:=0;
end;

procedure Test2;
begin

end;

end.

Is possible enumerate all the procedures and functions of the unit sample in runtime?

Was it helpful?

Solution

No. RTTI is not generated for standalone methods. Hopefully this will be fixed in a later version, (they'd probably need a TRttiUnit type to do that,) but for now it's not available.

OTHER TIPS

You could extract that information from some kind of debug info (TD32, Map file, Jdbg, etc.) using JCL and their great JclDebug.pas.

Try this:

uses
  JclDebug;

type
  TProc = record
    name: string;
    addr: Pointer;
  end;

  TProcArray = array of TProc;
  TMapLoader = class
  private
    FModule: Cardinal;
    FProcs: TProcArray;
    FMapFileName: string;
    FUnitName: string;
    procedure HandleOnPublicsByValue(Sender: TObject; const Address: TJclMapAddress; const Name: string);
  public
    constructor Create(const AFileName: string; AModule: Cardinal; const AUnitName: string);
    procedure Scan();
    property Procs: TProcArray read FProcs;
  end;

constructor TMapLoader.Create(const AFileName: string; AModule: Cardinal; const AUnitName: string);
begin
  inherited Create;
  FMapFileName := AFileName;
  FModule := AModule;
  FUnitName := AUnitName;
end;

procedure TMapLoader.HandleOnPublicsByValue(Sender: TObject; const Address: TJclMapAddress; const Name: string);
var
  l: Integer;
begin
  if Pos(FUnitName + '.', Name) = 1 then
  begin
    l := Length(FProcs);
    SetLength(FProcs, l + 1);
    FProcs[l].name := Name;
    FProcs[l].addr := Pointer(Address.Offset + FModule + $1000);
  end;
end;

procedure TMapLoader.Scan();
var
  parser: TJclMapParser;
begin
  parser := TJclMapParser.Create(FMapFileName, FModule);
  try
    parser.OnPublicsByValue := HandleOnPublicsByValue;
    parser.Parse;
  finally
    parser.Free;
  end;
end;

I don't think so.

That is a compile-time config, it's used so as the compiler knows which function name is being called or not. As far as I know, there is nothing at runtime which comes close to listing these functions.

Delphi's excellent runtime features come from RTTI, you might want to see what it offers in relation to this. But as I said, I don't think it's possible (know that I've delved in RTTI for quite some time...).

Edit: Oh and by the way, after compilation, functions lose their human-readable names (to addresses). There are some tables which pinpoint those names to addresses, most notably, RTTI and the Debug info.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top