开发过程中的小记录
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
void ReadFile_Port() { HANDLE hFile=NULL; IO_STATUS_BLOCK ioStatus; NTSTATUS ntStatus; OBJECT_ATTRIBUTES object_attributes; UNICODE_STRING uFileName=RTL_CONSTANT_STRING(L"\\??\\C:\\port.txt"); DbgPrint("ReadLog"); InitializeObjectAttributes( &object_attributes, &uFileName, OBJ_CASE_INSENSITIVE|OBJ_KERNEL_HANDLE, NULL, NULL); ntStatus=ZwCreateFile( &hFile, GENERIC_READ|GENERIC_WRITE, &object_attributes, &ioStatus, NULL, FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ, FILE_OPEN_IF, FILE_NON_DIRECTORY_FILE|FILE_RANDOM_ACCESS|FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0); if (ntStatus==STATUS_SUCCESS) { PVOID buffer; ULONG Length = 10; ULONG dwPort; ANSI_STRING AnsiString1; UNICODE_STRING UnicodeString1; buffer = ExAllocatePool(NonPagedPool, 10); ntStatus=ZwReadFile( hFile, NULL, NULL, NULL, &ioStatus, buffer, Length, NULL, NULL); DbgPrint("%s",buffer); //将buffer转换成ULONG RtlInitAnsiString(&AnsiString1,buffer); RtlAnsiStringToUnicodeString(&UnicodeString1,&AnsiString1,TRUE); RtlUnicodeStringToInteger(&UnicodeString1,10,&dwPort); DbgPrint("%d",dwPort); } else { DbgPrint("Open file error"); } ZwClose(hFile); } |
转载请注明:exchen's blog » ZwReadFile读TXT文件