Server Extension DllMain
A minimal DllMain skeleton for a server-side extension DLL, handling process and thread attach/detach with initialization hooks.
The smallest complete DllMain you can ship in a DLL that needs to do real setup work. On DLL_PROCESS_ATTACH it runs an Initialize routine (storing the module handle, setting up whatever global state the extension needs) and returns FALSE - aborting the load - if that fails; DLL_PROCESS_DETACH mirrors it with UnInitialize. The thread notifications are acknowledged but left empty, which is the correct default: doing heavy work in DLL_THREAD_ATTACH risks deadlocks because the loader lock is held.
//////////////////////////////////////////////////////////////////////////////////////////////
#include <Windows.H>
#include <Stdio.H>
#include <Stdlib.H>
#include "Headers/Definitions.H"
#include "Headers/Functions.H"
#include "Headers/Initialize.H"
#include "Headers/ProcessFilter.H"
#include "Headers/GetDescription.H"
#include "Headers/DisplaySetup.H"
//////////////////////////////////////////////////////////////////////////////////////////////
BOOL APIENTRY DllMain(HANDLE Module, DWORD Reason, LPVOID Reserved)
{
if(Reason == DLL_PROCESS_ATTACH)
{
if(!Initialize(Module))
return FALSE;
return TRUE;
}
if(Reason == DLL_PROCESS_DETACH)
{
if(!UnInitialize(Module))
return FALSE;
return TRUE;
}
if(Reason == DLL_THREAD_ATTACH)
{
return TRUE;
}
if(Reason == DLL_THREAD_DETACH)
{
return TRUE;
}
return TRUE;
}
Rules of thumb this structure illustrates: keep DllMain fast and free of calls into other DLLs (the loader lock makes that dangerous), capture Module during attach so later code can find its resources, and always pair attach-time allocation with detach-time cleanup. It pairs with the DLL Skeleton article, which shows what the exported functions look like once this shell is in place.