delphi – Change properties of a component contained in a system module

Question:

I'm modulating a system developed in Delphi XE3. One of my modules named Default has a dmDados with a connection. In my main application, in the onShow of my main Form I am loading the module and trying to disconnect my connection component:

LoadPackage('modules/Default.bpl');
dm_Connection.ZConnection.Disconnect;

But I'm getting an Access Violation message. I think I'm not doing it the right way. How should I do?

Answer:

What instantiates forms and datamodules in a Delphi application is the code contained in the body of the main program ( .DPR file). Delphi automatically places this code each time a new form or datamodule is added to the project, when it comes to a project to generate an .EXE .

When dealing with a package project however, such code is not automatically included.

You will need to manually instantiate all the objects you want inside a package . This is especially true when the programmer decides to load the runtime package manually, as it looks like that's what you're doing (by calling LoadPackage ).

One way to pre-initialize a series of objects within the package is to choose a unit and add the code to create the instances at the initialization of this unit .

When a package is loaded, the existing code within the initialization session of all units is executed in some uncontrolled order. When the package is unloaded, code existing within the finalization sessions of all units is executed, again without an order guarantee.

I recommend reading an interesting article that may be of good value to you. This article makes a comparison between the use of DLLs and packages and shows how the latter are easier and more straightforward, as all types declared within it are available for use as soon as the package is loaded.

Scroll to Top