Using VCL Styles in a DLL causes System Exception

2019-08-20 09:30发布

问题:

An application originally written in XE2 that uses styles within DLL's so that forms that popup from DLL's are the same style as the EXE, when updated to build in 10.2 Tokyo, now causes System Exceptions when opening certain forms from the EXE, or when closing certain forms in the EXE.

回答1:

I don't need to include the minimal reproducible example in this question, because I have an answer, that someone else may have been able to add to my original question had it not been closed so quickly, and then not re-opened even after making it on-topic.

Turns out it's a behavioural issue in VCL:

Exception if using comboboxes in a form that resides in a DLL and that uses VCLStyles.

Embarcadero won't fix it as it's not a "problem",

R&D writes that the style manager has to be enabled in the application and there can be only one one TStyleManager with enabled system hooks (TStyleManager.SystemHooks property), because it process all windows from application. The current system doesn't support the scenario you are suggesting, and there is currently no plan to rework it

but there is a workaround which is to add the following line of code immediately before calling SetStyle or TrySetStyle in the DLL code:

TStyleManager.SystemHooks := [];

Hopefully this will be of assistance to Delphi developers who run into this annoying problem after upgrading to the newer versions.

In my case, I add a conditional define to the project for the libraries, then add this code to the places where it setting styles, as the same unit is included in both EXE and DLL:

{$IFDEF DLL}
  TStyleManager.SystemHooks := [];
{$ENDIF}