Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add code before initialization of units in Delphi

Tags:

delphi

Is there a place where I can add code that will be executed before unit initialization?

The reason I want to do this is I need to change the DecimalSeparator, this has to be done before the initialization of some units. I have put it in the project source, before Application.Initialize but it is too late by then.

As I see it the only choice I have is to put it in the initialization of the unit that needs the DecimalSeparator to be changed, is this the case?

Thanks in advance for any advice.

like image 578
Alan Clark Avatar asked Dec 02 '22 05:12

Alan Clark


2 Answers

Initialization order in Delphi is deterministic: units get initialized in the same order as the compiler compiled them, and finalized in the reverse order. The compiler starts at the top of the DPR's uses clause and works its way down, and for each unit it finds, it does the same thing recursively: start at the start of the uses clause, try to compile each used unit that isn't already compiled, then compile the current unit. So if you can get your unit in before any of the other ones get compiled, then it will get initialized first.

If you want to make sure it gets executed first, make a new unit, put your changes in that unit's initialization block, and then make sure it ends up in the DPR before any of the units that will depend on the changes. You might even want to make it the first unit, unless you have other "must be first" units there already, such as replacement memory managers.

like image 65
Mason Wheeler Avatar answered Dec 03 '22 17:12

Mason Wheeler


Put it into the initialization section of the first unit in your project uses list, that way it will be executed prior to any other initialization code.

like image 44
mghie Avatar answered Dec 03 '22 18:12

mghie