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: $subst(‘Recip.EmailAddr’)
To unsubscribe send a blank email to leave-ntdev-$subst(‘Recip.MemberIDChar’)@lists.osr.com

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

Hi, Eliyas,

Thank you for your detailed explanation and good solution.

Actually, I have already solved my problem yesterday. My solution is
similar with yours. I still create the control object in DriverEntry but
delete it in IRP_MN_REMOVE_DEVICE routine when the last attached device is
removed.

However, originally I want to send a DeviceIoControl to filter driver to
get the removed device information, such as the number of removed devices
and which one is removed. Ideally, the deletion of control object is in
unload routine, so I still can send DeviceIoControl to it when last device
is removed.

With this new solution, although I have solved the driver unload problem, I
have to use other approach (a complex one) in the application to get the
needed information. Luckily, it can work so far.

If you have other solution solving my problem in filter driver, please let
me know. I will appreciate your help!

Best Regards,
Shunnian

On 02/26/01, “Eliyas Yakub ” wrote:
> 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


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