After partition was created using IOCTL_DISK_SET_D

2019-05-20 09:36发布

问题:

I am trying to create a new partition, format it and assign it a new drive letter via Python ctypes, using IOCTL_SET_DRIVE_LAYOUT. My initial coding attempt was done using this SO page : Python ctypes structure being overwritten when allocating more memory. You can see the definition of class DeviceIoControl there.

I successfully initialize the disk using IOCTL_DISK_CREATE_DISK, and create a new partition having certain size using IOCTL_DISK_SET_DRIVE_LAYOUT, below are the result in Disk Management:

.. but then, how do I create new volume in disk newly created partition?

I've try to use format method of Win32_Volume, but Win32_Volume only return all the n existing drive letters attached to existing partitions, while in my case, that drive letter is not assigned yet.

Any advice?

回答1:

Long story short (it really is a long story!), turn out, I mistakenly choose a wrong PartitionType. Previously I use PARTITION_EXTENDED 0x05, which after I experiment with another value : PARTITION_IFS 0x07, Windows directly asked me to format the partition. And to avoid this, as asked here, we have to wait for several seconds for the new drive letter to be available, and then call kernel32.GetLogicalDriveStringsA to query the new drive (you have to call this previously before creating the partition, and comparing the two, you will get the new drive letter). As the drive letter available, we can easily call Win32_Volume Format method to format the drive.

This is a mix of low level kernel32 call + WMI that bring about a successful solution for this problem. It will be way easier for us if all the functions were available in WMI. But, yeah, that's not we encounter here.