Every time an instance of a DLL is loaded its environment must be initialized. When you are finished with the DLL, its environment must be terminated. You can accept the default initialization and termination, which is included automatically by default, or write your own initialization and termination function.
The initialization and termination function for subsystems and regular DLLs must have the following prototype:
unsigned long _System _DLL_InitTerm(unsigned long modhandle,
unsigned long flag);
The _DLL_InitTerm function must be compiled using _System linkage, because it is called by the operating system loader.
The _DLL_InitTerm function has two parameters:
The return code from _DLL_InitTerm tells the loader if the initialization or termination was performed successfully. If the call was successful, _DLL_InitTerm returns a nonzero value. A return code of 0 indicates that the function failed.
You can perform any specialized initialization and termination you want. For example, you can set initial values to variables as part of the initialization. You must also make sure you initialize and terminate the environment properly. If you change a subsystem DLL at a later time to use the regular runtime libraries, you should modify your _DLL_InitTerm function to ensure that the runtime environment is correctly initialized.
If you can accept the default initialization and termination done by _DLL_InitTerm and want just to perform specialized initialization and termination actions required by your DLL, you may find it easier to provide your own version of function DllMain and use the default _DLL_InitTerm function. The function DllMain has the same linkage, return type, and argument list as _DLL_InitTerm. The default _DLL_InitTerm calls DllMain. The default DllMain is an empty function.
Both _DLL_InitTerm and DllMain can call functions in the VisualAge C++ run-time library. If you provide just DllMain, you do not have to perform the following standard initialization and termination actions.
Initializing the Environment
You may have to make four initializations for your DLL:
int _CRT_init(void);
If the run-time environment is successfully initialized, _CRT_init returns 0. A return code of -1 indicates an error. If an error occurs, an error message is written to file handle 2, which is the standard stream stderr.
void __ctordtorInit(int flag);
The argument should have the value zero. It is used only for multithreaded code on Windows.
void _Optlink _exception_dllinit( int (*)(struct _exception *) );
The parameter required is the address of your _matherr function.
Terminating the Environment
You must call matching termination functions for the
initialization functions:
void __ctordtorTerm(int flag);
The argument should have the value zero. It is used only for multithreaded code on Windows.
void _CRT_term(void);
After you have called _CRT_term, you cannot call any other library functions. If your DLL is dynamically linked, you cannot call library functions in the termination section of your _DLL_InitTerm function. If your termination routine requires calling library functions, you must register the termination routine by calling the OS/2 Toolkit function DosExitList. Note that all DosExitList routines are called before DLL termination routines.
Example of an
__DLL_InitTerm Function
Example of an Subsystem
_DLL_InitTerm Function
![]()
Static and Dynamic
Link Libraries
Subsystems
Run-Time
Library Environments
Calling
Conventions
![]()
Build Dynamic Link
Libraries
Initialize and Terminate
Your DLL Environment
Build Subsystems
