25.2 Creating an Installable Driver

An installable driver is a Windows dynamic-link library (DLL) that supports a special entry point, the DriverProc function. This function processes the driver messages described in the previous section. This function may also process pri-vate driver messages. These messages can be assigned values ranging from DRV_RESERVED to DRV_USER (two constants that appear in WINDOWS.H).

The following example shows the basic structure of the DriverProc function:

LRESULT CALLBACK* DriverProc (DWORD   dwDriverIdentifier,
                              HDRVR   hDriver,
                              UINT    wMessage,
                              LPARAM  lParam1,
                              LPARAM  lParam2)
{
DWORD dwRes = 0L;

switch (wMessage)
    {

    case DRV_LOAD:

        /* Sent when the driver is loaded. This is always   */
        /* the first message received by a driver.          */

        dwRes = 1L;     /* Return 0L to fail.               */
        break;

    case DRV_FREE:

        /* Sent when the driver is about to be discarded.   */
        /* This is the last message a driver receives       */
        /* before it is freed.                              */

        dwRes = 1L;     /* Return value is ignored.         */
        break;

    case DRV_OPEN:

        /* Sent when the driver is opened.                  */

        dwRes = 1L;     /* Return 0L to fail.               */
                        /* This value is subsequently used  */
                        /* for dwDriverIdentifier.          */

        break;

    case DRV_CLOSE:

        /* Sent when the driver is closed. Drivers are      */
        /* unloaded when the open count reaches zero.       */

        dwRes = 1L;     /* Return 0L to fail.               */
        break;



    case DRV_ENABLE:

        /* Sent when the driver is loaded or reloaded and   */
        /* when Windows is enabled. Hook or rehook          */
        /* interrupts and initialize hardware. Expect the   */
        /* driver to be in memory only between the enable   */
        /* and disable messages.                            */

        dwRes = 1L;     /* Return value is ignored.         */
        break;

    case DRV_DISABLE:

        /* Sent before the driver is freed or when Windows  */
        /* is disabled. Unhook interrupts and place         */
        /* peripherals in an inactive state.                */

        dwRes = 1L;     /* Return value is ignored.         */
        break;

    case DRV_INSTALL:

        /* Sent when the driver is installed.               */

        dwRes = DRV_OK; /* Can also return DRV_CANCEL       */
                        /* and DRV_RESTART.                 */
        break;

    case DRV_REMOVE:

        /* Sent when the driver is removed.                 */

        dwRes = 1L;     /* Return value is ignored.         */
        break;

    case DRV_QUERYCONFIGURE:

        /* Sent to determine if the driver can be           */
        /* configured.                                      */

        dwRes = 0L;     /* Zero indicates configuration     */
                        /* NOT supported.                   */
        break;

    case DRV_CONFIGURE:

        /* Sent to display the custom-configuration         */
        /* dialog box for the driver.                       */

        dwRes = DRV_OK; /* Can also return DRV_CANCEL       */
                        /* and DRV_RESTART.                 */
        break;

    default:

        /* Process any messages not explicitly trapped.     */

        return DefDriverProc (dwDriverIdentifier, hDriver,
                             wMessage, lParam1, lParam2);

    }
return dwRes;
}