cwinapidisk-partitioning

Win32: Accessing a partition beyond the end of a volume?


It's possible to open a disk volume as a file by using a function call like

HANDLE hDisk = CreateFile("\\\\.\\G:", GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, 0, OPEN_EXISTING, 0, NULL);

However, that only allows access to the area of the partition occupied by the volume - which may not be all of it.

Is it possible, given the drive letter of the volume, to open a handle to the entire partition? The true size of the partition can be calculated by calling DeviceIoControl(...IOCTL_GET_DRIVE_GEOMETRY...) on the volume handle, but reads/writes beyond the end of the volume do not work.


Solution

  • I found what appears to be a simpler solution for my use case: the FSCTL_ALLOW_EXTENDED_DASD_IO control code removes the restriction on the volume handle, allowing access to sectors on the partition outside the filesystem. So apart from error checking, it's a one-line addition to my code:

    DeviceIoControl(hDisk, FSCTL_ALLOW_EXTENDED_DASD_IO, NULL, 0, NULL, 0, &dwBytesRead, NULL);