I am trying to find the disk partition style using IOCTL. Earlier, I was using WMI, but in this particular code, I need to provide the physical drive path. What should be given as input, and how can I find or determine which physical drive path to provide? Also, is there a need to use IOCTL, or is WMI a better approach? Which method is recommended?
#include <windows.h>
#include <winioctl.h>
#include <iostream>
void GetDiskPartitionStyle(int& m_DiskPartitionStyle, const std::wstring& physicalDrivePath)
{
// Initialize the partition style as MBR
m_DiskPartitionStyle = HardDiskPartitionType_MBR;
// Open the physical drive
HANDLE hDevice = CreateFile(
physicalDrivePath.c_str(),
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
if (hDevice == INVALID_HANDLE_VALUE) {
std::wcerr << L"Failed to open device. Error: " << GetLastError() << std::endl;
return;
}
// Get partition information
PARTITION_INFORMATION_EX partitionInfo = { 0 };
DWORD bytesReturned;
if (DeviceIoControl(
hDevice,
IOCTL_DISK_GET_PARTITION_INFO_EX,
NULL,
0,
&partitionInfo,
sizeof(partitionInfo),
&bytesReturned,
NULL))
{
// Check the partition style
if (partitionInfo.PartitionStyle == PARTITION_STYLE_GPT) {
m_DiskPartitionStyle = HardDiskPartitionType_GPT;
}
} else {
std::wcerr << L"DeviceIoControl failed. Error: " << GetLastError() << std::endl;
}
// Close the handle
CloseHandle(hDevice);
```}
any better approach?