阅读:2012回复:11
弱问 发 iirp 写文件
我用这段代码向 fsd 发irp 来写文件, 可是会出错, 有时是 “延缓写入失败” 有时候是 ntfs! Xxxxxx bug check ,类似的读文件 就没有问题, 请问 有什么问题呢 ?
NTSTATUS IoCompletionRoutine( IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp, IN PVOID Context ) { *Irp->UserIosb = Irp->IoStatus; if (Irp->UserEvent) KeSetEvent(Irp->UserEvent, IO_NO_INCREMENT, 0); if (Irp->MdlAddress) { IoFreeMdl(Irp->MdlAddress); Irp->MdlAddress = NULL; } IoFreeIrp(Irp); return STATUS_MORE_PROCESSING_REQUIRED; } NTSTATUS IrpFileWrite( IN PDEVICE_OBJECT DeviceObject, IN PFILE_OBJECT FileObject, IN PLARGE_INTEGER ByteOffset OPTIONAL, IN ULONG Length, IN PVOID Buffer, OUT PIO_STATUS_BLOCK IoStatusBlock ) { NTSTATUS status; KEVENT event; PIRP irp; PIO_STACK_LOCATION irpSp; IO_STATUS_BLOCK ioStatus; if (ByteOffset == NULL) { if (!(FileObject->Flags & FO_SYNCHRONOUS_IO)) return STATUS_INVALID_PARAMETER; ByteOffset = &FileObject->CurrentByteOffset; } if (FileObject->Vpb == 0 || FileObject->Vpb->RealDevice == NULL) return STATUS_UNSUCCESSFUL; //deviceObject = FileObject->Vpb->DeviceObject; irp = IoAllocateIrp(DeviceObject->StackSize, FALSE); if (irp == NULL) return STATUS_INSUFFICIENT_RESOURCES; irp->MdlAddress = IoAllocateMdl(Buffer, Length, FALSE, TRUE, NULL); if (irp->MdlAddress == NULL) { IoFreeIrp(irp); return STATUS_INSUFFICIENT_RESOURCES;; } MmBuildMdlForNonPagedPool(irp->MdlAddress); irp->Flags = IRP_WRITE_OPERATION; irp->RequestorMode = KernelMode; irp->UserIosb = &ioStatus; irp->UserEvent = &event; irp->Tail.Overlay.Thread = (PETHREAD)KeGetCurrentThread(); irp->Tail.Overlay.OriginalFileObject = FileObject; irpSp = IoGetNextIrpStackLocation(irp); irpSp->MajorFunction = IRP_MJ_WRITE; irpSp->MinorFunction = IRP_MN_NORMAL; irpSp->DeviceObject = DeviceObject ; irpSp->FileObject = FileObject; irpSp->Parameters.Write.Length = Length; irpSp->Parameters.Write.ByteOffset = *ByteOffset; KeInitializeEvent(&event, SynchronizationEvent, FALSE); IoSetCompletionRoutine(irp, IoCompletionRoutine, NULL, TRUE, TRUE, TRUE); status = IoCallDriver(DeviceObject , irp); if (status == STATUS_PENDING) status = KeWaitForSingleObject(&event, Executive, KernelMode, TRUE, NULL); status = ioStatus.Status ; *IoStatusBlock = ioStatus; return status; } |
|
沙发#
发布于:2009-12-18 17:48
irp->Flags = IRP_WRITE_OPERATION;
(IRP_PAGING_IO|IRP_SYNCHRONOUS_PAGING_IO|IRP_NOCACHE) |
|
板凳#
发布于:2009-12-18 17:49
还有FileObject的偏移量你也没处理好……
|
|
地板#
发布于:2009-12-18 18:16
回 1楼(JeTus) 的帖子
小弟初学, 麻烦能稍微解释一下吗, 谢谢 |
|
地下室#
发布于:2009-12-18 19:42
自己在论坛上多搜搜就有了
|
|
5楼#
发布于:2009-12-19 20:26
回 1楼(JeTus) 的帖子
不好意思, 还是没搞懂, 恳求解释, 不胜感谢 |
|
6楼#
发布于:2009-12-20 13:07
论坛上有发irp处理的完整源码的
|
|
|
7楼#
发布于:2009-12-20 15:07
楼主用的好像就是这段代码。
|
|
8楼#
发布于:2009-12-20 22:43
那为什么会出错呢
|
|
9楼#
发布于:2009-12-20 22:43
回 7楼(JeTus) 的帖子
那为什么会出错呢 ? |
|
10楼#
发布于:2009-12-21 11:41
我发现是 process 为 system 时才出错
|
|
11楼#
发布于:2010-05-15 14:09
JeTus 说的很对!!!!
在 IrpWriteFile里面设: Irp->Flags = IRP_PAGING_IO|IRP_SYNCHRONOUS_PAGING_IO|IRP_NOCACHE; 同时注意,要配合写IrpSetInformationFile,否则写了白写,大小还是不变。 |
|