Code Example: Driver Unload

< BACK  NEXT >
[oR]

In the Minimal driver, an Unload routine is supplied that undoes the work of DriverEntry. Its work is straightforward, as it must delete each symbolic link and device object that has been created. To perform this work, the Unload routine relies on the fact that the driver object points to a linked list of device objects controlled by the driver.

The first device controlled by a driver is pointed to by the field DeviceObject within the driver object. Each device points to the next via the field NextDevice. When examining the Unload routine, remember that the Minimal DEVICE_EXTENSION structure maintains a back pointer to the parent device object.

 //++ // Function:  DriverUnload // // Description: //        Stops & Deletes devices controlled by this driver. //        Stops interrupt processing (if any) //        Releases kernel resources consumed by driver // // Arguments: //        pDriverObject - Passed from I/O Manager // // Return value: //        None // VOID DriverUnload (           IN PDRIVER_OBJECT pDriverObject ) {                 PDEVICE_OBJECT pNextObj;            // Loop through each device controlled by Driver      pNextObj = pDriverObject->DeviceObject;      while (pNextObj != NULL) {           // Dig out the Device Extension from the           // Device Object           PDEVICE_EXTENSION pDevExt = (PDEVICE_EXTENSION)                pNextObj->DeviceExtension;           // This will yield the symbolic link name           UNICODE_STRING pLinkName =                pDevExt->ustrSymLinkName;           // ... which can now be deleted           IoDeleteSymbolicLink(&pLinkName);           // a little trickery...           // we need to delete the device object, BUT           // the Device object is pointed to by pNextObj           // If we delete the device object first,           // we can't traverse to the next Device in the list           // Rather than create another pointer, we can           // use the DeviceExtension's back pointer           // to the device.           // So, first update the next pointer...           pNextObj = pNextObj->NextDevice;           // then delete the device using the Extension           IoDeleteDevice( pDevExt->pDevice );      }      // Finally, hardware that was allocated in DriverEntry      // would be released here using      // IoReportResourceUsage } 
< 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