In a Windows DLL, the DllMain function is automatically run when the DLL is loaded, just before it is unloaded, and whenever a process thread is started or exits cleanly.