Comment 2 for bug 932321

Revision history for this message
Magnus Karlsson (u-magnds-b) wrote :

I changed my code to use the DISK_GEOMETRY_EX like you suggest and this seems to work fine too.

The is the patch based on this verson:

unsigned long long getNumberOfSectors(HANDLE handle, unsigned long long *sectorsize)
{
 DWORD junk;
 DISK_GEOMETRY_EX diskgeometry;
 BOOL bResult;

 bResult = DeviceIoControl(handle, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX, NULL, 0, &diskgeometry, sizeof(diskgeometry), &junk, NULL);
 if (!bResult)
 {
  char *errormessage=NULL;
  FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, GetLastError(), 0, errormessage, 0, NULL);
  QMessageBox::critical(NULL, "Device Error", QString("An error occurred when attempting to get the device's geometry.\nError %1: %2").arg(GetLastError()).arg(errormessage));
  LocalFree(errormessage);
  return 0;
 }

 if (sectorsize != NULL)
  *sectorsize = (unsigned long long)diskgeometry.Geometry.BytesPerSector;
 return (unsigned long long)diskgeometry.DiskSize.QuadPart / (unsigned long long)diskgeometry.Geometry.BytesPerSector;

}