异步读写

测试了下异步读写,记录一下。

1. 若要进行异步读写,创建(或打开)文件时需要指定异步标志位(FILE_FLAG_OVERLAPPED)。

2. 读写函数的返回值依赖底层实现,若底层直接完成操作,则函数返回成功,User Mode代码可立即操作缓冲区。若底层挂起操作,则函数返回false,GetLastError()返回997,若  为其它的错误代码,说明底层处理失败。

3. 处理应用层发送下来的Irp,IoCompleteRequest只是调用完成例程,而不会去拷贝缓冲区、释放Irp等操作,这些操作会等待分发例程执行完毕后再去操作,也就是说,对于应用层IRP,设备栈中的下层驱动处理完IRP后,上层依然可以继续处理IRP,因为此时IRP依然存在。

4. 对于内核层创建的同步IRP,IoCompleteRequest只是调用完成例程,也会去拷贝缓冲区、释放Irp等操作。因此,IoCallDriver返回后,不应再处理IRP,因为此时IRP极可能已被销毁(IoCallDriver返回STATUS_PENDING) 或者已经被销毁(IoCallDriver返回其它代码)。

5. 对于内核层创建的异步IRP,上层驱动应该设置一个完成例程,在完成例程中设置事件、释放IRP,最后返回STATUS_MORE_PROCESSING_REQUIRED,返回这个值得目的是阻止IO Manager继续对这个驱动分配的IRP处理完成操作。代码模型如下:

SOMETYPE SomeFunction(PDEVICE_EXTENSION pdx,
  PDEVICE_OBJECT DeviceObject)
  {
  NTSTATUS status = IoAcquireRemoveLock(&pdx->RemoveLock,
    (PVOID) 42);
  if (!NT_SUCCESS(status))
    return <status>;
  PIRP Irp;
  Irp = IoBuildAsynchronousFsdRequest(IRP_MJ_XXX, DeviceObject,
    ...);
    -or-
  Irp = IoAllocateIrp(DeviceObject->StackSize, FALSE);
  PIO_STACK_LOCATION stack = IoGetNextIrpStackLocation(Irp);
  Stack->MajorFunction = IRP_MJ_XXX;
  <additional initialization)
  KEVENT event;
  KeInitializeEvent(&event, NotificationEvent, FALSE);
  IoSetCompletionRoutine[Ex]([pdx->DeviceObject], Irp,
    (PIO_COMPLETION_ROUTINE) CompletionRoutine,
    &event, TRUE, TRUE, TRUE);
  status = IoCallDriver(DeviceObject, Irp);
  if (status == STATUS_PENDING)
    KeWaitForSingleObject(&event, Executive, KernelMode,
    FALSE, NULL);
  IoReleaseRemoveLock(&pdx->RemoveLock, (PVOID) 42);
  }

NTSTATUS CompletionRoutine(PDEVICE_OBJECT junk, PIRP Irp,
  PKEVENT pev)
  {
  if (Irp->PendingReturned)
    KeSetEvent(pev, EVENT_INCREMENT, FALSE);
  <IRP cleanup -- see above>
  IoFreeIrp(Irp);
  return STATUS_MORE_PROCESSING_REQUIRED;
  }


//拷贝段wdk文档,凑个字数

A driver can call IoBuildDeviceIoControlRequest to set up IRPs for device control requests that it synchronously sends to lower-level drivers.

After calling IoBuildDeviceIoControlRequest to create a request, the driver must call IoCallDriver to send the request to the next-lower driver. If IoCallDriver returns STATUS_PENDING, the driver must wait for the completion of the IRP by calling KeWaitForSingleObject on the given Event. Most drivers do not need to set an IoCompletion routine for the IRP.

IRPs that are created by IoBuildDeviceIoControlRequest must be completed by a driver's call to IoCompleteRequest. A driver that calls IoBuildDeviceIoControlRequest must not call IoFreeIrp, because the I/O manager frees these synchronous IRPs after IoCompleteRequest has been called.

IoBuildDeviceIoControlRequest queues the IRPs that it creates to an IRP queue that is specific to the current thread. If the thread exits, the I/O manager cancels the IRP.



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值