阅读:2378回复:9
irp写文件的错误
各位老大,我用http://bbs.driverdevelop.com/read.php?tid-110036-page-1.html中irpWriteFile测试了一下自己发IRP写文件,发现如果写的数据大于512,则报错,如果循环写的话,第一次写512可以成功,第二次就报错,错误代码:STATUS_FILE_LOCK_CONFICT。各位老大帮忙看看是什么问题?
我把代码贴上: 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 IrpWriteFile( IN PFILE_OBJECT FileObject, OUT PIO_STATUS_BLOCK IoStatusBlock, IN PVOID Buffer, IN ULONG Length, IN PLARGE_INTEGER ByteOffset OPTIONAL) { NTSTATUS ntStatus; PIRP Irp; KEVENT kEvent; PIO_STACK_LOCATION IrpSp; if (FileObject->Vpb == NULL || FileObject->Vpb->DeviceObject == NULL) return STATUS_UNSUCCESSFUL; if (ByteOffset == NULL) { if (!(FileObject->Flags & FO_SYNCHRONOUS_IO)) return STATUS_INVALID_PARAMETER; ByteOffset = &FileObject->CurrentByteOffset; } Irp = IoAllocateIrp(FileObject->Vpb->DeviceObject->StackSize, FALSE); if (Irp == NULL) return STATUS_INSUFFICIENT_RESOURCES; if(FileObject->DeviceObject->Flags & DO_BUFFERED_IO) { Irp->AssociatedIrp.SystemBuffer = Buffer; } else { Irp->MdlAddress = IoAllocateMdl(Buffer, Length, 0, 0, 0); if (Irp->MdlAddress == NULL) { IoFreeIrp(Irp); return STATUS_INSUFFICIENT_RESOURCES; } MmBuildMdlForNonPagedPool(Irp->MdlAddress); } KeInitializeEvent(&kEvent, SynchronizationEvent, FALSE); Irp->UserEvent = &kEvent; Irp->UserIosb = IoStatusBlock; Irp->RequestorMode = KernelMode; Irp->Flags = IRP_WRITE_OPERATION; Irp->Tail.Overlay.Thread = PsGetCurrentThread(); Irp->Tail.Overlay.OriginalFileObject = FileObject; IrpSp = IoGetNextIrpStackLocation(Irp); IrpSp->MajorFunction = IRP_MJ_WRITE; IrpSp->MinorFunction = IRP_MN_NORMAL; IrpSp->DeviceObject = FileObject->Vpb->DeviceObject; IrpSp->FileObject = FileObject; IrpSp->Parameters.Write.Length = Length; IrpSp->Parameters.Write.ByteOffset = *ByteOffset; IoSetCompletionRoutine(Irp, IoCompletionRoutine, NULL, TRUE, TRUE, TRUE); ntStatus = IoCallDriver(FileObject->Vpb->DeviceObject, Irp); if (ntStatus == STATUS_PENDING) KeWaitForSingleObject(&kEvent, Executive, KernelMode, TRUE, NULL); return IoStatusBlock->Status; } 请各位老大指点一下,本人太菜,发现不了问题所在呀 |
|
沙发#
发布于:2010-08-04 19:23
我的就没问题, 你看看你的creat是不是有问题
|
|
地板#
发布于:2010-08-04 21:24
写少于512的数据,可以正常生成文件并保存,但就是大于512时有问题了
|
|
地下室#
发布于:2010-08-05 09:09
大家帮忙呀,给点建议
|
|
禁止发言
|
5楼#
发布于:2010-08-05 09:24
用户被禁言,该主题自动屏蔽! |
6楼#
发布于:2010-08-05 16:14
wanghui219,谢谢了,不报错了,能讲讲原因吗?但是文件对象中的当前偏移值CurrentByteOffset不自动修改了,为什么?
|
|
7楼#
发布于:2010-08-06 12:42
继续求助
|
|
禁止发言
|
8楼#
发布于:2010-08-06 13:25
用户被禁言,该主题自动屏蔽! |
9楼#
发布于:2010-08-06 16:39
谢谢wanghui219!
|
|