LibMain

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.

Syntax

int CALLBACK LibMain(HINSTANCE hinst, WORD wDataSeg, WORD cbHeapSize, LPSTR lpszCmdLine);

At a Glance

Platforms: H/PC
Windows CE versions: 1.0 and later

Parameters

hinst
Handle to the instance of the DLL.
wDataSeg
Value of the data segment (DS) register.
cbHeapSize
Size of the heap defined in the module-definition file. (The LibEntry routine in LIBENTRY.OBJ uses this value to initialize the local heap.)
lpszCmdLine
Pointer to a null-terminated string specifying command-line information. This parameter is rarely used by DLLs.

Return Values

The function should return 1 if it is successful. Otherwise, it should return 0.

Remarks

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 ;
  }