开发者

Add code before initialization of units in Delphi

开发者 https://www.devze.com 2023-01-01 12:58 出处:网络
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开发者_如何学Go befor

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开发者_如何学Go 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.


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.


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消