阅读:1668回复:3
十万火急!
在驱动程序中是否可以使用:fopen()函数?谢了
[quartz 编辑于 2001-08-17 16:35] |
|
|
沙发#
发布于:2001-08-17 13:20
zwopenfile,zwcreatefile
just see ddk document |
|
|
板凳#
发布于:2001-08-17 15:23
我这有一个函数,用来将指定位置的文件全部读入内存,你看着用吧
/******************************************************************* * * 名称:ReadFile * 功能:将指定的文件读入内存缓冲区中 * 参数: * AnsiName: 文件名 * dwpFileSize: 文件的大小 * 返回:成功为缓冲区头指针,失败为NULL * 其它:调用完本函数缓冲区使用完毕应注意删除 *******************************************************************/ PUCHAR ReadFileToBuffer( IN WCHAR *AnsiName, OUT PULONG dwpFileSize ) { HANDLE hFile=NULL; NTSTATUS ntStatus = STATUS_SUCCESS; OBJECT_ATTRIBUTES Attributes; IO_STATUS_BLOCK Status_Block; UNICODE_STRING FileName; PUCHAR pBuffer=NULL; FILE_STANDARD_INFORMATION FileInfomation; KIRQL Irql; TRACE("Enter OpenFile\r\n"); Irql=KeGetCurrentIrql( ); if(Irql>PASSIVE_LEVEL ) { TRACE1("The Irql is high than passive_level: %d",Irql); } //首先打开文件 ASSERT(Irql==PASSIVE_LEVEL); TRACE("Init the UnicodeString..."); RtlInitUnicodeString(&FileName,AnsiName); TRACE("Success\r\nInit the Attributes..."); InitializeObjectAttributes ( &Attributes, &FileName, OBJ_CASE_INSENSITIVE, NULL, NULL ); TRACE("Success\r\n"); TRACE("Open the file..."); ntStatus = ZwCreateFile( &hFile, //OUT PHANDLE SYNCHRONIZE | GENERIC_READ, //IN ACCESS_MASK &Attributes, //IN POBJECT_ATTRIBUTES &Status_Block, //OUT PIO_STATUS_BLOCK NULL, //IN PLARGE_INTEGER AllocationSize FILE_ATTRIBUTE_NORMAL, //IN ULONG FileAttributes FILE_SHARE_READ, //IN ULONG ShareAccess FILE_OPEN , // IN ULONG CreateDisposition, FILE_SYNCHRONOUS_IO_NONALERT,//IN ULONG CreateOptions NULL, //IN PVOID EaBuffer 0 // IN ULONG EaLength ); if ( !NT_SUCCESS( ntStatus ) ) { TRACE1("failed ,hFile=%d\r\n",hFile); return NULL; } TRACE("Success\r\n"); if (!hFile) return NULL; //查询文件的大小 ntStatus=ZwQueryInformationFile( hFile, &Status_Block, &FileInfomation, sizeof(FILE_STANDARD_INFORMATION ), FileStandardInformation ); if (!NT_SUCCESS(ntStatus)) { TRACE("The file infomation is error\r\n"); ZwClose(hFile); return NULL; } *dwpFileSize=(ULONG)FileInfomation.EndOfFile.QuadPart ; if(*dwpFileSize==0) { TRACE("The file is Empty\r\n"); ZwClose(hFile); return NULL; } //分配缓冲区 pBuffer= (PUCHAR)ExAllocatePool(NonPagedPool, *dwpFileSize); if(pBuffer==NULL) { TRACE("Has no enough memory to allocate pBuffer\r\n"); ZwClose(hFile); return NULL; } //将文件读入缓冲区 ntStatus=ZwReadFile(hFile, NULL, NULL, NULL, &Status_Block, pBuffer, *dwpFileSize, NULL, NULL); if(!NT_SUCCESS(ntStatus)||Status_Block.Information==0) { TRACE("Read file failed\r\n"); ZwClose(hFile); ExFreePool(pBuffer); return NULL; } else { TRACE1("%d bytes has been read from file\r\n",Status_Block.Information); ZwClose(hFile); return pBuffer; } }//End of ReadFileToBuffer |
|
地板#
发布于:2001-08-18 12:03
一个同事教我使用过
NdisOpenFile NdisMapFile , , , NdisUnmapFile NdisCloseFile 但只能打开系统驱动目录下的文件 |
|