游戏技术文章

构造读写IRP

时间:2016-12-29 13:20:10  作者:棋牌资源网  来源:棋牌资源网  查看:7828  评论:0
内容摘要:DDK示例中的代码。NTSTATUSFltReadSectors(  IN PDEVICE_OBJECT DeviceObject,  OUT PVOID Buffer,  IN ULONG Length,  IN LO...
DDK示例中的代码。

NTSTATUS
FltReadSectors(
  IN PDEVICE_OBJECT 
DeviceObject,
  OUT PVOID Buffer,
  IN ULONG Length,
  
IN LONGLONG ByteOffset,
  IN BOOLEAN Wait
  )

{
  
PIRP irp;
  IO_STATUS_BLOCK iosb;
  KEVENT event;
  
NTSTATUS status;

  irp = 
IoBuildAsynchronousFsdRequest(IRP_MJ_READ, DeviceObject,
  Buffer, 
Length, (PLARGE_INTEGER) &ByteOffset, &iosb);
  if (!irp) 
{
  return STATUS_INSUFFICIENT_RESOURCES;
  }

  if 
(Wait) {
  KeInitializeEvent(&event, NotificationEvent, 
FALSE);
  IoSetCompletionRoutine(irp, 
FltReadWriteSectorsCompletion,
  &event, TRUE, TRUE, 
TRUE);

  status = IoCallDriver(DeviceObject, irp);
  if 
(STATUS_PENDING == status) {
  KeWaitForSingleObject(&event, 
Executive, KernelMode, FALSE, NULL);
  status = iosb.Status;
  
}
  } else {
  IoSetCompletionRoutine(irp, 
FltReadWriteSectorsCompletion,
  NULL, TRUE, TRUE, TRUE);
  
irp->UserIosb = NULL;
  status = IoCallDriver(DeviceObject, 
irp);
  }

  return 
status;
}

NTSTATUS
FltWriteSectors(
  IN PDEVICE_OBJECT 
DeviceObject,
  IN PVOID Buffer,
  IN ULONG Length,
  IN 
LONGLONG ByteOffset,
  IN BOOLEAN Wait
  )

{
  
PIRP irp;
  IO_STATUS_BLOCK iosb;
  KEVENT event;
  
NTSTATUS status;

  irp = 
IoBuildAsynchronousFsdRequest(IRP_MJ_WRITE, DeviceObject,
  Buffer, 
Length, (PLARGE_INTEGER) &ByteOffset, &iosb);
  if (!irp) 
{
  return STATUS_INSUFFICIENT_RESOURCES;
  }

  if 
(Wait) {
  KeInitializeEvent(&event, NotificationEvent, 
FALSE);
  IoSetCompletionRoutine(irp, 
FltReadWriteSectorsCompletion,
  &event, TRUE, TRUE, 
TRUE);

  status = IoCallDriver(DeviceObject, irp);
  if 
(STATUS_PENDING == status) {
  KeWaitForSingleObject(&event, 
Executive, KernelMode, FALSE, NULL);
  status = iosb.Status;
  
}
  } else {
  IoSetCompletionRoutine(irp, 
FltReadWriteSectorsCompletion,
  NULL, TRUE, TRUE, TRUE);
  
irp->UserIosb = NULL;
  status = IoCallDriver(DeviceObject, 
irp);
  }

  return 
status;
}

NTSTATUS
FltReadWriteSectorsCompletion(
  IN 
PDEVICE_OBJECT DeviceObject,
  IN PIRP Irp,
  IN PVOID 
Context
  )

{
  PMDL mdl;
   
  
UNREFERENCED_PARAMETER(DeviceObject);

  //
  // Free 
resources
  //

  if (Irp->AssociatedIrp.SystemBuffer 
&& (Irp->Flags & IRP_DEALLOCATE_BUFFER)) {
  
ExFreePool(Irp->AssociatedIrp.SystemBuffer);
  }

  while 
(Irp->MdlAddress) {
  mdl = Irp->MdlAddress;
  
Irp->MdlAddress = mdl->Next;
  MmUnlockPages(mdl);
  
IoFreeMdl(mdl);
  }

  if (Irp->PendingReturned && 
(Context != NULL)) {
  *Irp->UserIosb = Irp->IoStatus;
  
KeSetEvent((PKEVENT) Context, IO_DISK_INCREMENT, FALSE);
  
}

  IoFreeIrp(Irp);

  //
  // Don't touch irp 
any more
  //
  return STATUS_MORE_PROCESSING_REQUIRED;
}
标签:构造读写IRP 

欢迎加入VIP,【VIP售价:只要288元永久VIP会员】畅享商业棋牌游戏程序下载,点击开通!

下载说明


☉本站所有源码和资源均由站长亲自测试-绝对保证都可以架设,运营!
☉如源码和资源有损坏或所有链接均不能下载,请告知管理员,

☉本站软件和源码大部分为站长独资,资源购买和收集,放心下载!

☉唯一站长QQ:1004003180  [人格担保-本站注重诚信!]

☉购买建议E-mail:1004003180@qq.com   源码收购 E-mail:1004003180@qq.com    

☉本站文件解压密码  【文章内都自带解压密码,每个密码不同!】


本站提供的所有源码,均来源站长提供,仅学习交流 浙ICP备09009969号

由此产生不良后果和法律责任与本站无关,如果侵犯了您的版权,请来信告知 1004003180@qq.com 将及时更正和删除! 

Copyright © 2008-2022 棋牌资源网,你身边的棋牌资源下载站    All Rights Reserved