How to Use a Data Module in Applications, DLLs, and Distributed Objects

   

Data modules are just like forms in the sense that they are encapsulated in their own .h and .cpp files, that they have a .dfm file that describes the components in the data module, and that they can reference other forms or data modules. Data modules can be autocreated (see the list in Project, Options, or edit the project source using Project, View Source), or can be created with the new operator (as in SomeDataModule = new TSomeDataModule(Application) ). A data module also has an owner that takes responsibility for freeing it. This is typically the application, in which case your data module will be automatically freed by the application when the application terminates. It can also be a form (in which case the form automatically frees the data module when the form is freed), a component (which will automatically free the data module when it is freed), or even NULL (in which case you must make sure the data module is freed by your own code, usually in a destructor because there is no owner to perform that action automatically).

To use a data module with a form, you typically need only two steps:

  • Make sure the data module will be created before any form that uses it (by altering the order of creation in changing the project source or the autocreate list, if needed). Of course, if the form is not autocreated, it can simply be created with new as needed, and deleted as needed.

  • Use File, Include Unit Hdr on the form that will use the data module to ensure that the data module is #included in the form .cpp file.

Using a Data Module with a DLL, COM, or CORBA object is actually just as simple.

If you are using a data module with a DLL and need the data module to remain open across calls to the DLL for the duration of time the DLL is loaded, you can open the data module in the DllEntryPoint() . The code in Listing 7.1 shows how this works.

Listing 7.1 Creating a Data Module in a DLL
 #include <vcl.h>  #include <windows.h>  #pragma hdrstop  #include <Forms.hpp>  #include <TestDataModuleUnit.h>  #pragma argsused  String InternallyMaintainedResultString;  int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)  {      // This is called on every process attach,      // so make sure we need to initialize by checking the global variable...      if (TestDataModule == NULL)      {          Application->Initialize(); // This is essential          Application->CreateForm(__classid(TTestDataModule),&TestDataModule);      };      return 1;  } 

Similar procedures can be followed for COM (the data module must be instantiated in the DllEntryPoint () function of the generated Active Server Library) and CORBA. In CORBA, the server is itself a data module, so it is very easy to use data module techniques with CORBA.

For more on multitier and distributed programming, see Part IV, "Distributed Computing."


   
Top


C++ Builder Developers Guide
C++Builder 5 Developers Guide
ISBN: 0672319721
EAN: 2147483647
Year: 2002
Pages: 253

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net