阅读:3258回复:4
在WINDOWS 7下写磁盘扇区
我做了一个磁盘过滤驱动,其中需要写磁盘扇区,使用以下代码,在XP中可以正常工作,但是在WINDOWS 7下,代码一直等待;停止在红色部分
NTSTATUS WriteDiskDataEx(PDEVICE_OBJECT dev, LARGE_INTEGER offset, PUCHAR buf, ULONG len) { PDEVICE_EXTENSION devExt = dev->DeviceExtension; PIRP irp; NTSTATUS status = STATUS_UNSUCCESSFUL; IO_STATUS_BLOCK ioStatus; ULONG roundlen; PUCHAR TempBuf; KEVENT event; PIO_STACK_LOCATION irpSp; PMDL mdl; KeInitializeEvent(&event, NotificationEvent, FALSE); irp = IoAllocateIrp(devExt->TargetDeviceObject->StackSize+1, FALSE); if(irp) { mdl = IoAllocateMdl(buf, len, FALSE, FALSE, irp); if(mdl) { MmBuildMdlForNonPagedPool(mdl); irp->MdlAddress = mdl; irpSp = IoGetNextIrpStackLocation(irp); irpSp->MajorFunction = IRP_MJ_WRITE; irpSp->Parameters.Write.ByteOffset = offset; irpSp->Parameters.Write.Length = roundlen; IoSetCompletionRoutine(irp, &WriteDiskComplete, &event, TRUE, TRUE, TRUE); status = IoCallDriver(devExt->TargetDeviceObject, irp); if(status == STATUS_PENDING) { KeWaitForSingleObject(&event, Executive, KernelMode, FALSE, NULL);//等待在此处,可能是没有调用WriteDiskComplete status = irp->IoStatus.Status; } IoFreeMdl(mdl); } IoFreeIrp(irp); } else { DBG_CODE(DbgPrint("write can't allocate a irp\n");); } return status; } NTSTATUS WriteDiskComplete(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp, IN PVOID Context) { PKEVENT event; event = (PKEVENT)Context; KeSetEvent(event, IO_NO_INCREMENT, FALSE); return STATUS_MORE_PROCESSING_REQUIRED; } |
|
沙发#
发布于:2011-01-18 18:26
呵,忘记最重要的一句:请各位大牛帮我看看原因,或者告诉我一下WIN 7下面是不是有什么限制;
附加一句:同样的代码,读是完全正常的; |
|
板凳#
发布于:2011-01-18 21:29
irsp->flags |= SL_FORCE_DIRECT_WRITE
|
|
地板#
发布于:2011-01-19 09:15
谢谢 bannerman;问题成功解决;
BS一下自己,昨天其实也搜到这个文章了http://www.debugman.com/discussion/3016/vista-win7%E5%AF%B9%E7%9B%B4%E6%8E%A5%E7%A3%81%E7%9B%98%E5%86%99%E5%85%A5%E7%9A%84%E9%98%B2%E6%8A%A4/p1 也仔细看过,结果估计是潜意识中急着下班,结果把代码写反了;irsp->flags &= SL_FORCE_DIRECT_WRITE;唉。。。。 再次谢谢bannerman |
|
地下室#
发布于:2011-01-20 09:15
收藏下了,目前还在学习中,后面应该会用到
|
|