List global unit methods using delphi

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 it possible to list all procedures and functions of a sample device at runtime?

+4
source share
3 answers

Not. RTTI is not generated for standalone methods. I hope this will be fixed in a later version (for this, probably, the TRttiUnit type will be needed), but so far it is not available.

+6
source

You can extract this information from any debugging information (TD32, map file, Jdbg, etc.) using JCL and their large JclDebug.pas.

Try the following:

 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; 
+1
source

I do not think so.

This is a compile-time configuration, it is used so that the compiler knows which function name is called or not. As far as I know, nothing happens at runtime, which is close to listing these functions.

Delphi's excellent functionality comes from RTTI, you can see what it offers in relation to this. But, as I said, I do not think that this is possible (know that I have been working in RTTI for quite some time ...).

Edit: Oh, and by the way, after compilation, the functions lose their human-readable names (by address). There are several tables that point these names to addresses, primarily RTTI and Debug information.

0
source

All Articles