Add code before initialization of units in Delphi

前端 未结 3 1112
礼貌的吻别
礼貌的吻别 2021-01-19 21:41

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 h

3条回答
  •  -上瘾入骨i
    2021-01-19 22:29

    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.

提交回复
热议问题