### Initialize and Terminate the DLL Environment

Before you can call any IBM C and C++ Compilers library functions, you must initialize the runtime environment. The initialization and termination entry point for a DLL is the _DLL_InitTerm function. When each new process gains access to the DLL, this function initializes the necessary environment for the DLL, including storage, semaphores, and variables. When each process frees its access to the DLL, the _DLL_InitTerm function terminates the DLL environment created for that process.

Initializing the Environment
Use the function _CRT_init, which is provided in the runtime libraries. The prototype for this function is:

       int _CRT_init(void);

If the runtime environment is successfully initialized, _CRT_init returns 0. A return code of -1 indicates an error. If an error occurs, a message is written to file handle 2, which is the usual destination of stderr.

If your DLL contains C++ code, you must also call __ctordtorInit after _CRT_init to ensure that static constructors and destructors are initialized properly. The prototype for __ctordtorInit is:

       void __ctordtorInit(void);

Note: If you are providing your own version of the _matherr function to be used in your DLL, you must call the _exception_dllinit function after the runtime environment is initialized, to ensure that the proper _matherr function will be called during exception handling. The prototype for this function is:

       void _Optlink _exception_dllinit( int (*)(struct exception *) );

The parameter required is the address of your _matherr function.

Terminating the Environment
If your DLL is statically linked, you must use the _CRT_term function to correctly terminate the C runtime environment. The _CRT_term function is provided in the IBM C and C++ Compilers runtime libraries. It has the following prototype:

       void _CRT_term(void);

If your DLL contains C++ code, you must also call __ctordtorTerm before you call _CRT_term to ensure that static constructors and destructors are terminated correctly. The prototype for __ctordtorTerm is:

       void __ctordtorTerm(void);

Once 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 with DosExitList. Note that all DosExitList routines are called before DLL termination routines.

WriteYour Own _DLL_InitTerm Function
WriteYour Own _DLL_InitTerm Function

The _DLL_InitTerm Function
Dynamic Link Libraries