LibMain

2.x

  int CALLBACK LibMain(hinst, wDataSeg, cbHeapSize, lpszCmdLine)    
  HINSTANCE hinst; /* handle of library instance */
  WORD wDataSeg; /* library data segment */
  WORD cbHeapSize; /* default heap size */
  LPSTR lpszCmdLine; /* command-line arguments */

The LibMain function is called by the system to initialize a dynamic-link library (DLL). A DLL must contain the LibMain function if the library is linked with the file LIBENTRY.OBJ.

Parameters

hinst

Identifies the instance of the DLL.

wDataSeg

Specifies the value of the data segment (DS) register.

cbHeapSize

Specifies the 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

Points to a null-terminated string specifying command-line information. This parameter is rarely used by DLLs.

Return Value

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

Comments

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 the LibMain function.

Example

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;

    /* Has the library been initialized yet? */

    if (hinstLib == NULL) {
        hgblClassStruct = GlobalAlloc(GHND, sizeof(WNDCLASS));
        if (hgblClassStruct != NULL) {
            lpClassStruct = (LPWNDCLASS) GlobalLock(hgblClassStruct);
            if (lpClassStruct != NULL) {

                /* Define the class attributes. */

                lpClassStruct->style = CS_HREDRAW | CS_VREDRAW |
                    CS_DBLCLKS | CS_GLOBALCLASS;
                lpClassStruct->lpfnWndProc = DllWndProc;
                lpClassStruct->cbWndExtra = 0;
                lpClassStruct->hInstance = hinst;
                lpClassStruct->hIcon = NULL;
                lpClassStruct->hCursor = LoadCursor(NULL, IDC_ARROW);
                lpClassStruct->hbrBackground =
                    (HBRUSH) (COLOR_WINDOW + 1);
                lpClassStruct->lpszMenuName = NULL;
                lpClassStruct->lpszClassName = "MyClassName";

                hinstLib = (RegisterClass(lpClassStruct)) ?
                    hinst : NULL;

                GlobalUnlock(hgblClassStruct);
            }

            GlobalFree(hgblClassStruct);
        }
    }
    return (hinstLib ? 1 : 0);  /* return 1 = success; 0 = fail */
}

See Also

GlobalAlloc, GlobalFree, GlobalLock, GlobalUnlock, WEP