3

I want to load all classes from a current project in a TList. If I read the dpr file like a normal file it will return me only strings. I want to get all the classes defined in the dpr file and their names. Does someone know how to do that?

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
Jordan Borisov
  • 1,603
  • 6
  • 34
  • 69
  • 1
    Classes are not normally referenced in the .dpr file. Units are. – David Heffernan Oct 12 '11 at 12:50
  • 1
    What do you mean by "classes from a current project"? If you mean list of all classes compiled into the executable (assuming from your [previous question](http://stackoverflow.com/questions/7737897/delphi-getclass)) there is no such list. You can use RegisterClasses or your own registration scheme, e.g. have a global TStringList with Strings holding class names and Objects holding TClass references. – Ondrej Kelle Oct 12 '11 at 12:56
  • I'm doing this right now. I just want a dynamics in this project – Jordan Borisov Oct 12 '11 at 13:01
  • And that's AFAIK the only way since the compiler doesn't generate a list of all compiled classes. If you're using runtime packages you can enumerate the exports as shown [here](http://stackoverflow.com/questions/763232/how-to-scan-the-full-list-of-currently-installed-vcl-components/765076#765076), though. – Ondrej Kelle Oct 12 '11 at 13:06
  • 1
    You may want to edit your question because it's very unclear. You might get much better help if people understand what you want. – Ondrej Kelle Oct 12 '11 at 13:08
  • @TOndrej, for all classes residing in binary, i'd go to "classes hack" proposed aeons ago by Ludovic Dubois (sp?) – Premature Optimization Oct 12 '11 at 14:08

1 Answers1

6

In the Delphi IDE, all classes are available in the .dcu files, corresponding to each .pas files. Those .dcu files have a proprietary binary evolving format, so can not be used outside the IDE.

At program execution, and within the exe file, there is no list of all existing classes. You can retrieve information about a known class using the RTTI functions (see TypInfo.pas and relatives as stated by the Embarcadero documentation). So at runtime, you just can retrieve information from a given class: you can use e.g. anObject.ClassName or anObject.ClassType methods.

But I suspect you want to retrieve all classes defined in a project, from its source code. For this, you will need a source code parser, which will extract the logic from the .pas files. In short, the parser will read the .dpr then all necessary .pas files source code, interpret the object pascal type definitions, and create a list of units, classes, methods and properties. There are several parsers around: see for instance PasDoc or the version we embedded in SynProject.

Additional note - for an exhaustive list: If you generate a .map file during the compilation, this text file will contain all symbol names of the executable, including the classes. You'll have to parse it, but won't have much information to deal with, since there is no easy way of guessing if each symbol is a class or a record, for instance, or about classes inheritances or properties... This .map is intended about execution debugging, not RTTI.

Arnaud Bouchez
  • 42,305
  • 3
  • 71
  • 159