1

I am trying to identify if a drive is system reserved drive(PhysicalDrive0 or C-Drive) using DeviceIoControl function. However my code is always returning true for all the drives.

HANDLE hDevice;               // handle to the drive to be examined
BOOL bResult;                 // results flag
DWORD junk;                   // discard results

PARTITION_INFORMATION_MBR *pdg

hDevice = CreateFile(TEXT("\\\\.\\C:"), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ |
        FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);


bResult = DeviceIoControl(
            hDevice,                        // device to be queried
            IOCTL_DISK_GET_PARTITION_INFO_EX,  // operation to perform
            NULL, 0,                        // no input buffer
            pdg, sizeof(*pdg),              // output buffer
            &junk,                          // # bytes returned
            (LPOVERLAPPED) NULL             // synchronous I/O
        );  
  • bResult isalways returning 0, indicating that the function succeeded.
  • Even pdg->PartitionType has junk information and not returning true.
J. Chomel
  • 8,193
  • 15
  • 41
  • 69
  • You might want to take a closer look: *Return value: If the operation completes successfully, the return value is nonzero. If the operation fails or is pending, the return value is zero. To get extended error information, call GetLastError.* – chris Sep 19 '12 at 04:01
  • DeviceIoControl successfully retrieved the partition info and it is (stored in your `PARTITION_INFORMATION_EX` structure) therefore it returned 0 (success). You need to now look at your `PARTITION_INFORMATION_EX` structure and see if the information you requested is there or not. –  Jan 24 '13 at 17:33

1 Answers1

0

bResult isalways returning 0, indicating that the function succeeded.

Plain wrong, the documentation states If the operation completes successfully, the return value is nonzero. Many things could be wrong, at least your parameters are not right and GetLastError would have returned ERROR_INSUFFICIENT_BUFFER:


You're giving DeviceIoControl a uninitialized pointer, but it expects that pdg points to a buffer, in this case, with the size of a pointer to PARTITION_INFORMATION_MBR. Dereferencing wild pointers invokes undefined behavior. Also, according to the documentation DeviceIoControl with OCTL_DISK_GET_PARTITION_INFO awaits a PARTITION_INFORMATION_EX structure so


Change

PARTITION_INFORMATION_MBR *pdg(;)

to

PARTITION_INFORMATION_EX pdg;

So you got a structure with automatic storage, for which you can give DeviceIoControl a temporary pointer to it, with the & operator.

bResult = DeviceIoControl(
        hDevice,                        // device to be queried
        IOCTL_DISK_GET_PARTITION_INFO_EX,  // operation to perform
        NULL, 0,                        // no input buffer
        &pdg, sizeof(pdg),              // output buffer
        &junk,                          // # bytes returned
        (LPOVERLAPPED) NULL             // synchronous I/O
    );  
Superlokkus
  • 4,731
  • 1
  • 25
  • 57