我已经编写了一个简单的(测试)脚本来列出选定目录中的文件。不使用FindFirstFile
;
只有本机API。
当我执行脚本并观察时,Win32API监视器会告诉我状态\u SUCCESS。
我的文件信息缓冲区是c_buffer(1024)
,没有使用Unicode缓冲区来查看原始数据。在
所以在打电话NtQueryDirectoryFile
后一切都好。
当我以原始模式将c_buffer
写入控制台以查看目录中的文件时,输出不是结构化的。
我创建了一个FILE\u DIRECTORY_信息结构,但要么它不能在windows7x86上工作,要么我的代码有问题。在
我的问题: 请告诉我在Windows7x86或任何变体上使用哪个文件\目录\信息结构
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)
未优化。在
NtQueryDirectoryFile
应该在循环中调用,直到它返回STATUS_NO_MORE_FILES
。如果返回的状态是STATUS_BUFFER_OVERFLOW
,或者状态是成功的(非负的),状态块Information
为0,则将缓冲区大小加倍,然后重试。对于每个成功的传递,将FILE_DIRECTORY_INFORMATION
记录从缓冲区中复制出来。每个记录的大小必须包括FileName
。当Next
字段为0时,您已到达结尾。在下面的示例将
FILE_DIRECTORY_INFORMATION
子类化为DirEntry
类,该类具有一个listbuf
类方法来列出查询的缓冲区中的记录。它跳过“.”和“.”条目。它在一个ntlistdir
函数中使用这个类,该函数通过NtQueryDirectoryFile
列出给定目录的DirEntry
记录。它支持将打开的文件描述符作为path
参数传递,这与os.listdir
在POSIX系统上的工作方式类似。在c类型定义
}
^{pr2}$DirEntry
和{示例
输出:
相关问题 更多 >
编程相关推荐