As the title suggest this is a Windows-only example, you may notice that I used it in QT environment from qDebug() helper function, honestly, the only small project that I can think of for this function is one where you would like to change the bootloader, but as I said this is a trivial example.

A few remarks:

  • 1 CreateFile needs to be called from a process that was run as admin in order to work ( require admin in manifest )
  • 2 On MBR Fist sector/boot sector is always writeable
  • 3 L"\\\\.\\PHYSICALDRIVE0" means the first Volume but the user might have multiple volumes and you should make additional checks

CODE:

void QtClAct::detectHddBootType(){

    HANDLE h = CreateFile ( L"\\\\.\\PHYSICALDRIVE0", GENERIC_WRITE |
    GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    if (h == INVALID_HANDLE_VALUE)
    {
      qDebug() <&lt; "Invalid Drive";
    }

    DWORD returned;
    BYTE buffer[1024];
    BOOL b = DeviceIoControl(h,               // handle to device
                             IOCTL_DISK_GET_DRIVE_LAYOUT_EX, // dwIoControlCode
                             NULL,         // lpInBuffer
                             0,            // nInBufferSize
                             buffer,       // output buffer
                             1024,         // size of output buffer
                             &amp;amp;returned,    // number of bytes returned
                             NULL);       // OVERLAPPED structure

    if (!b)
    {
        qDebug() &lt;&lt; "DeviceIoControl returned false "; } DRIVE_LAYOUT_INFORMATION_EX * li = (DRIVE_LAYOUT_INFORMATION_EX *)buffer; } if(li->PartitionStyle == PARTITION_STYLE_MBR){
        // Write your code
    }else if(li->PartitionStyle == PARTITION_STYLE_GPT){
          // Write your code
    }else{ // It must be PARTITION_STYLE_RAW
        // Write your code
    }
}