2

I have tried multiple way to fetch correct HDD serial number which we are getting from admin mode by using smart interface and while installing drivers in laptop but none of the them are able to fetch the actual HDD serial number.

Actual serial number : S65VNE0NC41799

Serial number in non-admin mode : 3635_5630_4EC4_1799_0025_3845_0000_0001 .

Given below code used for fetching the HDD serial number.

#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <strsafe.h>
#include <winioctl.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>


int ReadPhysicalDriveInNT_StorageQuery (char *deviceName, char *realDiskID, int diskid_len)
{
   int retCode = 1;
   STORAGE_PROPERTY_QUERY query;
   DWORD cbBytesReturned = 0;
   char buffer [10000] = {'\0'};
   HANDLE hPhysicalDriveIOCTL = 0;
   STORAGE_DEVICE_DESCRIPTOR * descrip = NULL;
   char serialNumber [128] = {'\0'};

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

   if(hPhysicalDriveIOCTL == INVALID_HANDLE_VALUE)
   {
      printf("ReadPhysicalDriveInNT_StorageQuery ERROR:"
             " CreateFile(%s) returned INVALID_HANDLE_VALUE\n"
             , deviceName);
      goto ERR_EXIT;
   }

   memset ((void *) & query, 0, sizeof (query));
   memset (buffer, 0, sizeof (buffer));
   query.QueryType = PropertyStandardQuery;
   query.PropertyId = StorageDeviceProperty; 
 
   if (!DeviceIoControl(hPhysicalDriveIOCTL, IOCTL_STORAGE_QUERY_PROPERTY,
                        & query,
                        sizeof (query),
                        &buffer,
                        sizeof (buffer),
                        & cbBytesReturned, NULL))
   {
           goto ERR_EXIT;
   }
          
   descrip =   (STORAGE_DEVICE_DESCRIPTOR *) & buffer;
   printf("\nserial number : %s\n\n", buffer + descrip->SerialNumberOffset);

ERR_EXIT:

   CloseHandle (hPhysicalDriveIOCTL);

   return retCode;
}

int main()
{

    char dname[256] = {0};
    char diskID[64];
    int i = 0;

    for(i=0; i< 4;i++)
    {
      sprintf(dname, "\\\\.\\PhysicalDrive%d", i);
      dname[strlen(dname)] = '\0';
      ReadPhysicalDriveInNT_StorageQuery(dname, diskID, sizeof(diskID));
    }


return 0;
}

I am getting wrong HDD serial number from this way, so what will be the exact way to fetch HDD serial number in NVME SSD.

  • you paste code for what ? you run this code ? got some error ? what error and where ? – RbMm Jan 17 '22 at 07:51
  • This code is working fine but I am not getting the actual serial number .This code will return 3635_5630_4EC4_1799_0025_3845_0000_0001 as HDD serial number which is wrong, so what will be the correct way to fetch HDD serial number in NVME SSD. – Ashish Durgapal Jan 17 '22 at 07:54
  • you try say that code return different result, based on you run it as admin or not ? – RbMm Jan 17 '22 at 07:56
  • There is different way to fetch the HDD in admin mode (by using smart interface) but the above code works fine in laptops does not have NVME ssd in non-admin mode but it is not working on laptops having NVME SSD . – Ashish Durgapal Jan 17 '22 at 07:58
  • what is "serial number" not definitely – RbMm Jan 17 '22 at 08:04
  • What is the method/code/tool you use that gets you "S65VNE0NC41799"? – Simon Mourier Jan 17 '22 at 08:44
  • Not sure if it's related, but you should remove the `&` before `buffer` in the `DeviceIoControl` call and in the cast just after it. Also, what happens if you remove the `FILE_SHARE_WRITE` flag in `CreateFile` (don't see why you need that)? – Adrian Mole Jan 17 '22 at 09:54
  • @AdrianMole : Made the change but the original issue is still there. – Ashish Durgapal Jan 18 '22 at 04:39
  • @SimonMourier : I have fetched the HDD serial number S65VNE0NC41799 using smart driver interface which only works in admin mode but I have to fetch the same in normal mode. – Ashish Durgapal Jan 18 '22 at 04:39
  • You said that already, what code do you use. – Simon Mourier Jan 18 '22 at 07:17

0 Answers0