This is a major drawback if you create deviceobject in DriverEntry and
delete it unload routine in a PNP driver. Every time a device is removed,
the PnP manager makes an attempt to unload it’s driver, and if there are any
deviceobjects associated with the DriverObject (DriverObject->DeviceObject
!= NULL), it skips the unload.
One solution to this would be to create the control deviceobject when the
AddDevice is called for the first time, and delete it when the
IRP_MN_REMOVE_DEVICE is called for the last time. You can track this
ADD/REMOVE sequence with a global counter. Here is how I would do this:
ULONG DeviceCount = 0;
AddDevice()
{
Create the filter device and attach it to the PDO;
if( 1 == InterlockedIncrement(&DeviceCount)) {
//
// This is a first call to add device, so create the
deviceobject
//
Create ControlDeviceObject;
Create Symbolic link for it;
//
// If an error happens here, don’t fail the AddDevice. That
// would stop the loading of entire devicestack. Just log
// the error and move on.
}
}
PnPHandler ()
{
case IRP_MN_REMOVE_DEVICE:
Detach and delete the filter deviceobject;
if(0 == InterlockedDecrement(&DeviceCount)) {
//
// This is a last call to remove device, so delete
the control
// so that our driver can be unloaded.
//
Delete the controlDeviceobject;
Delete the symbolicLink;
}
}
The interlocked operation takes care of race condition if your filter
belongs to a stack that talks to multiple devices. Let us know if this helps
solve your problem.
-Eliyas
-----Original Message-----
From: xxxxx@asia.adaptec.com [mailto:xxxxx@asia.adaptec.com]
Sent: Thursday, February 22, 2001 6:00 PM
To: NT Developers Interest List
Subject: [ntdev] How to call unload routine of WDM driver
Hi everyone,
Does anybody know how to call the unload routine of a WDM filter driver
from a user-mode application or the driver itself to release all resource
and completely unload the driver. Because I create one control device
object in my filter driver’s entry routine, the unload routine can not
automatically called when all devices are removed since the control object
hasn’t been deleted yet and it will only be deleted in the unload routine.
Thank you in advance for your help.
Regards,
Shunnian
You are currently subscribed to ntdev as: xxxxx@microsoft.com
To unsubscribe send a blank email to leave-ntdev-$subst(‘Recip.MemberIDChar’)@lists.osr.com
You are currently subscribed to ntdev as: $subst(‘Recip.EmailAddr’)
To unsubscribe send a blank email to leave-ntdev-$subst(‘Recip.MemberIDChar’)@lists.osr.com