Add code before initialization of units in Delphi

前端 未结 3 1114
礼貌的吻别
礼貌的吻别 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条回答
  • 2021-01-19 22:18

    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.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-19 22:31

    A word of warning here.

    I've got an application running on the desktop of a logged in user and IN THE MIDDLE of testing the app the DecimalSeparator changed for me, without me restarting the application.

    I used to set the

    DecimalSeparator := '.'; 
    

    once in the FormCreate() code, but that seems not the be enough. So now I set it once every time before I use my FormatFloat() function (used only in one place in my application).

    I do not know WHY this happens, but probably there are some system-wide parameter changes happening, that reset the char to ',' on my system.

    The best way to avoid this is probably to set the decimal separator in windows configuration to '.' to avoid strange problems...

    0 讨论(0)
提交回复
热议问题