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.