阅读:1409回复:0
请高手指点下面代码的问题下列程序,是读取指定扇区的数据,然后将数据写入文件,但是运行中总是有问题。 具体描述问题: 在第一个IRP到来,运行到FltReadSectors函数,单步进入该函数,发现在设置完IoComplete函数后,整个程序直接跳转到我所设置的断点位置(红色位置): 另外就是,ZwWriteFile函数有的时候会出错,错误信息是, 文件锁定冲突 if (ByteIndex >= g_BackupPartition.PartitionStart.QuadPart && ByteIndex <= g_BackupPartition.PartitionEnd.QuadPart) { OffsetBeginSector = (ByteIndex - g_BackupPartition.PartitionStart.QuadPart)/512; //断点 WriteToFile.SectorNumber = OffsetBeginSector; for (i = 0; i < Length; i++) { if (!(g_SectorBucket[OffsetBeginSector+i])) { WriteToFile.SectorNumber = OffsetBeginSector + i; TempByteIndex = ByteIndex + i*512; RtlZeroMemory(WriteToFile.SectorContent,512); if (NT_SUCCESS(FltReadSectors(DevExt->TargetDeviceObject,WriteToFile.SectorContent,512, TempByteIndex,1))) { status = ZwWriteFile( g_FileHandle,NULL,NULL,NULL, &IoStatus, &WriteToFile,WriteLength,&g_WriteOffset, NULL); if(!NT_SUCCESS(status)) return; KdPrint(("ZwWriteFile success!\n")); g_WriteOffset.QuadPart += WriteLength; } g_SectorBucket[OffsetBeginSector+i] = 1; } } } 下面是FltReadSectors函数 NTSTATUS FltReadSectors( IN PDEVICE_OBJECT DeviceObject, OUT PVOID Buffer, IN ULONG Length, IN LONGLONG ByteOffset, IN BOOLEAN Wait ) /*++ Routine Description: This routine tries to read the data from the disk. Arguments: DeviceObject - the lower device in the stack Buffer - the buffer which hold the data Length - the data length want to read ByteOffset - the data offset on the disk Return Value: NT Status is returned. --*/ { 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; } |
|