The DLL can make an entry point function, if the entry point function is specified, the system calls the entry point function when the process or thread is loaded or unloading the DLL. This can be used to achieve simple initialization and clear tasks. For example, when the new thread is created, the thread local memory can be set, and then the local storage is cleared when the thread is terminated.
If you use C to run the library connection DLL, you will automatically provide an entry point function and allow a separate initialization function to be provided. Or look at the documentation of the runtime library for more information.
If you provide your own entry point, see the dllmain function. DllMain is a user-defined function name placeholder. When you actually generate a DLL, you must specify its actual name. For more information, see the relevant documents for the development tool you are using.
Call entry point function
The system will call the entry point function when the following event occurs:
The process is loaded with a DLL. For programs dynamically connected when loaded, the DLL is loaded by process initialization. For programs connected to running, DLL is loaded before calling LoadLibrary or LoadLibraryEx. When the process is uninstalling the DLL. The process terminates or calls the FreeElibrary function, and the reference count is 0, the DLL is uninstalled. But if the process is terminated as the result of the TerminateProcess or TerminateThread function, the system does not call the entry point function. The new thread is created, but the thread has been loaded into the DLL. You can use the DisabletHreadLibraryCalls function to prohibit notification when you create a DISABLETHREADLIBRARYCALLS function. The threads in the process that have been loaded into the DLL are normally terminated instead of terminating with TerminateThread or TerminateProcess. When the process is uninstalling the DLL, the entire process only calls the entry point function once, not the thread owned by the process once. Notifications can be disabled via DisabletReadlibraryCalls when thread is terminated.
There is only one thread to call the entry point function each time.
The entry point function will call the entry point function when the function of the process or thread is called, and the E-entry point function of the DLL is allowed to allocate memory or open the handle of the access program in the virtual address space of the calling process. The entry point function can also allocate memory space for the new thread through the thread part memory (TLS). For more information on thread partial memory, refer to "Local Memory".
Define entry point functions
The DLL entry point function must be declared in accordance with the standard call agreement. If the declaration is incorrect, the DLL will not be loaded, and the system will indicate that the DLL entry point function must be declared with WinAPI.
Windows Me / 98/95: If the DLL entry point declaration is incorrect, the DLL will not load, the system displays the message starting with "ERROR Starting Program" and indicates that the user needs to check the file to correct the error.
In the main part of the function, you can handle the following entry point calls:
One process loads DLL (DLL_PROCESS_ATTACH). The current process creates a new thread (DLL_THREAD_ATTACH). The thread is normal to exit (DLL_THREAD_DETACH). Process Uninstall DLL (DLL_Process_Detach).
The entry point should only implement some simple initialization tasks, and cannot call the LoadLibrary or LoadLibraryEx function (or other profile to call these functions), because this may cause a dependency cycle on the order in the order in the order. This can cause the system to perform the initialization code before being used. The same entry point function cannot call the Freelibrary function (or indirectly call the Freelibrary function), which is due to the DLL after the system performs termination code.
Calling other functions in kernel32.dll are secure, because when the entry point function is called, the DLL can be guaranteed to be loaded into the address space of the process. Usually we create synchronous objects such as critical regions and muters in the entry point function, and use TLS. Do not use registration functions because they are in Advapi32.dll. If you want to dynamically link C runns, don't call Malloc, and you should use HeapAlloc. Calling the imported function is different from kernel32.dll, which may have some problems and difficult to diagnose. For example, when calling USER, Shell, and COM functions, the access violation is caused, because some functions in these DLLs use LoadLibrary to call other system components.
The next example demonstrates how to build a DLL entry function:
Bool WinApi Dllmain
Hinstance Hinstdll, // Handle to DLL Module
DWord Fdwreason, // Reason for Calling Function
LPVOID LPRESERVED) // Reserved
{
// Perform ActionS based on the reason for calling.
Switch (FDWREASON)
{
Case DLL_Process_attach:
// Initialize Once for Each New Process.
// Return False to fail DLL LOAD.
Break;
Case DLL_THREAD_ATTACH:
// do thread-specific initialization.
Break;
Case DLL_THREAD_DETACH:
// do thread-specific cleanup.
Break;
Case DLL_PROCESS_DETACH:
// Perform Any Necessary Cleanup.
Break;
}
Return True; // Successful DLL_Process_attach.
}
Entry point function return value
When a DLL entry point function is called when the program is called, the function returns TRUE to indicate that the call is successful. For programs that use load-in, false return values will cause the program to initialize failure and terminate. For programs that use runtime links, the return value will cause the LoadLibrary or LoadLibraryEx function to return NULL, indicating the failure. (The system will call the entry point function with DLL_PROCESS_DETACH and uninstall the DLL) If the entry point function is called for other reasons, the return value of the function will be ignored.