API Entry DllMain

Can someone explain, in layman's terms, what the purpose of the code below is. Is it required for the DLL to work properly?
1
2
3
4
5
6
7
8
9
10
11
 BOOL APIENTRY DllMain(HANDLE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved) {
	switch (ul_reason_for_call) {
	case DLL_PROCESS_ATTACH:
	case DLL_THREAD_ATTACH:
	case DLL_THREAD_DETACH:
	case DLL_PROCESS_DETACH:
		break;
	}

	return TRUE;
}
DllMain is executed when a process starts/terminates or a thread starts/terminates. If you don't need code for these events then you don't need a DllMain and the rest of the dll will work just fine. In the code given above DllMain has no effect and can be removed. See
https://learn.microsoft.com/en-us/windows/win32/dlls/dllmain
Last edited on
Note that each DLL has its own separate DllMain function, which will be called when certain events happen; fdwReason indicates the event. The function will be called when the DLL is loaded into a process (DLL_PROCESS_ATTACH), when the DLL is about to be unloaded from the process (DLL_PROCESS_DETACH) and also when threads are started (DLL_THREAD_ATTACH) or terminated (DLL_THREAD_DETACH).

The function is supposed to return TRUE, when the event was handled successfully successfully.

The code you have posted is just an "empty" DllMain function that doesn't really do anything – yet. You can use that as a "template" and add whatever initialization or clean-up actions that your DLL may need. Of course, you can just leave it empty 😏
Last edited on
Thank you for the comments.
Topic archived. No new replies allowed.