The LibMain function is called by the system to initialize a dynamic-link library (DLL). A DLL must contain LibMain if the library is linked with the file Libentry.obj.
int CALLBACK LibMain(HINSTANCE hinst, WORD wDataSeg, WORD cbHeapSize, LPSTR lpszCmdLine);
Platforms: | H/PC |
Windows CE versions: | 1.0 and later |
The function should return 1 if it is successful. Otherwise, it should return 0.
The LibMain function is called by LibEntry, which is called by Windows when the DLL is loaded. The LibEntry routine is provided in the LIBENTRY.OBJ module. LibEntry initializes the DLL's heap (if a HEAPSIZE value is specified in the DLL's module-definition file) before calling LibMain.
The following example shows a typical LibMain function:
int CALLBACK LibMain (HINSTANCE hinst, WORD wDataSeg, WORD cbHeap, LPSTR lpszCmdLine)
{
HGLOBAL hgblClassStruct;
LPWNDCLASS lpClassStruct;
static HINSTANCE hinstLib = NULL;
/* Has the library been initialized yet? */
if (hinstLib == NULL)
{
hgblClassStruct = LocalAlloc(0, sizeof(WNDCLASS));
if (hgblClassStruct != NULL)
{
lpClassStruct = (LPWNDCLASS) LocalLock(hgblClassStruct);
if (lpClassStruct != NULL)
{
/* Define the class attributes. */
lpClassStruct->style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS | CS_GLOBALCLASS;
lpClassStruct->lpfnWndProc = (WNDPROC)DllWndProc;
lpClassStruct->cbWndExtra = 0;
lpClassStruct->hInstance = hinst;
lpClassStruct->hIcon = NULL;
lpClassStruct->hCursor = NULL;
lpClassStruct->hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
lpClassStruct->lpszMenuName = NULL;
lpClassStruct->lpszClassName = TEXT("MyClassName");
hinstLib = (RegisterClass(lpClassStruct)) ? hinst : NULL;
LocalUnlock(lpClassStruct);
}
}
}
return 1 ;
}