Question

I am trying to read partition information in a Windows, using Python with comtypes. I found this reference to be useful: Python script for Windows to control ThinkPad features. From that, and this MSD page: Disk Management Control Codes, I know that I have to use Device IO Control. But, when I try to call IOCTL_DISK_GET_DRIVE_GEOMETRY_EX using these code:

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

I got this exception message, function 'IOCTL_DISK_GET_DRIVE_GEOMETRY_EX' not found

How to properly call IOCTL_DISK_GET_DRIVE_GEOMETRY_EX ?

Was it helpful?

Solution

Using this SO page Wrapping a C library in Python: C, Cython or ctypes?, it seems like ctypes only knew the functions, not the constants. Therefore, I'll have to define those constants myself.

Woops, the constants were listed nicely here:ioctls.net!

Now, I can die peacefully...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top