Microsoft KB Archive/177917

From BetaArchive Wiki
Knowledge Base


Initializing a DLL Using DLLMain Optional Entry Point

Article ID: 177917

Article Last Modified on 10/31/2006



APPLIES TO

  • Microsoft Windows NT Workstation 4.0 Developer Edition
  • Microsoft Windows NT Server 4.0 Standard Edition



This article was previously published under Q177917

SUMMARY

In Win32, all dynamic link libraries (DLLs) may contain an optional entry- point function (usually called DllMain) that is called for both initialization and termination. This gives you an opportunity to allocate or release additional resources as needed. Windows calls the entry-point function in four situations: process attach, process detach, thread attach, and thread detach.

MORE INFORMATION

The DllEntryPoint function is an optional method of entry into a 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 LoadLibrary and FreeLibrary functions.

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 LoadLibrary in 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 carried out its initialization code.

Calling Windows functions other than TLS, synchronization, and file functions may result in problems that are difficult to diagnose. For example, calling User, Shell, COM, RPC, and Windows Sockets functions (or any functions that call these functions) can cause access violation errors, because their DLLs call LoadLibrary to load other system components.


Keywords: kbapi kbinfo kbnetwork kbrpc KB177917