阅读:1313回复:3
斑竹大虾,各位高手,大虾,高高手们,来看看小弟的程序阿~~!救命阿!!!
晕,这个问题已经弄了几天了,还是不知道哪个地方的问题。
就是一个在workitem里面读取文件数据的问题,写倒可以写数据到文件,就是读出来是乱码,高手们help~~~! 小弟能给多少分,就给多少了~~!! 程序如下: #include "myadd.h" #include "MyAddFunc.h" #ifdef __cplusplus extern "C" { #endif NT::HANDLE m_ghFileHandle = NULL; PSTR m_gFileName = "\\\\??\\\\C:\\\\passthru.txt"; ULONG m_TotalProcessBytes = 0; #ifdef __cplusplus } #endif #ifdef __MY_ADD_H__ #ifdef InitializeObjectAttributes #undef InitializeObjectAttributes #endif #define InitializeObjectAttributes( p, n, a, r, s ) { \ (p)->Length = sizeof( NT::OBJECT_ATTRIBUTES ); \ (p)->RootDirectory = r; \ (p)->Attributes = a; \ (p)->ObjectName = n; \ (p)->SecurityDescriptor = s; \ (p)->SecurityQualityOfService = NULL; \ } #endif ////创建文件的workitem VOID MyDriverCreateFileWorkItem( PVOID Context) { HANDLE FileHandle; NTSTATUS ntStatus; NT::OBJECT_ATTRIBUTES ObjectAttributes; NT::IO_STATUS_BLOCK IoStatusBlock; PCWSTR FileName; NT::PUNICODE_STRING pUniFileName; NT::LARGE_INTEGER ByteOffset; NT::PFILE_WORK_ITEM workitem = (NT::PFILE_WORK_ITEM) Context; FileHandle = workitem->FileHandle; pUniFileName = workitem->pUFileName; InitializeObjectAttributes(&ObjectAttributes,pUniFileName, OBJ_CASE_INSENSITIVE,NULL,NULL); ntStatus=NT::ZwCreateFile( &FileHandle, SYNCHRONIZE | GENERIC_READ, &ObjectAttributes, &IoStatusBlock, NULL, FILE_ATTRIBUTE_NORMAL, 0, FILE_OPEN, FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0 ); if(NT_SUCCESS(ntStatus) && FileHandle != NULL) { } NT::ExFreePool(workitem); return; } ////创建文件的函数 VOID MyDriverCreateFile( IN PVOID Context, IN OUT HANDLE *FileHandle, IN PSTR FileName) { NTSTATUS ntStatus; NT::OBJECT_ATTRIBUTES ObjectAttributes; NT::POBJECT_ATTRIBUTES pObjectAttributes; NT::IO_STATUS_BLOCK IoStatusBlock; NT::UNICODE_STRING UniFileName; NT::ANSI_STRING m_AnsiFileName; NT::PFILE_WORK_ITEM workitem; pObjectAttributes = &ObjectAttributes; NT::RtlInitAnsiString(&m_AnsiFileName,FileName); ULONG level = NT::KeGetCurrentIrql(); if(level < DISPATCH_LEVEL) { ntStatus = NT::RtlAnsiStringToUnicodeString(&UniFileName , &m_AnsiFileName ,TRUE); if(!NT_SUCCESS(ntStatus)) return; InitializeObjectAttributes(pObjectAttributes,&UniFileName,OBJ_CASE_INSENSITIVE,NULL,NULL); ntStatus=NT::ZwCreateFile(FileHandle, SYNCHRONIZE | GENERIC_READ, &ObjectAttributes, &IoStatusBlock, NULL, FILE_ATTRIBUTE_NORMAL, 0, FILE_OPEN, FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0); if(NT_SUCCESS(ntStatus) && FileHandle != NULL) { return; } } else { ntStatus = STATUS_PENDING; workitem = (NT::PFILE_WORK_ITEM)NT::ExAllocatePool(NT::NonPagedPool, sizeof(NT::FILE_WORK_ITEM)); if (workitem) { ExInitializeWorkItem(&workitem->WorkItem,MyDriverCreateFileWorkItem,workitem); workitem->FileContext = Context; workitem->FileHandle = *FileHandle; workitem->pUFileName = &UniFileName; NT::ExQueueWorkItem(&workitem->WorkItem, NT::DelayedWorkQueue); } else { ntStatus = STATUS_INSUFFICIENT_RESOURCES; } } return ; } ////读文件的workitem VOID MyDriverReadFileWorkItem( PVOID Context) { HANDLE FileHandle; NTSTATUS ntStatus; NT::IO_STATUS_BLOCK IoStatusBlock; NT::LARGE_INTEGER ByteOffset; PVOID Buffer; ULONG Length; NT::PFILE_WORK_ITEM workitem = (NT::PFILE_WORK_ITEM) Context; FileHandle = workitem->FileHandle; Buffer = workitem->FileContext; Length = workitem->Length; ntStatus=NT::ZwReadFile(FileHandle, 0, 0, 0, &IoStatusBlock, Buffer, Length, NULL, NULL); if(NT_SUCCESS(ntStatus) && FileHandle != NULL) { } NT::ExFreePool(workitem); return; } ////读文件的函数 VOID MyDriverReadFile( IN PVOID Buffer, IN ULONG Length, IN OUT HANDLE FileHandle) { NTSTATUS ntStatus; NT::OBJECT_ATTRIBUTES ObjectAttributes; NT::IO_STATUS_BLOCK IoStatusBlock; NT::UNICODE_STRING UniFileName; NT::PFILE_WORK_ITEM workitem; NT::LARGE_INTEGER ByteOffset; if(NT::KeGetCurrentIrql() < DISPATCH_LEVEL) { ntStatus=NT::ZwReadFile(FileHandle, 0, 0, 0, &IoStatusBlock, Buffer, Length, NULL, NULL); if(NT_SUCCESS(ntStatus) && FileHandle != NULL) { } } else { ntStatus = STATUS_PENDING; workitem = (NT::PFILE_WORK_ITEM)NT::ExAllocatePool(NT::NonPagedPool, sizeof(NT::FILE_WORK_ITEM)); if (workitem) { ExInitializeWorkItem(&workitem->WorkItem, MyDriverReadFileWorkItem, workitem); workitem->FileContext = Buffer; workitem->FileHandle = FileHandle; workitem->Length = Length; NT::ExQueueWorkItem(&workitem->WorkItem, NT::DelayedWorkQueue); } else { ntStatus = STATUS_INSUFFICIENT_RESOURCES; } } return; } ////关闭文件的workitem VOID MyDriverCloseFileWorkItem( PVOID Context) { HANDLE FileHandle; NTSTATUS ntStatus; NT::IO_STATUS_BLOCK IoStatusBlock; NT::LARGE_INTEGER ByteOffset; PVOID Buffer; ULONG Length; NT::PFILE_WORK_ITEM workitem = (NT::PFILE_WORK_ITEM) Context; FileHandle = workitem->FileHandle; ntStatus=NT::ZwClose(FileHandle); if(NT_SUCCESS(ntStatus)) { } NT::ExFreePool(workitem); return; } ////关闭文件的函数 VOID MyDriverCloseFile( IN OUT HANDLE FileHandle) { NTSTATUS ntStatus; NT::PFILE_WORK_ITEM workitem; ULONG level = NT::KeGetCurrentIrql(); if(level < DISPATCH_LEVEL) { ntStatus = NT::ZwClose(FileHandle); if(NT_SUCCESS(ntStatus)) { return; } } else { ntStatus = STATUS_PENDING; workitem = (NT::PFILE_WORK_ITEM)NT::ExAllocatePool(NT::NonPagedPool, sizeof(NT::FILE_WORK_ITEM)); if (workitem) { ExInitializeWorkItem(&workitem->WorkItem,MyDriverCloseFileWorkItem,workitem); workitem->FileHandle = FileHandle; NT::ExQueueWorkItem(&workitem->WorkItem, NT::DelayedWorkQueue); } else { ntStatus = STATUS_INSUFFICIENT_RESOURCES; } } return ; } ////查询文件的大小 ULONG MyQueryInformationFile( IN HANDLE FileHandle ) { NT::IO_STATUS_BLOCK Status_Block; NT::FILE_STANDARD_INFORMATION FileInfomation; ULONG filesize; NT::ZwQueryInformationFile( FileHandle, &Status_Block, &FileInfomation, sizeof(NT::FILE_STANDARD_INFORMATION), NT::FileStandardInformation ); filesize=(ULONG)FileInfomation.EndOfFile.QuadPart; return filesize; } 根据运行结果显示,文件在DriverEntry里面打开,文件是打开成功的,文件句柄是176。 文件的读操作是在PtReceive下面调用的: NDIS_STATUS PtReceive( IN NDIS_HANDLE ProtocolBindingContext, IN NDIS_HANDLE MacReceiveContext, IN PVOID HeaderBuffer, IN UINT HeaderBufferSize, IN PVOID LookAheadBuffer, IN UINT LookAheadBufferSize, IN UINT PacketSize) { PADAPT pAdapt =(PADAPT)ProtocolBindingContext; PNDIS_PACKET MyPacket, Packet; NDIS_STATUS Status = NDIS_STATUS_SUCCESS; char readbuffer[50]; char * piChar; UINT i; MyDriverReadFile(readbuffer,50,m_ghFileHandle); DBGPRINT("readbuffer:\n"); piChar=readbuffer; DbgPrint("piChar:%u\n",piChar); for(i=0;i<=49;i++) { DbgPrint("%c",*piChar++); } ......后面是PtReceive的其余部分. 从程序看,我是从一个名为c:\\passthru.txt的文件里面读数据,该文件里是一些字符,我随便写了些:ggggggggghhhhhhhhh,这就是这个文本文件的全部内容,但根据程序中的显示,是些乱码,没有读正确。 但我把读的函数改为写的函数却可以正确的写文件(当然ZeCreateFile要相应的略微改一下),小弟百思不得其解,已经困扰 好几天了,看起来就是一个在workitem下读文件的操作,可就是不能实现,各路高手救命阿,小弟能给多少分,就给多少分了,help~!!! 对了,哪位高手要是有workitem下面读文件的函数示例,请贴给小弟看看好吗?谢谢啦~~~!!!!! |
|
沙发#
发布于:2004-08-19 08:31
高手们不要见死不救阿~~!!!
要是有用workitem来读文件数据的函数示例对我也是很有帮助的, 小弟第一次写驱动,很多都不懂,又一时找不到参考资料,高手们,帮帮忙,ok?? 谢谢啦~~! |
|
板凳#
发布于:2004-08-19 11:21
高手们,斑竹,救命啊~!~!~!
小弟初学者,这个问题搞不定阿~~~! 有workitem下读取文件的源码给偶参考一下,ok?? 感激不尽~~~~~~~~~! I NEED HELP!!!! SAY SOMETHING TO HELP ME?? DON'T KEEP QUIET,PLEASE????? |
|
地板#
发布于:2004-08-20 09:27
在这里回复你了,
http://www.driverdevelop.com/forum/html_76012.html?1092964624 你的内存释放位置好像不对,访问释放的内存可能会出现系统崩溃 |
|
|