阅读:1564回复:3
为什么ReadFile总是读不完数据,如何改变
下面程序:
当Ring3的应用程序调用ReadFile时,我在驱动里调用ZwReadFile把 C:\\abc.txt读出给应用程序程序,但发现,IRP_MJ_READ的 irpSp->Parameters.Read.ByteOffset总是为0,导致用程序的ReadFile总有读不完数据,并且读出的数据全是相同的. 请高手请教: 我在应用程序调用ReadFile时,在驱动里把c:\\abc.txt的文件读出给应程序,我不打算自记文件针,如何实现? --------------------- #include \"ntddk.h\" NTSTATUS AAUnload(IN DRIVER_OBJECT *DriverObject) { DEVICE_OBJECT *DeviceObject,*OldDeviceObject; UNICODE_STRING usDosString; DbgPrint(\"Unload\\n\"); RtlInitUnicodeString(&usDosString,\"\\\\DosDevices\\\\APP\"); IoDeleteSymbolicLink(&usDosString); DeviceObject=DriverObject->DeviceObject; while(DeviceObject!=NULL) { OldDeviceObject=DeviceObject; DeviceObject=DeviceObject->NextDevice; IoDeleteDevice(OldDeviceObject); } return STATUS_SUCCESS; } NTSTATUS AAMajorFunction(IN DEVICE_OBJECT *DeviceObject,IN IRP *Irp) { IoCompleteRequest( Irp, IO_NO_INCREMENT ); return STATUS_SUCCESS; } NTSTATUS AARead(IN DEVICE_OBJECT *DeviceObject,IN IRP *Irp) { NTSTATUS status; PIO_STACK_LOCATION irpSp; HANDLE hFile; OBJECT_ATTRIBUTES ObjectAttributes; UNICODE_STRING usFileName; irpSp = IoGetCurrentIrpStackLocation( Irp ); RtlInitUnicodeString(&usFileName,L\"\\\\DosDevices\\\\C:\\\\abc.txt\"); InitializeObjectAttributes( &ObjectAttributes, &usFileName, 0, NULL, NULL); status=ZwOpenFile( &hFile, FILE_READ_DATA|SYNCHRONIZE, &ObjectAttributes, &Irp->IoStatus, FILE_SHARE_READ, 0); DbgPrint(\"ZwOpenFile. status=%x\\n\",status); DbgPrint(\"Irp->AssociatedIrp.SystemBuffer=0x%08X\\n\",Irp->AssociatedIrp.SystemBuffer); DbgPrint(\"Parameters.Read.Length=%08d\\n\",irpSp->Parameters.Read.Length); DbgPrint(\"Parameters.Read.ByteOffset=%08d\\n\",irpSp->Parameters.Read.ByteOffset.HighPart,irpSp->Parameters.Read.ByteOffset.QuadPart); if(STATUS_SUCCESS==status) { status=ZwReadFile( hFile, NULL, NULL, NULL, &Irp->IoStatus, Irp->AssociatedIrp.SystemBuffer, irpSp->Parameters.Read.Length, &irpSp->Parameters.Read.ByteOffset, NULL); DbgPrint(\"ZwReadFile status=%x read=%d\\n\",status,Irp->IoStatus.Information); } ZwClose(hFile); IoCompleteRequest( Irp, IO_NO_INCREMENT ); return Irp->IoStatus.Information; } NTSTATUS DriverEntry(IN DRIVER_OBJECT *DriverObject ,IN UNICODE_STRING *RegPath) { DEVICE_OBJECT *DeviceObject; UNICODE_STRING usNtName,usDosName; NTSTATUS status; DbgPrint(\" Entry.\\n\"); DriverObject->MajorFunction[IRP_MJ_CREATE] = AAMajorFunction; DriverObject->MajorFunction[IRP_MJ_CLOSE] = AAMajorFunction; DriverObject->MajorFunction[IRP_MJ_READ]=AARead; DriverObject->DriverUnload=AAUnload; RtlInitUnicodeString(&usNtName,\"\\\\Device\\\\DDTest\"); if(STATUS_SUCCESS==IoCreateDevice(DriverObject,0,&usNtName,FILE_DEVICE_UNKNOWN,FILE_REMOVABLE_MEDIA,FALSE,&DeviceObject)) { RtlInitUnicodeString(&usDosName,L\"\\\\DosDevices\\\\APP\"); status=IoCreateSymbolicLink(&usDosName,&usNtName); if(STATUS_SUCCESS==status) { DeviceObject->Flags |=~DO_DEVICE_INITIALIZING; DeviceObject->Flags |= DO_BUFFERED_IO; } else { DbgPrint(\"SymbolicLink err\\n\"); IoDeleteDevice(DeviceObject); } } else { DbgPrint(\"Create Device Error\\n\"); } return STATUS_SUCCESS; } //App 为什么ReadFile总是读不完 int main(int argc,char *argv[]) { HANDLE hFile; DWORD read; char buf[2000]; hFile=CreateFile(\"\\\\\\\\.\\\\APP\",GENERIC_READ,FILE_SHARE_READ, NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL); if(hFile==INVALID_HANDLE_VALUE) { PrintErr(); } else { ZeroMemory(&dg,sizeof(dg)); do{ ZeroMemory(buf,sizeof(buf)); read=0; ReadFile(hFile,buf,512,&read,NULL); printf(\"read %d \\n[%s]\\n\",read,buf); }while(read!=0); } CloseHandle(hFile); return 0; } |
|
|
沙发#
发布于:2003-06-23 13:48
IoCompleteRequest( Irp, IO_NO_INCREMENT );
return Irp->IoStatus.Information; 有问题。 return status; |
|
板凳#
发布于:2003-06-23 13:28
在你的驱动程序中,用ZwReadFile读了多少,在你的 IRP_MJ_READ 例程中 Irp->IoStatus.Information 要填多少。
再试试看!你的代码我没有仔细看,这是凭经验判断的,希望对你有用。 |
|
|
地板#
发布于:2003-06-23 13:28
在你的驱动程序中,用ZwReadFile读了多少,在你的 IRP_MJ_READ 中 Irp->IoStatus.Information 要填多少。
再试试看!你的代码我没有仔细看,这是凭经验判断的,希望对你有用。 |
|
|