Microsoft Windows CE 3.0  

DllMain

Important:
This is retired content. This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This content may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

This function is an optional method of entry into a dynamic-link library (DLL). If the function is used, it is called by the system when processes and threads are initialized and terminated, or upon calls to the LoadLibraryand FreeLibraryfunctions.

DllMainis a placeholder for the library-defined function name. You must specify the actual name you use when you build your DLL. For more information, see the documentation included with your development tools.

BOOL WINAPI DllMain( HANDLE

hinstDLL
, DWORD

dwReason
, LPVOID

lpvReserved
);

Parameters

hinstDLL
Handle to the DLL. The value is the base address of the DLL. The HINSTANCE of a DLL is the same as the HMODULE of the DLL, so hinstDLLcan be used in subsequent calls to the GetModuleFileNamefunction and other functions that require a module handle.
dwReason
Specifies a flag indicating why the DLL entry-point function is being called. This parameter can be one of the following values:
Value Description
DLL_PROCESS_ATTACH Indicates that the DLL is being loaded into the virtual address space of the current process as a result of the process starting up or as a result of a call to LoadLibrary. DLLs can use this opportunity to initialize any instance data or to use the TlsAllocfunction to allocate a thread local storage (TLS) index.
DLL_THREAD_ATTACH Indicates that the current process is creating a new thread. When this occurs, the system calls the entry-point function of all DLLs currently attached to the process. The call is made in the context of the new thread. DLLs can use this opportunity to initialize a TLS slot for the thread. A thread calling the DLL entry-point function with DLL_PROCESS_ATTACH does not call the DLL entry-point function with DLL_THREAD_ATTACH.
  Note that the entry-point of a DLL function is called with this value only by threads created after the DLL is loaded by the process. When a DLL is loaded using LoadLibrary, existing threads do not call the entry-point function of the newly loaded DLL.
DLL_THREAD_DETACH Indicates that a thread is exiting cleanly. If the DLL has stored a pointer to allocated memory in a TLS slot, it uses this opportunity to free the memory. The system calls the entry-point function of all currently loaded DLLs with this value. The call is made in the context of the exiting thread.
DLL_PROCESS_DETACH Indicates that the DLL is being unloaded from the virtual address space of the calling process as a result of either a process exit or a call to FreeLibrary. The DLL can use this opportunity to call the TlsFreefunction to free any TLS indices allocated by using TlsAllocand to free any thread local data.
lpvReserved
Specifies further aspects of DLL initialization and cleanup.

If dwReasonis DLL_PROCESS_ATTACH, lpvReservedis NULL for dynamic loads and non-NULL for static loads.

If dwReasonis DLL_PROCESS_DETACH, lpvReservedis NULL if DllMainhas been called by using FreeLibraryand non-NULL if DllMainhas been called during process termination.

Return Values

When the system calls the DllMainfunction with the DLL_PROCESS_ATTACH value, the function returns TRUE if it succeeds or FALSE if initialization fails. If the return value is FALSE when DllMainis called because the process uses the LoadLibraryfunction, LoadLibraryreturns NULL. If the return value is FALSE when DllMainis called during process initialization, the process terminates with an error. To get extended error information, call GetLastError.

When the system calls the DllMainfunction with any value other than DLL_PROCESS_ATTACH, the return value is ignored.

Remarks

During initial process startup or after a call to LoadLibrary, the system scans the list of loaded DLLs for the process. For each DLL that has not already been called with the DLL_PROCESS_ATTACH value, the system calls the entry-point function of the DLL. This call is made in the context of the thread that caused the process address space to change, such as the primary thread of the process or the thread that called LoadLibrary.

There are cases in which the entry-point function is called for a terminating thread even if the DLL never attached to the thread — for example, the entry-point function was never called with the DLL_THREAD_ATTACH value in the context of the thread in either of these two situations:

  • The thread was the initial thread in the process, so the system called the entry-point function with the DLL_PROCESS_ATTACH value.
  • The thread was already running when a call to the LoadLibraryfunction was made, so the system never called the entry-point function for it.

    When a DLL is unloaded from a process as a result of process termination or as a result of a call to FreeLibrary, the system does not call the entry-point function of the DLL with the DLL_THREAD_DETACH value for the individual threads of the process. The DLL is only given DLL_PROCESS_DETACH notification. DLLs can take this opportunity to clean up all resources for all threads known to the DLL.

    On attach, the body of your DLL entry-point function should perform only simple initialization tasks, such as setting up thread local storage (TLS), creating synchronization objects, and opening files. You must not call LoadLibraryin the entry-point function, because you may create dependency loops in the DLL load order. This can result in a DLL being used before the system has executed its initialization code. Similarly, you must not call the FreeLibraryfunction in the entry-point function on detach, because this can result in a DLL being used after the system has executed its termination code.

    Calling Win32 functions other than TLS, object-creation, and file functions may result in problems that are difficult to diagnose. For example, calling User, Shell, COM, and Windows Sockets functions (or any functions that call these functions) can cause access violation errors, because their DLLs call LoadLibraryto load other system components. While it is acceptable to create synchronization objects in DllMain, you should not perform synchronization in DllMain(or a function called by DllMain) because all calls to DllMainare serialized. Waiting on synchronization objects in DllMaincan cause a deadlock.

    To provide more complex initialization, create an initialization routine for the DLL. You can require applications to call the initialization routine before calling any other routines in the DLL. Otherwise, you can have the initialization routine create a named mutex, and have each routine in the DLL call the initialization routine if the mutex does not exist.

    Requirements

    Runs On Versions Defined in Include Link to
    Windows CE OS 1.0 and later Winbase.h   Coredll.lib, Nk.lib
    Note   This API is part of the complete Windows CE OS package as provided by Microsoft. The functionality of a particular platform is determined by the original equipment manufacturer (OEM) and some devices may not support this API.

    See Also

    FreeLibrary, GetModuleFileName, GetLastError, LoadLibrary, TlsAlloc, TlsFree