#define PRINTING_TO_CONSOLE_ALLOWED
#pragma warning(disable: 4121)
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <string.h>
#include <windows.h>
#include <winioctl.h>
#define TITLE "DiskId32"
char HardDriveSerialNumber[1024];
char HardDriveModelNumber[1024];
int PRINT_DEBUG = false;
static void dump_buffer(const char* title,
const unsigned char* buffer,
int len);
void WriteConstantString(char *entry, char *string)
{
}
#pragma pack(1)
#define IDENTIFY_BUFFER_SIZE 512
#define DFP_GET_VERSION 0x00074080
#define DFP_SEND_DRIVE_COMMAND 0x0007c084
#define DFP_RECEIVE_DRIVE_DATA 0x0007c088
#define FILE_DEVICE_SCSI 0x0000001b
#define IOCTL_SCSI_MINIPORT_IDENTIFY ((FILE_DEVICE_SCSI << 16) + 0x0501)
#define IOCTL_SCSI_MINIPORT 0x0004D008
#define SMART_GET_VERSION CTL_CODE(IOCTL_DISK_BASE, 0x0020, METHOD_BUFFERED, FILE_READ_ACCESS)
#define SMART_SEND_DRIVE_COMMAND CTL_CODE(IOCTL_DISK_BASE, 0x0021, METHOD_BUFFERED, FILE_READ_ACCESS | FILE_WRITE_ACCESS)
#define SMART_RCV_DRIVE_DATA CTL_CODE(IOCTL_DISK_BASE, 0x0022, METHOD_BUFFERED, FILE_READ_ACCESS | FILE_WRITE_ACCESS)
BYTE IdOutCmd[sizeof(SENDCMDOUTPARAMS) + IDENTIFY_BUFFER_SIZE - 1];
typedef struct _GETVERSIONOUTPARAMS
{
BYTE bVersion;
BYTE bRevision;
BYTE bReserved;
BYTE bIDEDeviceMap;
DWORD fCapabilities;
DWORD dwReserved[4];
} GETVERSIONOUTPARAMS, *PGETVERSIONOUTPARAMS, *LPGETVERSIONOUTPARAMS;
char *ConvertToString(DWORD diskdata[256],
int firstIndex,
int lastIndex,
char* buf);
void PrintIdeInfo(int drive, DWORD diskdata[256]);
BOOL DoIDENTIFY(HANDLE, PSENDCMDINPARAMS, PSENDCMDOUTPARAMS, BYTE, BYTE,
PDWORD);
#define MAX_IDE_DRIVES 16
#define IDE_ATAPI_IDENTIFY 0xA1
#define IDE_ATA_IDENTIFY 0xEC
int ReadPhysicalDriveInNTWithAdminRights(void)
{
int done = FALSE;
int drive = 0;
for (drive = 0; drive < MAX_IDE_DRIVES; drive++)
{
HANDLE hPhysicalDriveIOCTL = 0;
char driveName[256];
sprintf_s(driveName, "\\\\.\\PhysicalDrive%d", drive);
hPhysicalDriveIOCTL = CreateFileA(driveName,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
OPEN_EXISTING, 0, NULL);
if (hPhysicalDriveIOCTL == INVALID_HANDLE_VALUE)
{
#ifdef PRINTING_TO_CONSOLE_ALLOWED
if (PRINT_DEBUG)
printf("\n%d ReadPhysicalDriveInNTWithAdminRights ERROR"
"\nCreateFile(%s) returned INVALID_HANDLE_VALUE\n",
__LINE__, driveName);
#endif
}
else
{
GETVERSIONOUTPARAMS VersionParams;
DWORD cbBytesReturned = 0;
memset((void*)&VersionParams, 0, sizeof(VersionParams));
if (!DeviceIoControl(hPhysicalDriveIOCTL, DFP_GET_VERSION,
NULL,
0,
&VersionParams,
sizeof(VersionParams),
&cbBytesReturned, NULL))
{
#ifdef PRINTING_TO_CONSOLE_ALLOWED
if (PRINT_DEBUG)
{
DWORD err = GetLastError();
printf("\n%d ReadPhysicalDriveInNTWithAdminRights ERROR"
"\nDeviceIoControl(%d, DFP_GET_VERSION) returned 0, error is %d\n",
__LINE__, (int)hPhysicalDriveIOCTL, (int)err);
}
#endif
}
if (VersionParams.bIDEDeviceMap <= 0)
{
#ifdef PRINTING_TO_CONSOLE_ALLOWED
if (PRINT_DEBUG)
printf("\n%d ReadPhysicalDriveInNTWithAdminRights ERROR"
"\nNo device found at position %d (%d)\n",
__LINE__, (int)drive, (int)VersionParams.bIDEDeviceMap);
#endif
}
else
{
BYTE bIDCmd = 0;
SENDCMDINPARAMS scip;
bIDCmd = (VersionParams.bIDEDeviceMap >> drive & 0x10) ? \
IDE_ATAPI_IDENTIFY : IDE_ATA_IDENTIFY;
memset(&scip, 0, sizeof(scip));
memset(IdOutCmd, 0, sizeof(IdOutCmd));
if (DoIDENTIFY(hPhysicalDriveIOCTL,
&scip,
(PSENDCMDOUTPARAMS)&IdOutCmd,
(BYTE)bIDCmd,
(BYTE)drive,
&cbBytesReturned))
{
DWORD diskdata[256];
int ijk = 0;
USHORT *pIdSector = (USHORT *)
((PSENDCMDOUTPARAMS)IdOutCmd)->bBuffer;
for (ijk = 0; ijk < 256; ijk++)
diskdata[ijk] = pIdSector[ijk];
PrintIdeInfo(drive, diskdata);
done = TRUE;
}
}
CloseHandle(hPhysicalDriveIOCTL);
}
}
return done;
}
#pragma pack(1)
typedef struct _IDENTIFY_DATA {
USHORT GeneralConfiguration;
USHORT NumberOfCylinders;
USHORT Reserved1;
USHORT NumberOfHeads;
USHORT UnformattedBytesPerTrack;
USHORT UnformattedBytesPerSector;
USHORT SectorsPerTrack;
USHORT VendorUnique1[3];
USHORT SerialNumber[10];
USHORT BufferType;
USHORT BufferSectorSize;
USHORT NumberOfEccBytes;
USHORT FirmwareRevision[4];
USHORT ModelNumber[20];
UCHAR MaximumBlockTransfer;
UCHAR VendorUnique2;
USHORT DoubleWordIo;
USHORT Capabilities;
USHORT Reserved2;
UCHAR VendorUnique3;
UCHAR PioCycleTimingMode;
UCHAR VendorUnique4;
UCHAR DmaCycleTimingMode;
USHORT TranslationFieldsValid : 1;
USHORT Reserved3 : 15;
USHORT NumberOfCurrentCylinders;
USHORT NumberOfCurrentHeads;
USHORT CurrentSectorsPerTrack;
ULONG CurrentSectorCapacity;
USHORT CurrentMultiSectorSetting;
ULONG UserAddressableSectors;
USHORT SingleWordDMASupport : 8;
USHORT SingleWordDMAActive : 8;
USHORT MultiWordDMASupport : 8;
USHORT MultiWordDMAActive : 8;
USHORT AdvancedPIOModes : 8;
USHORT Reserved4 : 8;
USHORT MinimumMWXferCycleTime;
USHORT RecommendedMWXferCycleTime;
USHORT MinimumPIOCycleTime;
USHORT MinimumPIOCycleTimeIORDY;
USHORT Reserved5[2];
USHORT ReleaseTimeOverlapped;
USHORT ReleaseTimeServiceCommand;
USHORT MajorRevision;
USHORT MinorRevision;
USHORT Reserved6[50];
USHORT SpecialFunctionsEnabled;
USHORT Reserved7[128];
} IDENTIFY_DATA, *PIDENTIFY_DATA;
#pragma pack()
int ReadPhysicalDriveInNTUsingSmart(void)
{
int done = FALSE;
int drive = 0;
for (drive = 0; drive < MAX_IDE_DRIVES; drive++)
{
HANDLE hPhysicalDriveIOCTL = 0;
char driveName[256];
sprintf_s(driveName, "\\\\.\\PhysicalDrive%d", drive);
hPhysicalDriveIOCTL = CreateFileA(driveName,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0, NULL);
if (hPhysicalDriveIOCTL == INVALID_HANDLE_VALUE)
{
#ifdef PRINTING_TO_CONSOLE_ALLOWED
if (PRINT_DEBUG)
printf("\n%d ReadPhysicalDriveInNTUsingSmart ERROR"
"\nCreateFile(%s) returned INVALID_HANDLE_VALUE\n"
"Error Code %d\n",
__LINE__, driveName, GetLastError());
#endif
}
else
{
GETVERSIONINPARAMS GetVersionParams;
DWORD cbBytesReturned = 0;
memset((void*)& GetVersionParams, 0, sizeof(GetVersionParams));
if (!DeviceIoControl(hPhysicalDriveIOCTL, SMART_GET_VERSION,
NULL,
0,
&GetVersionParams, sizeof(GETVERSIONINPARAMS),
&cbBytesReturned, NULL))
{
#ifdef PRINTING_TO_CONSOLE_ALLOWED
if (PRINT_DEBUG)
{
DWORD err = GetLastError();
printf("\n%d ReadPhysicalDriveInNTUsingSmart ERROR"
"\nDeviceIoControl(%d, SMART_GET_VERSION) returned 0, error is %d\n",
__LINE__, (int)hPhysicalDriveIOCTL, (int)err);
}
#endif
}
else
{
ULONG CommandSize = sizeof(SENDCMDINPARAMS) + IDENTIFY_BUFFER_SIZE;
PSENDCMDINPARAMS Command = (PSENDCMDINPARAMS)malloc(CommandSize);
#define ID_CMD 0xEC
Command->irDriveRegs.bCommandReg = ID_CMD;
DWORD BytesReturned = 0;
if (!DeviceIoControl(hPhysicalDriveIOCTL,
SMART_RCV_DRIVE_DATA, Command, sizeof(SENDCMDINPARAMS),
Command, CommandSize,
&BytesReturned, NULL))
{
}
else
{
DWORD diskdata[256];
USHORT *pIdSector = (USHORT *)
(PIDENTIFY_DATA)((PSENDCMDOUTPARAMS)Command)->bBuffer;
for (int ijk = 0; ijk < 256; ijk++)
diskdata[ijk] = pIdSector[ijk];
PrintIdeInfo(drive, diskdata);
done = TRUE;
}
CloseHandle(hPhysicalDriveIOCTL);
free(Command);
}
}
}
return done;
}
#pragma pack(4)
#define IOCTL_STORAGE_QUERY_PROPERTY CTL_CODE(IOCTL_STORAGE_BASE, 0x0500, METHOD_BUFFERED, FILE_ANY_ACCESS)
#pragma pack(4)
char * flipAndCodeBytes(const char * str,
int pos,
int flip,
char * buf)
{
int i;
int j = 0;
int k = 0;
buf[0] = '\0';
if (pos <= 0)
return buf;
if (!j)
{
char p = 0;
j = 1;
k = 0;
buf[k] = 0;
for (i = pos; j && str[i] != '\0'; ++i)
{
char c = tolower(str[i]);
if (isspace(c))
c = '0';
++p;
buf[k] <<= 4;
if (c >= '0' && c <= '9')
buf[k] |= (unsigned char)(c - '0');
else if (c >= 'a' && c <= 'f')
buf[k] |= (unsigned char)(c - 'a' + 10);
else
{
j = 0;
break;
}
if (p == 2)
{
if (buf[k] != '\0' && !isprint(buf[k]))
{
j = 0;
break;
}
++k;
p = 0;
buf[k] = 0;
}
}
}
if (!j)
{
j = 1;
k = 0;
for (i = pos; j && str[i] != '\0'; ++i)
{
char c = str[i];
if (!isprint(c))
{
j = 0;
break;
}
buf[k++] = c;
}
}
if (!j)
{
k = 0;
}
buf[k] = '\0';
if (flip)
for (j = 0; j < k; j += 2)
{
char t = buf[j];
buf[j] = buf[j + 1];
buf[j + 1] = t;
}
i = j = -1;
for (k = 0; buf[k] != '\0'; ++k)
{
if (!isspace(buf[k]))
{
if (i < 0)
i = k;
j = k;
}
}
if ((i >= 0) && (j >= 0))
{
for (k = i; (k <= j) && (buf[k] != '\0'); ++k)
buf[k - i] = buf[k];
buf[k - i] = '\0';
}
return buf;
}
#define IOCTL_DISK_GET_DRIVE_GEOMETRY_EX CTL_CODE(IOCTL_DISK_BASE, 0x0028, METHOD_BUFFERED, FILE_ANY_ACCESS)
int ReadPhysicalDriveInNTWithZeroRights(void)
{
int done = FALSE;
int drive = 0;
for (drive = 0; drive < MAX_IDE_DRIVES; drive++)
{
HANDLE hPhysicalDriveIOCTL = 0;
char driveName[256];
sprintf_s(driveName, "\\\\.\\PhysicalDrive%d", drive);
hPhysicalDriveIOCTL = CreateFileA(driveName, 0,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
OPEN_EXISTING, 0, NULL);
if (hPhysicalDriveIOCTL == INVALID_HANDLE_VALUE)
{
#ifdef PRINTING_TO_CONSOLE_ALLOWED
if (PRINT_DEBUG)
printf("\n%d ReadPhysicalDriveInNTWithZeroRights ERROR"
"\nCreateFile(%s) returned INVALID_HANDLE_VALUE\n",
__LINE__, driveName);
#endif
}
else
{
STORAGE_PROPERTY_QUERY query;
DWORD cbBytesReturned = 0;
char local_buffer[10000];
memset((void *)& query, 0, sizeof(query));
query.PropertyId = StorageDeviceProperty;
query.QueryType = PropertyStandardQuery;
memset(local_buffer, 0, sizeof(local_buffer));
if (DeviceIoControl(hPhysicalDriveIOCTL, IOCTL_STORAGE_QUERY_PROPERTY,
&query,
sizeof(query),
&local_buffer[0],
sizeof(local_buffer),
&cbBytesReturned, NULL))
{
STORAGE_DEVICE_DESCRIPTOR * descrip = (STORAGE_DEVICE_DESCRIPTOR *)& local_buffer;
char serialNumber[1000];
char modelNumber[1000];
char vendorId[1000];
char productRevision[1000];
#ifdef PRINTING_TO_CONSOLE_ALLOWED
if (PRINT_DEBUG)
{
printf("\n%d STORAGE_DEVICE_DESCRIPTOR contents for drive %d\n"
" Version: %ld\n"
" Size: %ld\n"
" DeviceType: %02x\n"
" DeviceTypeModifier: %02x\n"
" RemovableMedia: %d\n"
" CommandQueueing: %d\n"
" VendorIdOffset: %4ld (0x%02lx)\n"
" ProductIdOffset: %4ld (0x%02lx)\n"
" ProductRevisionOffset: %4ld (0x%02lx)\n"
" SerialNumberOffset: %4ld (0x%02lx)\n"
" BusType: %d\n"
" RawPropertiesLength: %ld\n",
__LINE__, drive,
(unsigned long)descrip->Version,
(unsigned long)descrip->Size,
(int)descrip->DeviceType,
(int)descrip->DeviceTypeModifier,
(int)descrip->RemovableMedia,
(int)descrip->CommandQueueing,
(unsigned long)descrip->VendorIdOffset,
(unsigned long)descrip->VendorIdOffset,
(unsigned long)descrip->ProductIdOffset,
(unsigned long)descrip->ProductIdOffset,
(unsigned long)descrip->ProductRevisionOffset,
(unsigned long)descrip->ProductRevisionOffset,
(unsigned long)descrip->SerialNumberOffset,
(unsigned long)descrip->SerialNumberOffset,
(int)descrip->BusType,
(unsigned long)descrip->RawPropertiesLength);
dump_buffer("Contents of RawDeviceProperties",
(unsigned char*)descrip->RawDeviceProperties,
descrip->RawPropertiesLength);
dump_buffer("Contents of first 256 bytes in buffer",
(unsigned char*)local_buffer, 256);
}
#endif
flipAndCodeBytes(local_buffer,
descrip->VendorIdOffset,
0, vendorId);
flipAndCodeBytes(local_buffer,
descrip->ProductIdOffset,
0, modelNumber);
flipAndCodeBytes(local_buffer,
descrip->ProductRevisionOffset,
0, productRevision);
flipAndCodeBytes(local_buffer,
descrip->SerialNumberOffset,
1, serialNumber);
if (0 == HardDriveSerialNumber[0] &&
(isalnum(serialNumber[0]) || isalnum(serialNumber[19])))
{
strcpy_s(HardDriveSerialNumber, serialNumber);
strcpy_s(HardDriveModelNumber, modelNumber);
done = TRUE;
}
#ifdef PRINTING_TO_CONSOLE_ALLOWED
printf("\n**** STORAGE_DEVICE_DESCRIPTOR for drive %d ****\n"
"Vendor Id = [%s]\n"
"Product Id = [%s]\n"
"Product Revision = [%s]\n"
"Serial Number = [%s]\n",
drive,
vendorId,
modelNumber,
productRevision,
serialNumber);
#endif
memset(local_buffer, 0, sizeof(local_buffer));
if (!DeviceIoControl(hPhysicalDriveIOCTL,
IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
NULL,
0,
&local_buffer[0],
sizeof(local_buffer),
&cbBytesReturned,
NULL))
{
#ifdef PRINTING_TO_CONSOLE_ALLOWED
if (PRINT_DEBUG)
printf("\n%d ReadPhysicalDriveInNTWithZeroRights ERROR"
"|nDeviceIoControl(%s, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX) returned 0",
driveName);
#endif
}
else
{
DISK_GEOMETRY_EX * geom = (DISK_GEOMETRY_EX *)& local_buffer[0];
int fixed = (geom->Geometry.MediaType == FixedMedia);
__int64 size = geom->DiskSize.QuadPart;
#ifdef PRINTING_TO_CONSOLE_ALLOWED
printf("\n**** DISK_GEOMETRY_EX for drive %d ****\n"
"Disk is%s fixed\n"
"DiskSize = %I64d\n",
drive,
fixed ? "" : " NOT",
size);
#endif
}
}
else
{
DWORD err = GetLastError();
#ifdef PRINTING_TO_CONSOLE_ALLOWED
printf("\nDeviceIOControl IOCTL_STORAGE_QUERY_PROPERTY error = %d\n", err);
#endif
}
CloseHandle(hPhysicalDriveIOCTL);
}
}
return done;
}
BOOL DoIDENTIFY(HANDLE hPhysicalDriveIOCTL, PSENDCMDINPARAMS pSCIP,
PSENDCMDOUTPARAMS pSCOP, BYTE bIDCmd, BYTE bDriveNum,
PDWORD lpcbBytesReturned)
{
pSCIP->cBufferSize = IDENTIFY_BUFFER_SIZE;
pSCIP->irDriveRegs.bFeaturesReg = 0;
pSCIP->irDriveRegs.bSectorCountReg = 1;
pSCIP->irDriveRegs.bCylLowReg = 0;
pSCIP->irDriveRegs.bCylHighReg = 0;
pSCIP->irDriveRegs.bDriveHeadReg = 0xA0 | ((bDriveNum & 1) << 4);
pSCIP->irDriveRegs.bCommandReg = bIDCmd;
pSCIP->bDriveNumber = bDriveNum;
pSCIP->cBufferSize = IDENTIFY_BUFFER_SIZE;
return (DeviceIoControl(hPhysicalDriveIOCTL, DFP_RECEIVE_DRIVE_DATA,
(LPVOID)pSCIP,
sizeof(SENDCMDINPARAMS) - 1,
(LPVOID)pSCOP,
sizeof(SENDCMDOUTPARAMS) + IDENTIFY_BUFFER_SIZE - 1,
lpcbBytesReturned, NULL));
}
typedef struct _rt_IdeDInfo_
{
BYTE IDEExists[4];
BYTE DiskExists[8];
WORD DisksRawInfo[8 * 256];
} rt_IdeDInfo, *pt_IdeDInfo;
typedef struct _rt_DiskInfo_
{
BOOL DiskExists;
BOOL ATAdevice;
BOOL RemovableDevice;
WORD TotLogCyl;
WORD TotLogHeads;
WORD TotLogSPT;
char SerialNumber[20];
char FirmwareRevision[8];
char ModelNumber[40];
WORD CurLogCyl;
WORD CurLogHeads;
WORD CurLogSPT;
} rt_DiskInfo;
#define m_cVxDFunctionIdesDInfo 1
int ReadDrivePortsInWin9X(void)
{
int done = FALSE;
unsigned long int i = 0;
HANDLE VxDHandle = 0;
pt_IdeDInfo pOutBufVxD = 0;
DWORD lpBytesReturned = 0;
BOOL status =
SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS);
#ifdef PRINTING_TO_CONSOLE_ALLOWED
if (0 == status)
printf("\nERROR: Could not SetPriorityClass, LastError: %d\n", GetLastError());
#endif
rt_IdeDInfo info;
pOutBufVxD = &info;
ZeroMemory(&info, sizeof(info));
VxDHandle = CreateFileA("\\\\.\\IDE21201.VXD", 0, 0, 0,
0, FILE_FLAG_DELETE_ON_CLOSE, 0);
if (VxDHandle != INVALID_HANDLE_VALUE)
{
DeviceIoControl(VxDHandle, m_cVxDFunctionIdesDInfo,
0, 0, pOutBufVxD, sizeof(pt_IdeDInfo), &lpBytesReturned, 0);
CloseHandle(VxDHandle);
}
else
MessageBoxA(NULL, "ERROR: Could not open IDE21201.VXD file",
TITLE, MB_ICONSTOP);
for (i = 0; i < 8; i++)
{
if ((pOutBufVxD->DiskExists[i]) && (pOutBufVxD->IDEExists[i / 2]))
{
DWORD diskinfo[256];
for (int j = 0; j < 256; j++)
diskinfo[j] = pOutBufVxD->DisksRawInfo[i * 256 + j];
PrintIdeInfo(i, diskinfo);
done = TRUE;
}
}
SetPriorityClass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS);
return done;
}
#define SENDIDLENGTH sizeof (SENDCMDOUTPARAMS) + IDENTIFY_BUFFER_SIZE
typedef struct _SRB_IO_CONTROL
{
ULONG HeaderLength;
UCHAR Signature[8];
ULONG Timeout;
ULONG ControlCode;
ULONG ReturnCode;
ULONG Length;
} SRB_IO_CONTROL, *PSRB_IO_CONTROL;
typedef struct _IDSECTOR
{
USHORT wGenConfig;
USHORT wNumCyls;
USHORT wReserved;
USHORT wNumHeads;
USHORT wBytesPerTrack;
USHORT wBytesPerSector;
USHORT wSectorsPerTrack;
USHORT wVendorUnique[3];
CHAR sSerialNumber[20];
USHORT wBufferType;
USHORT wBufferSize;
USHORT wECCSize;
CHAR sFirmwareRev[8];
CHAR sModelNumber[40];
USHORT wMoreVendorUnique;
USHORT wDoubleWordIO;
USHORT wCapabilities;
USHORT wReserved1;
USHORT wPIOTiming;
USHORT wDMATiming;
USHORT wBS;
USHORT wNumCurrentCyls;
USHORT wNumCurrentHeads;
USHORT wNumCurrentSectorsPerTrack;
ULONG ulCurrentSectorCapacity;
USHORT wMultSectorStuff;
ULONG ulTotalAddressableSectors;
USHORT wSingleWordDMA;
USHORT wMultiWordDMA;
BYTE bReserved[128];
} IDSECTOR, *PIDSECTOR;
int ReadIdeDriveAsScsiDriveInNT(void)
{
int done = FALSE;
int controller = 0;
for (controller = 0; controller < 16; controller++)
{
HANDLE hScsiDriveIOCTL = 0;
char driveName[256];
sprintf_s(driveName, "\\\\.\\Scsi%d:", controller);
hScsiDriveIOCTL = CreateFileA(driveName,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
OPEN_EXISTING, 0, NULL);
if (hScsiDriveIOCTL != INVALID_HANDLE_VALUE)
{
int drive = 0;
for (drive = 0; drive < 2; drive++)
{
char buffer[sizeof(SRB_IO_CONTROL) + SENDIDLENGTH];
SRB_IO_CONTROL *p = (SRB_IO_CONTROL *)buffer;
SENDCMDINPARAMS *pin =
(SENDCMDINPARAMS *)(buffer + sizeof(SRB_IO_CONTROL));
DWORD dummy;
memset(buffer, 0, sizeof(buffer));
p->HeaderLength = sizeof(SRB_IO_CONTROL);
p->Timeout = 10000;
p->Length = SENDIDLENGTH;
p->ControlCode = IOCTL_SCSI_MINIPORT_IDENTIFY;
strncpy_s((char *)p->Signature, 8, "SCSIDISK", 8);
pin->irDriveRegs.bCommandReg = IDE_ATA_IDENTIFY;
pin->bDriveNumber = drive;
if (DeviceIoControl(hScsiDriveIOCTL, IOCTL_SCSI_MINIPORT,
buffer,
sizeof(SRB_IO_CONTROL) +
sizeof(SENDCMDINPARAMS) - 1,
buffer,
sizeof(SRB_IO_CONTROL) + SENDIDLENGTH,
&dummy, NULL))
{
SENDCMDOUTPARAMS *pOut =
(SENDCMDOUTPARAMS *)(buffer + sizeof(SRB_IO_CONTROL));
IDSECTOR *pId = (IDSECTOR *)(pOut->bBuffer);
if (pId->sModelNumber[0])
{
DWORD diskdata[256];
int ijk = 0;
USHORT *pIdSector = (USHORT *)pId;
for (ijk = 0; ijk < 256; ijk++)
diskdata[ijk] = pIdSector[ijk];
PrintIdeInfo(controller * 2 + drive, diskdata);
done = TRUE;
}
}
}
CloseHandle(hScsiDriveIOCTL);
}
}
return done;
}
void PrintIdeInfo(int drive, DWORD diskdata[256])
{
char serialNumber[1024];
char modelNumber[1024];
char revisionNumber[1024];
char bufferSize[32];
__int64 sectors = 0;
__int64 bytes = 0;
ConvertToString(diskdata, 10, 19, serialNumber);
ConvertToString(diskdata, 27, 46, modelNumber);
ConvertToString(diskdata, 23, 26, revisionNumber);
sprintf_s(bufferSize, "%u", diskdata[21] * 512);
if (0 == HardDriveSerialNumber[0] &&
(isalnum(serialNumber[0]) || isalnum(serialNumber[19])))
{
strcpy_s(HardDriveSerialNumber, serialNumber);
strcpy_s(HardDriveModelNumber, modelNumber);
}
#ifdef PRINTING_TO_CONSOLE_ALLOWED
printf("\nDrive %d - ", drive);
switch (drive / 2)
{
case 0: printf("Primary Controller - ");
break;
case 1: printf("Secondary Controller - ");
break;
case 2: printf("Tertiary Controller - ");
break;
case 3: printf("Quaternary Controller - ");
break;
}
switch (drive % 2)
{
case 0: printf("Master drive\n\n");
break;
case 1: printf("Slave drive\n\n");
break;
}
printf("Drive Model Number________________: [%s]\n",
modelNumber);
printf("Drive Serial Number_______________: [%s]\n",
serialNumber);
printf("Drive Controller Revision Number__: [%s]\n",
revisionNumber);
printf("Controller Buffer Size on Drive___: %s bytes\n",
bufferSize);
printf("Drive Type________________________: ");
if (diskdata[0] & 0x0080)
printf("Removable\n");
else if (diskdata[0] & 0x0040)
printf("Fixed\n");
else printf("Unknown\n");
if (diskdata[83] & 0x400)
sectors = diskdata[103] * 65536I64 * 65536I64 * 65536I64 +
diskdata[102] * 65536I64 * 65536I64 +
diskdata[101] * 65536I64 +
diskdata[100];
else
sectors = diskdata[61] * 65536 + diskdata[60];
bytes = sectors * 512;
printf("Drive Size________________________: %I64d bytes\n",
bytes);
#endif
char string1[1000];
sprintf_s(string1, "Drive%dModelNumber", drive);
WriteConstantString(string1, modelNumber);
sprintf_s(string1, "Drive%dSerialNumber", drive);
WriteConstantString(string1, serialNumber);
sprintf_s(string1, "Drive%dControllerRevisionNumber", drive);
WriteConstantString(string1, revisionNumber);
sprintf_s(string1, "Drive%dControllerBufferSize", drive);
WriteConstantString(string1, bufferSize);
sprintf_s(string1, "Drive%dType", drive);
if (diskdata[0] & 0x0080)
WriteConstantString(string1, "Removable");
else if (diskdata[0] & 0x0040)
WriteConstantString(string1, "Fixed");
else
WriteConstantString(string1, "Unknown");
}
char *ConvertToString(DWORD diskdata[256],
int firstIndex,
int lastIndex,
char* buf)
{
int index = 0;
int position = 0;
for (index = firstIndex; index <= lastIndex; index++)
{
buf[position++] = (char)(diskdata[index] / 256);
buf[position++] = (char)(diskdata[index] % 256);
}
buf[position] = '\0';
for (index = position - 1; index > 0 && isspace(buf[index]); index--)
buf[index] = '\0';
return buf;
}
long getHardDriveComputerID()
{
int done = FALSE;
__int64 id = 0;
OSVERSIONINFO version;
strcpy_s(HardDriveSerialNumber, "");
memset(&version, 0, sizeof(version));
version.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&version);
if (version.dwPlatformId == VER_PLATFORM_WIN32_NT)
{
#ifdef PRINTING_TO_CONSOLE_ALLOWED
printf("\nTrying to read the drive IDs using physical access with admin rights\n");
#endif
done = ReadPhysicalDriveInNTWithAdminRights();
#ifdef PRINTING_TO_CONSOLE_ALLOWED
printf("\nTrying to read the drive IDs using the SCSI back door\n");
#endif
done = ReadIdeDriveAsScsiDriveInNT();
#ifdef PRINTING_TO_CONSOLE_ALLOWED
printf("\nTrying to read the drive IDs using physical access with zero rights\n");
#endif
done = ReadPhysicalDriveInNTWithZeroRights();
#ifdef PRINTING_TO_CONSOLE_ALLOWED
printf("\nTrying to read the drive IDs using Smart\n");
#endif
done = ReadPhysicalDriveInNTUsingSmart();
}
else
{
int attempt = 0;
for (attempt = 0;
attempt < 10 && !done && 0 == HardDriveSerialNumber[0];
attempt++)
done = ReadDrivePortsInWin9X();
}
if (HardDriveSerialNumber[0] > 0)
{
char *p = HardDriveSerialNumber;
WriteConstantString("HardDriveSerialNumber", HardDriveSerialNumber);
if (!strncmp(HardDriveSerialNumber, "WD-W", 4))
p += 5;
for (; p && *p; p++)
{
if ('-' == *p)
continue;
id *= 10;
switch (*p)
{
case '0': id += 0; break;
case '1': id += 1; break;
case '2': id += 2; break;
case '3': id += 3; break;
case '4': id += 4; break;
case '5': id += 5; break;
case '6': id += 6; break;
case '7': id += 7; break;
case '8': id += 8; break;
case '9': id += 9; break;
case 'a': case 'A': id += 10; break;
case 'b': case 'B': id += 11; break;
case 'c': case 'C': id += 12; break;
case 'd': case 'D': id += 13; break;
case 'e': case 'E': id += 14; break;
case 'f': case 'F': id += 15; break;
case 'g': case 'G': id += 16; break;
case 'h': case 'H': id += 17; break;
case 'i': case 'I': id += 18; break;
case 'j': case 'J': id += 19; break;
case 'k': case 'K': id += 20; break;
case 'l': case 'L': id += 21; break;
case 'm': case 'M': id += 22; break;
case 'n': case 'N': id += 23; break;
case 'o': case 'O': id += 24; break;
case 'p': case 'P': id += 25; break;
case 'q': case 'Q': id += 26; break;
case 'r': case 'R': id += 27; break;
case 's': case 'S': id += 28; break;
case 't': case 'T': id += 29; break;
case 'u': case 'U': id += 30; break;
case 'v': case 'V': id += 31; break;
case 'w': case 'W': id += 32; break;
case 'x': case 'X': id += 33; break;
case 'y': case 'Y': id += 34; break;
case 'z': case 'Z': id += 35; break;
}
}
}
id %= 100000000;
if (strstr(HardDriveModelNumber, "IBM-"))
id += 300000000;
else if (strstr(HardDriveModelNumber, "MAXTOR") ||
strstr(HardDriveModelNumber, "Maxtor"))
id += 400000000;
else if (strstr(HardDriveModelNumber, "WDC "))
id += 500000000;
else
id += 600000000;
#ifdef PRINTING_TO_CONSOLE_ALLOWED
printf("\nHard Drive Serial Number__________: %s\n",
HardDriveSerialNumber);
printf("\nHard Drive Model Number___________: %s\n",
HardDriveModelNumber);
printf("\nComputer ID_______________________: %I64d\n", id);
#endif
return (long)id;
}
#include <Iphlpapi.h>
#include <Assert.h>
#pragma comment(lib, "iphlpapi.lib")
static void PrintMACaddress(unsigned char MACData[])
{
#ifdef PRINTING_TO_CONSOLE_ALLOWED
printf("\nMAC Address: %02X-%02X-%02X-%02X-%02X-%02X\n",
MACData[0], MACData[1], MACData[2], MACData[3], MACData[4], MACData[5]);
#endif
char string[256];
sprintf_s(string, "%02X-%02X-%02X-%02X-%02X-%02X", MACData[0], MACData[1],
MACData[2], MACData[3], MACData[4], MACData[5]);
WriteConstantString("MACaddress", string);
}
DWORD GetMACaddress(void)
{
DWORD MACaddress = 0;
IP_ADAPTER_INFO AdapterInfo[16];
DWORD dwBufLen = sizeof(AdapterInfo);
DWORD dwStatus = GetAdaptersInfo(
AdapterInfo,
&dwBufLen);
assert(dwStatus == ERROR_SUCCESS);
PIP_ADAPTER_INFO pAdapterInfo = AdapterInfo;
do {
if (MACaddress == 0)
MACaddress = pAdapterInfo->Address[5] + pAdapterInfo->Address[4] * 256 +
pAdapterInfo->Address[3] * 256 * 256 +
pAdapterInfo->Address[2] * 256 * 256 * 256;
PrintMACaddress(pAdapterInfo->Address);
pAdapterInfo = pAdapterInfo->Next;
} while (pAdapterInfo);
return MACaddress;
}
static void dump_buffer(const char* title,
const unsigned char* buffer,
int len)
{
int i = 0;
int j;
printf("\n-- %s --\n", title);
if (len > 0)
{
printf("%8.8s ", " ");
for (j = 0; j < 16; ++j)
{
printf(" %2X", j);
}
printf(" ");
for (j = 0; j < 16; ++j)
{
printf("%1X", j);
}
printf("\n");
}
while (i < len)
{
printf("%08x ", i);
for (j = 0; j < 16; ++j)
{
if ((i + j) < len)
printf(" %02x", (int)buffer[i + j]);
else
printf(" ");
}
printf(" ");
for (j = 0; j < 16; ++j)
{
if ((i + j) < len)
printf("%c", isprint(buffer[i + j]) ? buffer[i + j] : '.');
else
printf(" ");
}
printf("\n");
i += 16;
}
printf("-- DONE --\n");
}
int main(int argc, char * argv[])
{
printf("To get all details use \"diskid32 /d\"\n");
if (argc > 1 && strstr(argv[1], "/d"))
PRINT_DEBUG = true;
else
PRINT_DEBUG = false;
long id = getHardDriveComputerID();
id = id;
GetMACaddress();
return 0;
}
https://www.winsim.com/diskid32/diskid32.cpp