Syntax
int _CRT_init(void); /* no header file - defined in the run time startup code */Description
By default, all DLLs call The Developer's Toolkit _DLL_InitTerm function, which in turn calls _CRT_init for you. However, if you are writing your own _DLL_InitTerm function (for example, to perform actions other than run time initialization and termination), you must call _CRT_init from your version of _DLL_InitTerm before you can call any other run-time functions.
If your DLL contains C++ code, you must also call __ctordtorInit after _CRT_init to ensure that static constructors and destructors are initialized properly. __ctordtorInit is defined in the run-time startup code as:
void __ctordtorInit(void);
Note: If you are providing your own version of the _matherr function to be used in your DLL, you must also call the _exception_dllinit function after the run-time environment is initialized. Calling this function ensures that the proper _matherr function will be called during exception handling. _exception_dllinit is defined in the run-time startup code as:
void _exception_dllinit( int (*)(struct exception *) );
The parameter required is the address of your _matherr function.
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 usual destination of stderr.
The following example shows the _DLL_InitTerm function from The Developer's Toolkit sample program for building DLLs, which calls _CRT_init to initialize the library environment.
#define INCL_DOSMODULEMGR
#define INCL_DOSPROCESS
#include <os2.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/* _CRT_init is the C run-time environment initialization function. */
/* It will return 0 to indicate success and -1 to indicate failure. */
int _CRT_init(void);
#ifdef STATIC_LINK
/* _CRT_term is the C run-time environment termination function. */
/* It only needs to be called when the C run-time functions are statically */
/* linked. */
void _CRT_term(void);
#else
/* A clean up routine registered with DosExitList must be used if run-time */
/* calls are required and the run time is dynamically linked. This will */
/* guarantee that this clean up routine is run before the library DLL is */
/* terminated. */
static void cleanup(ULONG ulReason);
#endif
size_t nSize;
int *pArray;
/* _DLL_InitTerm is the function that gets called by the operating system */
/* loader when it loads and frees this DLL for each process that accesses */
/* this DLL. However, it only gets called the first time the DLL is loaded */
/* and the last time it is freed for a particular process. The system */
/* linkage convention MUST be used because the operating system loader is */
/* calling this function. */
unsigned long _DLL_InitTerm(unsigned long hModule, unsigned long
ulFlag)
{
size_t i;
APIRET rc;
char namebuf[CCHMAXPATH];
/* If ulFlag is zero then the DLL is being loaded so initialization should*/
/* be performed. If ulFlag is 1 then the DLL is being freed so */
/* termination should be performed. */
switch (ulFlag) {
case 0 :
/*******************************************************************/
/* The C run-time environment initialization function must be */
/* called before any calls to C run-time functions that are not */
/* inlined. */
/*******************************************************************/
if (_CRT_init() == -1)
return 0UL;
#ifndef STATIC_LINK
/********************************************************************/
/* A DosExitList routine must be used to clean up if run-time calls */
/* are required and the run time is dynamically linked. */
/********************************************************************/
if (rc = DosExitList(0x0000FF00|EXLST_ADD, cleanup))
printf("DosExitList returned %lu\n", rc);
#endif
if (rc = DosQueryModuleName(hModule, CCHMAXPATH, namebuf))
printf("DosQueryModuleName returned %lu\n", rc);
else
printf("The name of this DLL is %s\n", namebuf);
srand(17);
nSize = (rand()%128)+32;
printf("The array size for this process is %u\n", nSize);
if ((pArray = malloc(nSize *sizeof(int))) == NULL) {
printf("Could not allocate space for unsorted array.\n");
return 0UL;
}
for (i = 0; i < nSize; ++i)
pArray[i] = rand();
break;
case 1 :
#ifdef STATIC_LINK
printf("The array will now be freed.\n");
free(pArray);
_CRT_term();
#endif
break;
default :
printf("ulFlag = %lu\n", ulFlag);
return 0UL;
}
/* A non-zero value must be returned to indicate success. */
return 1UL;
}
#ifndef STATIC_LINK
static void cleanup(ULONG ulReason)
{
if (!ulReason) {
printf("The array will now be freed.\n");
free(pArray);
}
DosExitList(EXLST_EXIT, cleanup);
return ;
}
#endif
Related Information