Get the partition type of a specific partition in C++, on a GPT disk

I'm working on a project which requires me to operate at a low level on Windows drives, and am doing so primarily using Windows API calls. But before I can operate on the drive, I need to know the types of partitions present on it.

This is fairly simple on a disk formatted by MBR, because

DeviceIoControl(...,IOCTL_DISK_GET_DRIVE_LAYOUT_EX,...);

returns a structure in format DRIVE_LAYOUT_INFORMATION_EX, which contains an array of PARTITION_INFORMATION_EX. On an MBR disk, the PARTITION_INFORMATION_EX.Mbr.PartitionType element contains a unique identifier for the partition type, e.g. for NTFS it is 0x07, for Extended it is 0x05.

However, this isn't so simple on a GPT disk. I know that I can read the identifier off of the beginning of the partition, but I'd prefer to handle this with API calls, such as DeviceIoControl. When I run DeviceIoControl on a GPT disk, the PARTITION_INFORMATION_EX.Mbr.PartitionType contains completely different values than those which would be normally there.

Note that the GUID is useless to me because that only tells me the purpose of the partition, not what type of partition it is. I'm trying to figure out if the drive is NTFS, FAT, etc.

Upvotes: 1

Views: 3467

Answers (3)

Prakash Sanchela
Prakash Sanchela

Reputation: 11

For GPT partition in your code when you call DeviceIoControl(), this call will return the Partition information in the object of PARTITION_INFORMATION_EX. If you see the PARTITION_INFORMATION_EX structure, there are two separate structure for MBR and GPT disk. So when you get the information in PARTITION_INFORMATION_EX object, you'll have to first confirm that whether the disk type is GPT or MBR, if GPT you can get the specific partition type by comparing it's GUID.

Upvotes: 1

Djof
Djof

Reputation: 603

Instead of going through PARTITION_INFORMATION_EX, I found the best way to find the filesystem of a volume is to call GetVolumeInformation. On Vista+, this seems to be just a wrapper for GetVolumeInformationByHandleW. The later might be the best for you if you already have a volume handle.

Both work well with either MBR or GPT disks. The result is the filesystem name string instead of a type ID, but should be easy to adapt.

Upvotes: 0

Remy Lebeau
Remy Lebeau

Reputation: 595349

Look at Microsoft's PARTITION_INFORMATION_GPT struct for GPT partitions.

Upvotes: 0

Related Questions