Description
_DLL_InitTerm() is a special entry point function in a DLL. Normally you don't need to worry about this function, but sometimes you want to gain extra control over the DLL loading and initialization process, and knowing about this function is important in such a case.
The sample program shows how to set up your own entry function within a C++ DLL. There are several points to note in the example. One is that the function, along with several helper functions such as _CRT_init(), must be declared with C linkage, using extern "C". This is because C++ changes the names of functions (for example, to reflect overloaded functions), and these special functions are defined in C.
Another point is that ulFlag indicates whether the DLL is being initialized or terminated. If it's being initialized, then _CRT_init() is called to initialize the C runtime system (such as zeroing of global data), and __ctordtorInit() is called to initialize C++ (constructors for global objects). If the DLL is being terminated, then __ctordtorTerm() is called to run destructors on global objects, followed by a call to _CRT_Term() to shut down the C runtime system (such as flushing and closing open files).
Finally, _DLL_InitTerm() returns a non-zero value to indicate that it was successful in its startup or shutdown processing.
Concept
The sample has two parts. One configuration file, share.icc, is used to build a DLL. This DLL exports a single function f(), that returns a value indicating whether the global object of type A in the DLL had its constructor executed. Without the constructor being run, the data value in A objects (A.x) will have a default value of 0.
The other part is the driver program, that calls f() and checks its return value. If you comment out the call to __ctordtorInit(), the constructor will not be run, and the return value will change from 100 to 0.
Special Notes:
Execution of constructors and destructors is of crucial importance for global objects in C++. For example, the C++ stream I/O system uses this mechanism for initialization and for shutdown (such as flushing buffers before closing input or output files).
Supported
Supported
