|
我自己参考别人的代码写了点代码,读U盘扇区没有成功。
过滤驱动过载在注册表HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{36FC9E60-C465-11CF-8056-444553540000}下,
部分代码如下:高手们给指点一下
#pragma LOCKEDCODE
NTSTATUS DispatchForSCSI(IN PDEVICE_OBJECT fido, IN PIRP Irp)
{
KdPrint((DRIVERNAME " - Enter DispatchForSCSI \n"));
//获得设备扩展
PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION) fido->DeviceExtension;
//获得I/O堆栈
PIO_STACK_LOCATION irpStack = IoGetCurrentIrpStackLocation(Irp);
PDEVICE_OBJECT DeviceObject = pdx->DeviceObject;
NTSTATUS status;
if (_wcsnicmp(DeviceObject->AttachedDevice->DriverObject->DriverName.Buffer,L"\\Driver\\USBSTOR",15)==0)
{
//读取U盘扇区,没有标识禁用
status = HandleStartDevice(fido,Irp);
if (!NT_SUCCESS(status))
{
Irp->IoStatus.Status = STATUS_ACCESS_DENIED;
Irp->IoStatus.Information = 0;
IoCompleteRequest( Irp, IO_NO_INCREMENT );
return STATUS_ACCESS_DENIED;
}
//获取自旋锁
status = IoAcquireRemoveLock(&pdx->RemoveLock, Irp);
//判断是否成功获取自旋锁
if (!NT_SUCCESS(status))
//结束IRP请求
return CompleteRequest(Irp, status, 0);
//略过当前I/O堆栈
IoSkipCurrentIrpStackLocation(Irp);
//调用底层驱动程序
status = IoCallDriver(pdx->LowerDeviceObject, Irp);
//释放自旋锁
IoReleaseRemoveLock(&pdx->RemoveLock, Irp);
return status;
}
}
NTSTATUS
HandleStartDevice(
IN PDEVICE_OBJECT DeviceObject,
IN PIRP Irp
)
{
NTSTATUS ntStatus;
CHAR buffer[512] = {0};
ULONG Length = 512;
// Read the device descriptor
//ntStatus = ReadandSelectDescriptors(DeviceObject);
ntStatus = ReadSector(DeviceObject,&buffer,Length);
if(!NT_SUCCESS(ntStatus))
{
KdPrint(("ReadandSelectDescriptors failed\n"));
return ntStatus;
}
return ntStatus;
}
NTSTATUS ReadSector(PDEVICE_OBJECT DeviceObject, PVOID Buffer, ULONG Length)
{
KEVENT event;
NTSTATUS ntStatus = STATUS_SUCCESS;
PIRP Irp;
LARGE_INTEGER lioffset = {0};
IO_STATUS_BLOCK iostatus = {0};
PDEVICE_EXTENSION pdx;
pdx = (PDEVICE_EXTENSION)DeviceObject->DeviceExtension;
lioffset.QuadPart = 512;
//初始化事件
KeInitializeEvent(&event, NotificationEvent, FALSE);
Irp = IoBuildSynchronousFsdRequest(IRP_MJ_READ,
pdx->LowerDeviceObject,
Buffer,
Length,
&lioffset,
&event,
&iostatus);
if(0 == Irp)
{
KdPrint(("IoBuildSynchronousFsdRequest fail! \n"));
return STATUS_INSUFFICIENT_RESOURCES;
}
//发送IRP
ntStatus = IoCallDriver(pdx->LowerDeviceObject, Irp);
//等待事件
if(STATUS_PENDING == ntStatus)
{
KeWaitForSingleObject(&event, Executive, KernelMode, FALSE, 0);
ntStatus = iostatus.Status;
}
if(iostatus.Information == 0)
{
KdPrint(("iostatus.Information == 0 \n"));
goto _end;
}
KdPrint(("iostatus.Information == 0x%x \n", iostatus.Information));
_end:
return ntStatus;
} |
|