The Windows DllMain() function, the DLL program skeleton
// Create a new project. During the second page Win32 Application Wizard, the Application Settings, select DLL for the Application type. Then add the source file as usual.
#include <windows.h>
#include <stdio.h>
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:
{
printf("Initialize once for each new process...\n");
// Initialize once for each new process.
// Return FALSE to fail DLL load.
break;
}
case DLL_THREAD_ATTACH:
{
printf("Do thread-specific initialization...\n");
// Do thread-specific initialization.
break;
}
case DLL_THREAD_DETACH:
{
printf("Do thread-specific cleanup...\n");
// Do thread-specific cleanup.
break;
}
case DLL_PROCESS_DETACH:
{
printf("Perform any necessary cleanup...\n");
// Perform any necessary cleanup.
break;
}
}
// Successful DLL_PROCESS_ATTACH.
return TRUE;
}
Output example:
// We need other program to call/run/link this DLL...
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows Xp Pro SP2
Target platform: none, just for learning and fun
Header file: Standard and Windows
Additional library: Windows Platform SDK
Additional project setting: Set project to be compiled as C.
Project->your_project_name Properties->Configuration Properties->C/C++->Advanced->Compiled As: Compiled as C Code (/TC)
Other info: This is a DLL project. non-CLR or unmanaged. Need to add Advapi32.lib (Advapi32.dll) to the project. Click the Project menu->Select the your_project_name Properties... sub menu->Expand the Configuration Properties folder on the left pane->Expand the Linker subfolder->Select the Input subfolder->Select the Additional Dependencies field on the right pane->Click the ... at the end of the field->Type in 'Advapi32.lib' in the empty pane->Click the OK button->Click the OK button second time to close the project Properties dialog.
To do: The Windows DllMain() function C program skeleton example
To show: Creating a very simple Windows DLL main entry point: the DllMain()