none
DeviceIoControl() function is working differently in Windows 7 and Windows 8 RRS feed

  • Question

  • I am trying to get the Hard Disk Serial using DeviceIoControl function. And calling it with same parameters it gives different value in output buffer in case of win7 and win8.

    I am caliing it as

      if ( DeviceIoControl (hPhysicalDriveIOCTL, IOCTL_STORAGE_QUERY_PROPERTY,
                        & query,
                        sizeof (query),
                        & buffer,
                        sizeof (buffer),
                        & cbBytesReturned, NULL) )
    

    where hPhysicalDriveIOCTL = CreateFile (TEXT(deviceName), 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);

    I am using following code:

    int FetchHDDserial (char *realDiskID, int diskid_len)
    {
       STORAGE_PROPERTY_QUERY query;
       DWORD cbBytesReturned = 0;
       char buffer [10000];
       HANDLE hPhysicalDriveIOCTL = 0;
       STORAGE_DEVICE_DESCRIPTOR * descrip;
       char serialNumber [128];
       char deviceName[256];
       
       sprintf (deviceName, "\\\\.\\PhysicalDrive0");
       hPhysicalDriveIOCTL = CreateFile (TEXT(deviceName), 0,
                      FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
                      OPEN_EXISTING, 0, NULL);
       if (hPhysicalDriveIOCTL == INVALID_HANDLE_VALUE)
       {
          printf("INVALID_HANDLE_VALUE");
       }
       else
       {
          memset ((void *) & query, 0, sizeof (query));
          query.PropertyId = StorageDeviceProperty;
          query.QueryType = PropertyStandardQuery;

          memset (buffer, 0, sizeof (buffer));

          if ( DeviceIoControl (hPhysicalDriveIOCTL, IOCTL_STORAGE_QUERY_PROPERTY,
                            & query,
                            sizeof (query),
                            & buffer,
                            sizeof (buffer),
                            & cbBytesReturned, NULL) )
          {
              descrip = (STORAGE_DEVICE_DESCRIPTOR *) & buffer;

              flipAndCodeBytes (buffer,
                            descrip -> SerialNumberOffset,
                            1, serialNumber );
              strcpy (realDiskID, serialNumber);
              retCode = 0;
          }
          else
          {
    #if 0
              DWORD err = GetLastError ();
              printf("\nDeviceIOControl IOCTL_STORAGE_QUERY_PROPERTY error = %d\n", err);
    #endif
          }

          CloseHandle (hPhysicalDriveIOCTL);
       }

        return 0;
    }

    Want to know the reason for this behavior and Is this behavior is same on all Windows 8 machines ?

    Tuesday, February 4, 2014 8:03 AM

Answers

  • You don't say what is different between the two outputs.  Serial number representation has definitely changed between versions, but I believe the rest should be constant.


    Don Burn Windows Filesystem and Driver Consulting Website: http://www.windrvr.com Blog: http://msmvps.com/blogs/WinDrvr

    Monday, February 17, 2014 6:27 PM