I've written a simple (test) script to list files in a selected directory. Not using FindFirstFile
;
only native API.
When I execute the script and watch, Win32API monitor tells me STATUS_SUCCESS.
My File Information buffer is c_buffer(1024)
, not using a Unicode buffer to see the raw data.
So after call NtQueryDirectoryFile
all is ok.
When I write c_buffer
in raw mode to console to see the files in the directory, the output is not structured.
I created a FILE_DIRECTORY_INFORMATION structure but either it does not work Windows 7 X86 or there's a problem in my code.
My Question: Please tell me which FILE_DIRECTORY_INFORMATION structure use on Windows 7 X86 or any variants
from ctypes import *
hFile = windll.kernel32.CreateFileW("C:\\a",0x80000000,0,0,3,0x02000000,0)
class Info(Union):
_fields_ = [('STATUS',c_long),
('Pointer',c_ulong),]
class io_stat(Structure):
_fields_ = [('Stat',Info),
('Information',c_ulong),]
class FILE_OBJECT(Structure):
_fields_ = [('Next',c_ulong),
('FileIndex',c_ulong),
('ctime',c_longlong),
('lat',c_longlong),
('wtime',c_longlong),
('ch',c_longlong),
('Endogfile',c_longlong),
('allo',c_longlong),
('Fileattr',c_ulong),
('Filenalen',c_ulong),
('Filename',c_wchar * 2),]
b = io_stat()
a = c_buffer(1024)
windll.ntdll.NtQueryDirectoryFile(hFile,0,0,0,byref(b),byref(a),sizeof(a), 1,0,None,0)
print(a.raw)
Not optimized.
NtQueryDirectoryFile
should be called in a loop until it returnsSTATUS_NO_MORE_FILES
. If either the returned status isSTATUS_BUFFER_OVERFLOW
or the status is successful (non-negative) with the status blockInformation
as 0, then double the buffer size and try again. For each successful pass, copy theFILE_DIRECTORY_INFORMATION
records out of the buffer. Each record has to be sized to include theFileName
. You've reached the end when theNext
field is 0.The following example subclasses
FILE_DIRECTORY_INFORMATION
as aDirEntry
class that has alistbuf
class method to list the records in a queried buffer. It skips the "." and ".." entries. It uses this class in anntlistdir
function that lists theDirEntry
records for a given directory viaNtQueryDirectoryFile
. It supports passing an open file descriptor as thepath
argument, which is like howos.listdir
works on POSIX systems.ctypes definitions
DirEntry
andntlistdir
Example
Output: