WDF驱动如何向底层磁盘驱动发送IRP_MJ_READ请求读取原始数据?请给出示例
时间: 2023-12-09 17:03:43 浏览: 153
WDF驱动向底层磁盘驱动发送IRP_MJ_READ请求读取原始数据的示例代码如下:
```cpp
NTSTATUS ReadFromDisk(WDFDEVICE Device, PVOID Buffer, ULONG Length)
{
NTSTATUS status = STATUS_SUCCESS;
WDFREQUEST request = NULL;
PIRP irp = NULL;
KEVENT event;
IO_STATUS_BLOCK ioStatusBlock;
// 初始化事件
KeInitializeEvent(&event, NotificationEvent, FALSE);
// 创建请求对象
status = WdfRequestCreate(WDF_NO_OBJECT_ATTRIBUTES, WdfDeviceGetIoTarget(Device), &request);
if (!NT_SUCCESS(status)) {
return status;
}
// 获取IRP对象
irp = WdfRequestWdmGetIrp(request);
// 设置IRP参数
IoSetCompletionRoutine(irp, ReadCompletionRoutine, &event, TRUE, TRUE, TRUE);
irp->Flags |= IRP_BUFFERED_IO | IRP_DEALLOCATE_BUFFER;
irp->AssociatedIrp.SystemBuffer = Buffer;
irp->MdlAddress = NULL;
irp->IoStatus.Status = STATUS_NOT_SUPPORTED;
irp->IoStatus.Information = 0;
irp->Tail.Overlay.Thread = PsGetCurrentThread();
irp->Tail.Overlay.OriginalFileObject = NULL;
irp->Tail.Overlay.AuxiliaryBuffer = NULL;
irp->Flags &= ~(IRP_PAGING_IO | IRP_NOCACHE | IRP_SYNCHRONOUS_API);
// 设置IO栈位置
PIO_STACK_LOCATION irpStack = IoGetNextIrpStackLocation(irp);
irpStack->MajorFunction = IRP_MJ_READ;
irpStack->Parameters.Read.Length = Length;
irpStack->Parameters.Read.ByteOffset.QuadPart = 0;
// 发送IRP请求
status = WdfRequestSend(request, WdfDeviceGetIoTarget(Device), WDF_NO_SEND_OPTIONS);
if (!NT_SUCCESS(status)) {
WdfRequestComplete(request, status);
return status;
}
// 等待请求完成
KeWaitForSingleObject(&event, Executive, KernelMode, FALSE, NULL);
// 获取IO状态
status = irp->IoStatus.Status;
// 完成请求
WdfRequestComplete(request, status);
return status;
}
```
在以上代码中,我们创建了一个WDF请求对象,并获取了其对应的IRP对象,然后设置了IRP的各项参数,最终发送请求并等待请求完成。一旦请求完成,我们就可以通过IRP的IoStatus成员获取IO操作的状态,然后使用WdfRequestComplete函数完成请求。
阅读全文