Writing an Unload Routine

< BACK  NEXT >
[oR]

By default, once the driver is loaded, it remains in the system until a reboot occurs. To make a driver unloadable, an Unload routine is necessary. The Unload routine is announced during DriverEntry. The I/O Manager then calls this routine whenever the driver is manually or automatically unloaded.

Execution Context

The I/O Manager calls a driver's Unload routine just before removing the driver from memory. Table 6.4 shows that the Unload routine runs at PASSIVE_LEVEL IRQL, which means it has access to paged system resources.

What an Unload Routine Does

Although the exact details vary from driver to driver, in general the following steps are performed in an Unload routine.

Table 6.4. Function Prototype for Unload Routine
VOID Unload IRQL == PASSIVE_LEVEL
Parameter Description
IN PDRIVER_OBJECT pDriverObject Pointer to driver object for this driver
Return value (void)

  1. For some kinds of hardware, the state of the device should be saved in the Registry. That way, the device can be restored to its last known state the next time DriverEntry executes. For example, an audio card driver might save the current volume setting of the card.

  2. If interrupts have been enabled for the device, the Unload routine must disable them and disconnect from its interrupt object. It is crucial that the device not generate any interrupt requests once the interrupt object is deleted.

  3. Hardware belonging to the driver must be deallocated.

  4. The symbolic link name must be removed from the Win32 namespace. This is accomplished using IoDeleteSymbolicLink.

  5. The device object itself must be removed using IoDeleteDevice.

  6. If managing multiunit controllers, repeat steps 4 and 5 for each device attached to the controller. Then remove the controller object itself, using IoDeleteController.

  7. Repeat steps 4 through 6 for all controllers and devices that belong to the driver.

  8. Deallocate any pool memory held by the driver.

It is important to note that for WDM drivers, the responsibilities of Unload are performed in the RemoveDevice routine.

One final note: A driver's Unload routine is not called at system shutdown time. Any special work required during system shutdown must be performed inside of a separate shutdown routine.

< BACK  NEXT >


The Windows 2000 Device Driver Book(c) A Guide for Programmers
The Windows 2000 Device Driver Book: A Guide for Programmers (2nd Edition)
ISBN: 0130204315
EAN: 2147483647
Year: 2000
Pages: 156

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net