基于sfilter修改的磁盘过滤驱动,在应用层和驱动进行通讯时,在调用CreateFile时会返回Errorcode 1,原因是对sfilter本身对sfCreate做了处理,不允许对自身对象进行操作。
sfilter的sfCreate代码如下:
...
if (IS_MY_CONTROL_DEVICE_OBJECT(DeviceObject)) {
//
// Sfilter doesn't allow for any communication through its control
// device object, therefore it fails all requests to open a handle
// to its control device object.
//
// See the FileSpy sample for an example of how to allow creates to
// the filter's control device object and manage communication via
// that handle.
//
Irp->IoStatus.Status = STATUS_INVALID_DEVICE_REQUEST;
Irp->IoStatus.Information = 0;
IoCompleteRequest( Irp, IO_NO_INCREMENT );
return STATUS_INVALID_DEVICE_REQUEST;
}
...
这段代码使得当用CreateFile打开sfilter驱动时直接返回了STATUS_INVALID_DEVICE_REQUEST,所以导致打开失败。
参考FileSpy,修改代码如下:
if (DeviceObject == gSFilterControlDeviceObject)
{
Irp->IoStatus.Status = STATUS_SUCCESS; //修改
Irp->IoStatus.Information = FILE_OPENED; //修改
IoCompleteRequest(Irp, IO_NO_INCREMENT);
return STATUS_SUCCESS;
}
做了上述修改以后发现CreateFile可以打开了,但CloseFile直接蓝屏(BSOD)了。想想也明白了,在CloseFile时需要修改文件状态。
因为原来sfilter根本就不允许打开sfilter驱动本身,所以在其关闭文件处理时就没有对这种情况进行处理,我们需要添加处理:
在SfCleanupClose函数中,添加
if (DeviceObject == gSFilterControlDeviceObject) {
Irp->IoStatus.Status = STATUS_SUCCESS;
Irp->IoStatus.Information = 0;
IoCompleteRequest( Irp, IO_NO_INCREMENT );
return STATUS_SUCCESS;
}
搞定!!!