0

I have problems when querying PartitionCount using DeviceIoControl.

#include <tchar.h>
#include <iostream>
#include <windows.h>

int wmain(int argc, wchar_t *argv[])
{
    HANDLE hDevice = CreateFileA("\\\\.\\PhysicalDrive0", 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
    if (hDevice == INVALID_HANDLE_VALUE) {
        return 0;
    }
    else {
            DWORD dwInfoSize = sizeof(DRIVE_LAYOUT_INFORMATION_EX)+sizeof(PARTITION_INFORMATION_EX)* 3;
            DRIVE_LAYOUT_INFORMATION_EX* tpDriveLayoutInformationEx = (DRIVE_LAYOUT_INFORMATION_EX*)malloc(dwInfoSize);
            if (NULL == tpDriveLayoutInformationEx) {
                CloseHandle(hDevice);
                return(-1);
            }
            DWORD dwResult = 0;
            BOOL bResult = DeviceIoControl(hDevice,                            // handle to device
                IOCTL_DISK_GET_DRIVE_LAYOUT_EX,          // dwIoControlCode
                NULL,                                        // lpInBuffer
                0,                                           // nInBufferSize
                tpDriveLayoutInformationEx,          // output buffer
                dwInfoSize,                           // size of output buffer
                &dwResult,                          // number of bytes returned
                NULL                          // OVERLAPPED structure
                );
            if (0 == bResult) {
                printf("DeviceIoControl IOCTL_DISK_GET_DRIVE_LAYOUT_EX FAILED", "");
                CloseHandle(hDevice);
                return 0;
            }
            printf("PartitionCount: %s", tpDriveLayoutInformationEx->PartitionCount);
}
    system("pause");
    return 0;
}

Why DeviceIoControl IOCTL_DISK_GET_DRIVE_LAYOUT_EX failed ?. Please help me. Thanks.

Orion8
  • 41
  • 9
  • 2
    If the operation fails or is pending, the return value is zero. To get extended error information, call GetLastError. <-- What does GetLastError return? – kvr Jul 30 '20 at 02:15
  • I can run the code normally, but change `%s` to `%d`, because `PartitionCount` is a `DWORD`. Hope you can use `GetLastError` to check the error code, so that we can provide you with more help. – Zeus Jul 30 '20 at 06:58
  • GetLastError() = 122. – Orion8 Jul 30 '20 at 11:23

1 Answers1

0

You can refer to How to call DeviceIoControl to retrieve the amount of memory it needs?

And according to VOLUME_DISK_EXTENTS:

An extent is a contiguous run of sectors on one disk. When the number of extents returned is greater than one (1), the error code ERROR_MORE_DATA is returned. You should call DeviceIoControl again, allocating enough buffer space based on the value of NumberOfDiskExtents after the first DeviceIoControl call.

If you pass an output buffer, that is smaller than sizeof(VOLUME_DISK_EXTENTS) is also documented at IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS control code:

If the output buffer is less than sizeof(VOLUME_DISK_EXTENTS), the call fails, GetLastError returns ERROR_INSUFFICIENT_BUFFER, and lpBytesReturned is 0 (zero).

Zeus
  • 3,703
  • 3
  • 7
  • 20