如何按需停止即插即用过滤器驱动程序?

How to stop a Plug-and-Play filter driver on demand?

我正在 Windows 下开发过滤驱动程序。该驱动程序可感知 PnP 以接收设备通知,它还创建一个控制设备以通过 IOCTL 与伴随的用户模式服务进行通信。

据我了解,驱动程序的生命周期由 PnP-manager 控制。在处理下一个设备通知后卸载驱动程序,因为此时驱动程序不拥有任何设备对象。

我需要做的是按需停止驱动程序,而不是下一个设备arrives/departures。到目前为止,我认为这不是它应该工作的方式,另一方面,我没有看到任何禁止按需卸载过滤器驱动程序的逻辑。我研究了发送 STOP 控制,但我没有看到如何在 PnP 驱动程序中处理它(仅适用于旧版驱动程序)。

现在我正在考虑添加特殊的 IOCTL 处理程序,这将关闭所有设备对象。但这还不够,驱动程序的生命周期由 PnP-manager 管理,所以我需要以某种方式将管理器 "bring attention" 分配给我的驱动程序。感谢您的帮助!

如果您想知道,我需要驱动程序可以停止,以便卸载不需要重新启动。

WDM 过滤器驱动程序始终将自己的设备附加到设备堆栈。在此之后,另一个设备可以将自己附加到这个堆栈 - 所以它将附加到您的设备。之后这个驱动程序已经无法卸载了。

只有在不再引用驱动程序时才能卸载驱动程序DRIVER_OBJECT 为此,您需要从设备堆栈中分离并销毁所有 DEVICE_OBJECTs. only one correct way for WDM filter driver do this - when you handle IRP_MN_REMOVE_DEVICE - read Removing a Device in a Filter Driver or as alternative you can register FAST_IO_DISPATCH with FastIoDetachDevice in driver - as result FastIoDetachDevice will be called when will be called IoDeleteDevice for DeviceObject to which you attached (this is also during IRP_MN_REMOVE_DEVICE process). at this moment you need call IoDetachDevice and IoDeleteDevice - 只有在此之后,您的驱动程序才能被卸载,如果没有更多的 DeviceObjects 或其他对您的驱动程序的引用,PnP 管理器会自动执行此操作。

所以只有一个卸载 WDM 驱动程序的选项 - 完全销毁 device stack - not all stacks is can be stopped at runtime. but some can - by call CM_Request_Device_Eject - 准备本地设备实例以便安全移除(如果设备是可移动的)。如果设备可以物理弹出,那就可以了。