我正在尝试使用带有comtypes的Python来读取Windows中的分区信息。我发现这个引用很有用:Python script for Windows to control ThinkPad features。从那以及这个MSD页面Disk Management Control Codes,我知道我必须使用Device IO Control。但是,当我尝试使用这些代码调用IOCTL_DISK_GET_DRIVE_GEOMETRY_EX
时:
def doio(self, ctrl, x):
b_in = c_ulong(x)
b_out = c_ulong()
numbytes = c_ulong()
if not self.k32.DeviceIoControl(self.dev, ctrl, byref(b_in), 4, byref(b_out), 4, byref(numbytes), 0):
raise IOError('DeviceIoControl() failed!')
return b_out.value
class TestPartition(unittest.TestCase):
def test_ioctl(self):
c_drive = '\\\\.\C:' #or, \PhysicalDrive0
self.k32 = windll.kernel32
dev = self.k32.CreateFileA(c_drive, FileAccess_Read, FileAccess_ReadWrite, None, FileMode_Open, 0, None)
if dev == -1:
raise Exception('Unable to open drive')
if not dev:
raise Exception('Can not get information from drive')
try:
retval = doio(self.k32.IOCTL_DISK_GET_DRIVE_GEOMETRY_EX, 0)
except Exception as e:
print e.message
我收到了此异常消息function 'IOCTL_DISK_GET_DRIVE_GEOMETRY_EX' not found
如何正确拨打IOCTL_DISK_GET_DRIVE_GEOMETRY_EX
?
答案 0 :(得分:1)
使用这个SO页面Wrapping a C library in Python: C, Cython or ctypes?,似乎ctypes只知道函数,而不是常量。因此,我必须自己定义这些常量。
Woops,常量列在这里很好:ioctls.net!
现在,我可以安静地死去......