Adding a unit automatically to the project
Asked Answered
D

3

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?

Distrustful answered 24/4, 2012 at 7:1 Comment(4)
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).Overhand
@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.Godhead
@Rob, it's the next question I've misread. I'll have to pay more attention. Thanks!Overhand
@menjaraz Yes, sure..u know how to do it?Distrustful
D
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.

Doubletime answered 24/4, 2012 at 11:35 Comment(4)
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.Godhead
@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.Doubletime
Shoudn't that IsUnitOrForm parameter be True?Brevier
@Brevier According to the source code comments in the ToolsAPI yesDoubletime
G
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.

Godforsaken answered 24/4, 2012 at 7:19 Comment(2)
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 sameDistrustful
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" ..Godforsaken
I
0

go Project > Eurekalog Options and disable Eurekalog.

Imaginal answered 24/4, 2012 at 10:33 Comment(1)
i think you didnt get my question, i mean like eureka log adds ExceptionLog to the dpr. how to do the sameDistrustful

© 2022 - 2024 — McMap. All rights reserved.