5

I am working on a component in Delphi 7 and Delphi 2006, where I am using a unit which I need to add to the .dpr file of the project on which the component is dropped automatically. Like the way Eureka Log automatically adds the unit 'ExceptionLog' to the project file:

enter image description here

Can anyone tell me how to programmatically add a unit to the project file when I drop my component on any form in the project?

Johan
  • 74,508
  • 24
  • 191
  • 319
PresleyDias
  • 3,657
  • 6
  • 36
  • 62
  • 2
    I think the source units are added automatically in all cases. Surely only when they are known, when they have already been included to the Delphi paths (what is consequent). – TLama Apr 24 '12 at 07:28
  • 2
    @Tlama, the component units are added the the *form* or *data module* unit automatically. They're not automatically added to the *project*. That's something special Eureka Log does for itself. – Rob Kennedy Apr 24 '12 at 13:03
  • @Rob, it's the next question I've misread. I'll have to pay more attention. Thanks! – TLama Apr 24 '12 at 13:07
  • @menjaraz Yes, sure..u know how to do it? – PresleyDias Apr 24 '12 at 17:33

3 Answers3

7

You most likely have to use the Open Tools API for that.

Also it might require to write a TSelectionEditor for your component to trigger the adding of the unit (I would try the RequiresUnit method).

While there is an easy way to just add a unit to the active project (code below) this just works for the active project which might not be the project the form belongs to you are adding the component to. Also it adds the unit at the end of the uses clause.

uses
  ToolsAPI;

var
  currentProject: IOTAProject;
begin
  currentProject := GetActiveProject();
  currentProject.AddFile('MyUnit.pas', True);

You can check the GExperts source code because it contains a class (TUsesManager) that can parse units and modify the uses clause.

Stefan Glienke
  • 20,860
  • 2
  • 48
  • 102
  • I expect `AddFile` would add an item like `uses MyUnit in 'MyUnit.pas'`, which wouldn't be appropriate for a library component. The component should add just a unit reference, not a file reference, as shown in the picture in the question. – Rob Kennedy Apr 24 '12 at 13:10
  • @Rob It does as you say. It was just an example to show that OTA may be able to do so. But I did not find a appropriate method. That's why I mentioned the GExperts source. – Stefan Glienke Apr 24 '12 at 13:30
  • Shoudn't that `IsUnitOrForm` parameter be `True`? – NGLN Feb 02 '13 at 10:05
  • @NGLN According to the source code comments in the ToolsAPI yes – Stefan Glienke Feb 04 '13 at 07:00
0

Odd.

I used to set my default dpr to contain next to nothing as a result my toolbox was very empty. So if it was in my toolbox it was in the dpr - what are you having problems with - normally if its in the toolbox, its already in the dpr.

BugFinder
  • 17,474
  • 4
  • 36
  • 51
  • i mean, my component needs a `unit` in the `dpr`, and i cannot ask/relay on the `user` to put it there..so i need to automate the same – PresleyDias Apr 24 '12 at 07:28
  • Without my copy of delphi to hand, you could of course set a condition in the unit you should add in the dpr, and if its not there for the other units which would be added to the .pas file - throw a compile error with "Please add unit x to the dpr file" .. – BugFinder Apr 24 '12 at 07:32
0

go Project > Eurekalog Options and disable Eurekalog.

MajidTaheri
  • 3,813
  • 6
  • 28
  • 46
  • 5
    i think you didnt get my question, i mean like eureka log adds `ExceptionLog` to the `dpr`. how to do the same – PresleyDias Apr 24 '12 at 10:35