Previous Next Up Title Contents Top Library

3.9.4. USING THE C RUN-TIME LIBRARY

This section describes how to use the different forms of the C run-time libraries when building your 32-bit application. It also describes how to specify the entry point when linking a DLL with the run-time libraries.

Three forms of the C run-time library are provided with the WIN32 SDK:

LIBC.LIB

Statically linked library for single-threaded applications.

LIBCMT.LIB

Statically linked library that supports multi-threaded applications

CRTDLL.LIB

Import library for CRTDLL.DLL ( the C run-time DLL) that also supports multi-threaded applications.

Calling the C Run-Time Library from a DLL

When linking a DLL with any of the C run-time libraries, the entry point for the DLL must be the routine _CRT_INIT, or your initialization code must explicitly call _CRT_INIT every time the DLL entry point is called.

To call _CRT_INIT if you do not have your own DLL entry point:

1. Specify _CRT_INIT as the entry point of the DLL

2. Assuming that you have included NTWIN32.MAK (which defines the macros DLLENTRY as @12), add the following option to the DLL's linker command line :

-entry:_CRT_INIT$(DLLENTRY)

To call _CRT_INIT if you have your own DLL entry point :

1. Add the following code in the entry point, using this prototype for _CRT_INIT:

BOOL WINAPI _CRT_INIT(HINSTANCE hinstDLL,

DWORD fdwReason, LPVOID lpReserved);

For information on _CRT_INIT return values, see the documentation for DllEntryPoint

This API returns the same values as _CRT_INT.`

2. On PROCESS_ATTACH and THREAD_ATTACH (see the documentation for the DllEntryPoint API for more information on these flags), call _CRT_INIT at the beginning of the initialisation routine, before any C run-time functions are called or any floating-point operations are performed.

3. Call your own process/thread initialization/terminaison code.

4. On PROCESS_DETTACH and THREAD_DETACH, call _CRT_INIT near the end of the initialization routine , after all C run-time functions are called or any floating-point operations are completed.

Be sure to pass all of the parameters of the entry point to _CRT_INT. Because _CRT_INIT expects these parameters , your application may not work reliably if they are omitted (in particular, fdwReason is required to determine whether process initialization or terminaison is needed).

The following is a sample entry point function that shows how and when to make these calls to _CRT_INIT in the DLL entry point:

BOOL WINAPI DllEntryPoint(HINSTANCE hinstDLL,DWORD fdwReason, LPVOID lpReason)

{

/* Initialize the C run-time before calling any of your code. */

if(fdwReason == DLL_PROCESS_ATTACH | | fdwReserved == THREAD_ATTACH)

if(!_CRT_INIT(hinstDLL,fdwReason,lpReserved))

return (FALSE);

/* Place your DLL's initialization/terminaison code here. */

/* Terlminate the C run-time after all your code */

if(fdwReason == DLL_PROCESS_DETACH | | fdwReason == THREAD_DETACH)

if(!_CRT_INIT(hinstDLL,fdwReason,lpReserved))

return (FALSE);

return( TRUE );

}


Previous Next Up Title Contents Top Library