123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791 |
- #include "stdafx.h"
- #include "HardDriveSerialNumber.h"
- //#include <VersionHelpers.h>
- int MasterHardDiskSerial::ReadPhysicalDriveInNTWithAdminRights (void)
- {
- int iDone = FALSE;
- int iDrive = 0;
- for (iDrive = 0; iDrive < MAX_IDE_DRIVES; iDrive++)
- {
- HANDLE hPhysicalDriveIOCTL = 0;
- // Try to get a handle to PhysicalDrive IOCTL, report failure
- // and exit if can't.
- char cszDriveName [256];
- sprintf_s(cszDriveName, 256, "\\\\.\\PhysicalDrive%d", iDrive);
- // Windows NT, Windows 2000, must have admin rights
- hPhysicalDriveIOCTL = CreateFileA (cszDriveName,
- GENERIC_READ | GENERIC_WRITE,
- FILE_SHARE_READ | FILE_SHARE_WRITE , NULL,
- OPEN_EXISTING, 0, NULL);
- if (hPhysicalDriveIOCTL == INVALID_HANDLE_VALUE)
- {
- SecureZeroMemory(m_cszErrorMessage,sizeof(m_cszErrorMessage));
- sprintf_s(m_cszErrorMessage,256,"%d ReadPhysicalDriveInNTWithAdminRights ERROR ,CreateFileA(%s) returned INVALID_HANDLE_VALUE",__LINE__, cszDriveName);
- }
- else
- {
- GETVERSIONOUTPARAMS VersionParams;
- DWORD dwBytesReturned = 0;
- // Get the version, etc of PhysicalDrive IOCTL
- memset ((void*) &VersionParams, 0, sizeof(VersionParams));
- if ( ! DeviceIoControl (hPhysicalDriveIOCTL, DFP_GET_VERSION,
- NULL,
- 0,
- &VersionParams,
- sizeof(VersionParams),
- &dwBytesReturned, NULL) )
- {
- DWORD dwErr = GetLastError ();
- SecureZeroMemory(m_cszErrorMessage,sizeof(m_cszErrorMessage));
- #pragma warning(disable: 4311)
- #pragma warning(disable: 4302)
- int nPhysicalDriveIOCTL = (int)hPhysicalDriveIOCTL;
- #pragma warning(default: 4311)
- #pragma warning(default: 4302)
- sprintf_s(m_cszErrorMessage,256,"%d ReadPhysicalDriveInNTWithAdminRights ERROR DeviceIoControl() %d, DFP_GET_VERSION) returned 0, error is %d\n",__LINE__, nPhysicalDriveIOCTL, (int) dwErr);
- }
- // If there is a IDE device at number "iI" issue commands
- // to the device
- if (VersionParams.bIDEDeviceMap <= 0)
- {
- SecureZeroMemory(m_cszErrorMessage,sizeof(m_cszErrorMessage));
- sprintf_s(m_cszErrorMessage,256,"%d ReadPhysicalDriveInNTWithAdminRights ERROR No device found at iPosition %d (%d)",__LINE__, (int) iDrive, (int) VersionParams.bIDEDeviceMap);
- }
- else
- {
- BYTE bIDCmd = 0; // IDE or ATAPI IDENTIFY cmd
- SENDCMDINPARAMS scip;
- //SENDCMDOUTPARAMS OutCmd;
- // Now, get the ID sector for all IDE devices in the system.
- // If the device is ATAPI use the IDE_ATAPI_IDENTIFY command,
- // otherwise use the IDE_ATA_IDENTIFY command
- bIDCmd = (VersionParams.bIDEDeviceMap >> iDrive & 0x10) ? \
- IDE_ATAPI_IDENTIFY : IDE_ATA_IDENTIFY;
- memset (&scip, 0, sizeof(scip));
- memset (byIdOutCmd, 0, sizeof(byIdOutCmd));
- if ( DoIDENTIFY (hPhysicalDriveIOCTL,
- &scip,
- (PSENDCMDOUTPARAMS)&byIdOutCmd,
- (BYTE) bIDCmd,
- (BYTE) iDrive,
- &dwBytesReturned))
- {
- DWORD dwDiskData [256];
- int iIjk = 0;
- USHORT *punIdSector = (USHORT *)
- ((PSENDCMDOUTPARAMS) byIdOutCmd) -> bBuffer;
- for (iIjk = 0; iIjk < 256; iIjk++)
- dwDiskData [iIjk] = punIdSector [iIjk];
- PrintIdeInfo (iDrive, dwDiskData);
- iDone = TRUE;
- }
- }
- CloseHandle (hPhysicalDriveIOCTL);
- }
- }
- return iDone;
- }
- int MasterHardDiskSerial::ReadPhysicalDriveInNTUsingSmart (void)
- {
- int iDone = FALSE;
- int iDrive = 0;
- for (iDrive = 0; iDrive < MAX_IDE_DRIVES; iDrive++)
- {
- HANDLE hPhysicalDriveIOCTL = 0;
- // Try to get a handle to PhysicalDrive IOCTL, report failure
- // and exit if can't.
- char cszDriveName [256];
- sprintf_s(cszDriveName,256, "\\\\.\\PhysicalDrive%d", iDrive);
- // Windows NT, Windows 2000, Windows Server 2003, Vista
- hPhysicalDriveIOCTL = CreateFileA (cszDriveName,
- GENERIC_READ | GENERIC_WRITE,
- FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
- NULL, OPEN_EXISTING, 0, NULL);
- // if (hPhysicalDriveIOCTL == INVALID_HANDLE_VALUE)
- // printf ("Unable to open physical iDrive %d, error code: 0x%lX\n",
- // iDrive, GetLastError ());
- if (hPhysicalDriveIOCTL == INVALID_HANDLE_VALUE)
- {
- SecureZeroMemory(m_cszErrorMessage,sizeof(m_cszErrorMessage));
- sprintf_s(m_cszErrorMessage,256,"%d ReadPhysicalDriveInNTUsingSmart ERROR, CreateFileA(%s) returned INVALID_HANDLE_VALUE Error Code %d",__LINE__, cszDriveName, GetLastError ());
- }
- else
- {
- GETVERSIONINPARAMS GetVersionParams;
- DWORD dwBytesReturned = 0;
- // Get the version, etc of PhysicalDrive IOCTL
- memset ((void*) & GetVersionParams, 0, sizeof(GetVersionParams));
- #pragma warning(disable: 4311)
- #pragma warning(disable: 4302)
- if ( ! DeviceIoControl (hPhysicalDriveIOCTL, SMART_GET_VERSION,
- NULL,
- 0,
- &GetVersionParams, sizeof (GETVERSIONINPARAMS),
- &dwBytesReturned, NULL) )
- {
- DWORD dwErr = GetLastError ();
- SecureZeroMemory(m_cszErrorMessage,sizeof(m_cszErrorMessage));
- int nPhysicalDriveIOCTL = (int) hPhysicalDriveIOCTL;
- sprintf_s(m_cszErrorMessage,256,"\n%d ReadPhysicalDriveInNTUsingSmart ERROR DeviceIoControl(%d, SMART_GET_VERSION) returned 0, error is %d",__LINE__, nPhysicalDriveIOCTL, (int) dwErr);
- }
- #pragma warning(default: 4311)
- #pragma warning(default: 4302)
- else
- {
- // Print the SMART version
- // PrintVersion (& GetVersionParams);
- // Allocate the command cszBuffer
- ULONG CommandSize = sizeof(SENDCMDINPARAMS) + IDENTIFY_BUFFER_SIZE;
- PSENDCMDINPARAMS Command = (PSENDCMDINPARAMS) malloc (CommandSize);
- // Retrieve the IDENTIFY data
- // Prepare the command
- #define ID_CMD 0xEC // Returns ID sector for ATA
- Command -> irDriveRegs.bCommandReg = ID_CMD;
- DWORD BytesReturned = 0;
- if ( ! DeviceIoControl (hPhysicalDriveIOCTL,
- SMART_RCV_DRIVE_DATA, Command, sizeof(SENDCMDINPARAMS),
- Command, CommandSize,
- &BytesReturned, NULL) )
- {
- SecureZeroMemory(m_cszErrorMessage,sizeof(m_cszErrorMessage));
- sprintf_s(m_cszErrorMessage,256,"SMART_RCV_DRIVE_DATA IOCTL");
- // Print the error
- //PrintError ("SMART_RCV_DRIVE_DATA IOCTL", GetLastError());
- }
- else
- {
- // Print the IDENTIFY data
- DWORD dwDiskData [256];
- USHORT *punIdSector = (USHORT *)
- (PIDENTIFY_DATA) ((PSENDCMDOUTPARAMS) Command) -> bBuffer;
- for (int iIjk = 0; iIjk < 256; iIjk++)
- dwDiskData [iIjk] = punIdSector [iIjk];
- PrintIdeInfo (iDrive, dwDiskData);
- iDone = TRUE;
- }
- // Done
- CloseHandle (hPhysicalDriveIOCTL);
- free (Command);
- }
- }
- }
- return iDone;
- }
- char * MasterHardDiskSerial::flipAndCodeBytes ( int iPos, int iFlip, const char * pcszStr, char * pcszBuf)
- {
- int iI;
- int iJ = 0;
- int iK = 0;
- pcszBuf [0] = '\0';
- if (iPos <= 0)
- return pcszBuf;
- if ( ! iJ)
- {
- char cP = 0;
- // First try to gather all characters representing hex digits only.
- iJ = 1;
- iK = 0;
- pcszBuf[iK] = 0;
- for (iI = iPos; iJ && !(pcszStr[iI] == '\0'); ++iI)
- {
- char cC = (char)tolower(pcszStr[iI]);
- if (isspace(cC))
- cC = '0';
- ++cP;
- pcszBuf[iK] <<= 4;
- if (cC >= '0' && cC <= '9')
- pcszBuf[iK] |= (char) (cC - '0');
- else if (cC >= 'a' && cC <= 'f')
- pcszBuf[iK] |= (char) (cC - 'a' + 10);
- else
- {
- iJ = 0;
- break;
- }
- if (cP == 2)
- {
- if ((pcszBuf[iK] != '\0') && ! isprint(pcszBuf[iK]))
- {
- iJ = 0;
- break;
- }
- ++iK;
- cP = 0;
- pcszBuf[iK] = 0;
- }
- }
- }
- if ( ! iJ)
- {
- // There are non-digit characters, gather them as is.
- iJ = 1;
- iK = 0;
- for (iI = iPos; iJ && (pcszStr[iI] != '\0'); ++iI)
- {
- char cC = pcszStr[iI];
- if ( ! isprint(cC))
- {
- iJ = 0;
- break;
- }
- pcszBuf[iK++] = cC;
- }
- }
- if ( ! iJ)
- {
- // The characters are not there or are not printable.
- iK = 0;
- }
- pcszBuf[iK] = '\0';
- if (iFlip)
- // Flip adjacent characters
- for (iJ = 0; iJ < iK; iJ += 2)
- {
- char t = pcszBuf[iJ];
- pcszBuf[iJ] = pcszBuf[iJ + 1];
- pcszBuf[iJ + 1] = t;
- }
- // Trim any beginning and end space
- iI = iJ = -1;
- for (iK = 0; (pcszBuf[iK] != '\0'); ++iK)
- {
- if (! isspace(pcszBuf[iK]))
- {
- if (iI < 0)
- iI = iK;
- iJ = iK;
- }
- }
- if ((iI >= 0) && (iJ >= 0))
- {
- for (iK = iI; (iK <= iJ) && (pcszBuf[iK] != '\0'); ++iK)
- pcszBuf[iK - iI] = pcszBuf[iK];
- pcszBuf[iK - iI] = '\0';
- }
- return pcszBuf;
- }
- int MasterHardDiskSerial::ReadPhysicalDriveInNTWithZeroRights (void)
- {
- int iDone = FALSE;
- int iDrive = 0;
- for (iDrive = 0; iDrive < MAX_IDE_DRIVES; iDrive++)
- {
- HANDLE hPhysicalDriveIOCTL = 0;
- // Try to get a handle to PhysicalDrive IOCTL, report failure
- // and exit if can't.
- char cszDriveName [256];
- sprintf_s(cszDriveName,256,"\\\\.\\PhysicalDrive%d", iDrive);
- // Windows NT, Windows 2000, Windows XP - admin rights not required
- hPhysicalDriveIOCTL = CreateFileA (cszDriveName, 0,
- FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
- OPEN_EXISTING, 0, NULL);
- if (hPhysicalDriveIOCTL == INVALID_HANDLE_VALUE)
- {
- SecureZeroMemory(m_cszErrorMessage,sizeof(m_cszErrorMessage));
- sprintf_s(m_cszErrorMessage,256,"%d ReadPhysicalDriveInNTWithZeroRights ERROR CreateFileA(%s) returned INVALID_HANDLE_VALUE",__LINE__, cszDriveName);
- }
- else
- {
- STORAGE_PROPERTY_QUERY query;
- DWORD dwBytesReturned = 0;
- char cszBuffer [10000];
- memset ((void *) & query, 0, sizeof (query));
- query.PropertyId = StorageDeviceProperty;
- query.QueryType = PropertyStandardQuery;
- memset (cszBuffer, 0, sizeof (cszBuffer));
- if ( DeviceIoControl (hPhysicalDriveIOCTL, IOCTL_STORAGE_QUERY_PROPERTY,
- & query,
- sizeof (query),
- & cszBuffer,
- sizeof (cszBuffer),
- & dwBytesReturned, NULL) )
- {
- STORAGE_DEVICE_DESCRIPTOR * descrip = (STORAGE_DEVICE_DESCRIPTOR *) & cszBuffer;
- char cszSerialNumber [1000];
- char cszModelNumber [1000];
- char cszVendorId [1000];
- char cszProductRevision [1000];
- flipAndCodeBytes ( descrip -> VendorIdOffset,
- 0,cszBuffer, cszVendorId );
- flipAndCodeBytes ( descrip -> ProductIdOffset,
- 0,cszBuffer, cszModelNumber );
- flipAndCodeBytes ( descrip -> ProductRevisionOffset,
- 0,cszBuffer, cszProductRevision );
- flipAndCodeBytes ( descrip -> SerialNumberOffset,
- 1,cszBuffer, cszSerialNumber );
- if (0 == m_cszHardDriveSerialNumber [0] &&
- // serial number must be alphanumeric
- // (but there can be leading spaces on IBM drives)
- (iswalnum (cszSerialNumber [0]) || iswalnum (cszSerialNumber [19])))
- {
- strcpy_s(m_cszHardDriveSerialNumber, 1024, cszSerialNumber);
- strcpy_s(m_cszHardDriveModelNumber, 1024, cszModelNumber);
- iDone = TRUE;
- }
- // Get the disk iDrive geometry.
- memset (cszBuffer, 0, sizeof(cszBuffer));
- if ( ! DeviceIoControl (hPhysicalDriveIOCTL,
- IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
- NULL,
- 0,
- &cszBuffer,
- sizeof(cszBuffer),
- &dwBytesReturned,
- NULL))
- {
- SecureZeroMemory(m_cszErrorMessage,sizeof(m_cszErrorMessage));
- sprintf_s(m_cszErrorMessage,"%s ReadPhysicalDriveInNTWithZeroRights ERROR DeviceIoControl(), IOCTL_DISK_GET_DRIVE_GEOMETRY_EX) returned 0", cszDriveName);
- }
- //else
- //{
- // DISK_GEOMETRY_EX* geom = (DISK_GEOMETRY_EX*) &cszBuffer;
- // int iFixed = (geom->Geometry.MediaType == FixedMedia);
- // __int64 i64Size = geom->DiskSize.QuadPart;
- //}
- }
- else
- {
- DWORD dwErr = GetLastError ();
- SecureZeroMemory(m_cszErrorMessage,sizeof(m_cszErrorMessage));
- sprintf_s (m_cszErrorMessage,"DeviceIOControl IOCTL_STORAGE_QUERY_PROPERTY error = %d\n", dwErr);
- }
- CloseHandle (hPhysicalDriveIOCTL);
- }
- }
- return iDone;
- }
- BOOL MasterHardDiskSerial::DoIDENTIFY (HANDLE hPhysicalDriveIOCTL, PSENDCMDINPARAMS pSCIP,
- PSENDCMDOUTPARAMS pSCOP, BYTE bIDCmd, BYTE bDriveNum,
- PDWORD lpcbBytesReturned)
- {
- // Set up data structures for IDENTIFY command.
- pSCIP -> cBufferSize = IDENTIFY_BUFFER_SIZE;
- pSCIP -> irDriveRegs.bFeaturesReg = 0;
- pSCIP -> irDriveRegs.bSectorCountReg = 1;
- //pSCIP -> irDriveRegs.bSectorNumberReg = 1;
- pSCIP -> irDriveRegs.bCylLowReg = 0;
- pSCIP -> irDriveRegs.bCylHighReg = 0;
- // Compute the iDrive number.
- pSCIP -> irDriveRegs.bDriveHeadReg = 0xA0 | ((bDriveNum & 1) << 4);
- // The command can either be IDE identify or ATAPI identify.
- 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) );
- }
- int MasterHardDiskSerial::ReadIdeDriveAsScsiDriveInNT (void)
- {
- int iDone = FALSE;
- int iController = 0;
- for (iController = 0; iController < 2; iController++)
- {
- HANDLE hScsiDriveIOCTL = 0;
- char cszDriveName [256];
- // Try to get a handle to PhysicalDrive IOCTL, report failure
- // and exit if can't.
- sprintf_s (cszDriveName, "\\\\.\\Scsi%d:", iController);
- // Windows NT, Windows 2000, any rights should do
- hScsiDriveIOCTL = CreateFileA (cszDriveName,
- GENERIC_READ | GENERIC_WRITE,
- FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
- OPEN_EXISTING, 0, NULL);
- if (hScsiDriveIOCTL != INVALID_HANDLE_VALUE)
- {
- int iDrive = 0;
- for (iDrive = 0; iDrive < 2; iDrive++)
- {
- char cszBuffer [sizeof (SRB_IO_CONTROL) + SENDIDLENGTH];
- SRB_IO_CONTROL *cP = (SRB_IO_CONTROL *) cszBuffer;
- SENDCMDINPARAMS *pin =
- (SENDCMDINPARAMS *) (cszBuffer + sizeof (SRB_IO_CONTROL));
- DWORD dwDummy;
- memset (cszBuffer, 0, sizeof (cszBuffer));
- cP -> HeaderLength = sizeof (SRB_IO_CONTROL);
- cP -> Timeout = 10000;
- cP -> Length = SENDIDLENGTH;
- cP -> ControlCode = IOCTL_SCSI_MINIPORT_IDENTIFY;
- cP -> Signature[0] = 'S';
- cP -> Signature[1] = 'C';
- cP -> Signature[2] = 'S';
- cP -> Signature[3] = 'I';
- cP -> Signature[4] = 'D';
- cP -> Signature[5] = 'I';
- cP -> Signature[6] = 'S';
- cP -> Signature[7] = 'K';
- //strncpy ((char *) cP -> Signature, "SCSIDISK", 8);
- pin -> irDriveRegs.bCommandReg = IDE_ATA_IDENTIFY;
- pin -> bDriveNumber = (BYTE)iDrive;
- if (DeviceIoControl (hScsiDriveIOCTL, IOCTL_SCSI_MINIPORT,
- cszBuffer,
- sizeof (SRB_IO_CONTROL) +
- sizeof (SENDCMDINPARAMS) - 1,
- cszBuffer,
- sizeof (SRB_IO_CONTROL) + SENDIDLENGTH,
- &dwDummy, NULL))
- {
- SENDCMDOUTPARAMS *pOut =
- (SENDCMDOUTPARAMS *) (cszBuffer + sizeof (SRB_IO_CONTROL));
- IDSECTOR *pId = (IDSECTOR *) (pOut -> bBuffer);
- if (pId -> sModelNumber [0])
- {
- DWORD dwDiskData [256];
- int iIjk = 0;
- USHORT *punIdSector = (USHORT *) pId;
- for (iIjk = 0; iIjk < 256; iIjk++)
- dwDiskData [iIjk] = punIdSector [iIjk];
- PrintIdeInfo (iController * 2 + iDrive, dwDiskData);
- iDone = TRUE;
- }
- }
- }
- CloseHandle (hScsiDriveIOCTL);
- }
- }
- return iDone;
- }
- void MasterHardDiskSerial::PrintIdeInfo (int /*iDrive*/, DWORD dwDiskData [256])
- {
- char cszSerialNumber [1024];
- char cszModelNumber [1024];
- char cszRevisionNumber [1024];
- char bufferSize [32];
- //__int64 i64Sectors = 0;
- //__int64 i64Byte = 0;
- // copy the hard iDrive serial number to the cszBuffer
- ConvertToString (dwDiskData, 10, 19, cszSerialNumber);
- ConvertToString (dwDiskData, 27, 46, cszModelNumber);
- ConvertToString (dwDiskData, 23, 26, cszRevisionNumber);
- sprintf_s(bufferSize,32, "%u", dwDiskData [21] * 512);
- if (0 == m_cszHardDriveSerialNumber [0] &&
- // serial number must be alphanumeric
- // (but there can be leading spaces on IBM drives)
- (isalnum (cszSerialNumber [0]) || isalnum (cszSerialNumber [19])))
- {
- strcpy_s(m_cszHardDriveSerialNumber,1024, cszSerialNumber);
- strcpy_s(m_cszHardDriveModelNumber,1024, cszModelNumber);
- }
- }
- /**
- * Get the id of this computer as a string.
- */
- CString MasterHardDiskSerial::GetHardDriveComputerIdAsString(void)
- {
- if (m_nComputerId == 0)
- {
- getHardDriveComputerID();
- }
- CString sRet;
- int nHiHi, nHiLo, nLoHi, nLoLo;
- #pragma warning( push )
- #pragma warning( disable : 4244 )
- nHiHi = m_nComputerId >> 48;
- nHiLo = (m_nComputerId >> 32) & 0xfff;
- nLoHi = (m_nComputerId >> 16) & 0xfff;
- nLoLo = m_nComputerId & 0xffff;
- #pragma warning( pop )
- CString sId;
- sId.Format(_T("%04X%04X%04X%04X"),
- nHiHi, nHiLo, nLoHi, nLoLo);
- int nSeedX = 12 - m_nComputerId % 10;
- int nSeedY = 15 - nSeedX;
- CString sEncryptId;
- int nIdLen = sId.GetLength();
- LPTSTR idStr = sEncryptId.GetBuffer(nIdLen);
- for (unsigned int i = 0; i < nIdLen; ++i)
- {
- int nValue = 0;
- if (sId[i] >= _T('0') && sId[i] <= _T('9'))
- {
- nValue = sId[i] - _T('0');
- }
- if (sId[i] >= _T('a') && sId[i] <= _T('f'))
- {
- nValue = sId[i] - _T('a') + 10;
- }
- if (sId[i] >= _T('A') && sId[i] <= _T('F'))
- {
- nValue = sId[i] - _T('A') + 10;
- }
- idStr[i] = _T('A') + ((nValue + i + 1) * nSeedX + nSeedY) % 26;
- }
- sEncryptId.ReleaseBuffer();
- sRet.Format(_T("%s-%s-%s-%s"),
- (LPCTSTR)sEncryptId.Right(4), (LPCTSTR)sEncryptId.Left(4), (LPCTSTR)sEncryptId.Mid(4, 4), (LPCTSTR)sEncryptId.Mid(8, 4)
- );
- return sRet;
- }
- __int64 MasterHardDiskSerial::getHardDriveComputerID()
- {
- int iDone = FALSE;
- m_nComputerId = 0;
- strcpy_s(m_cszHardDriveSerialNumber,1024, "");
- //if (IsWindowsXPOrGreater())
- if (true)
- {
- // this works under WinNT4 or Win2K or WinXP if you have any rights
- if (!iDone)
- iDone = ReadPhysicalDriveInNTWithZeroRights ();
- // this works under WinNT4 or Win2K or WinXP or Windows Server 2003 or Vista if you have any rights
- if (!iDone)
- iDone = ReadPhysicalDriveInNTUsingSmart ();
- // this works under WinNT4 or Win2K if you have admin rights
- if (!iDone)
- iDone = ReadPhysicalDriveInNTWithAdminRights();
- // this should work in WinNT or Win2K if previous did not work
- // this is kind of a backdoor via the SCSI mini port driver into
- // the IDE drives
- if (!iDone)
- iDone = ReadIdeDriveAsScsiDriveInNT ();
- }
- else
- {
- return 0;
- }
- if (m_cszHardDriveSerialNumber [0] > 0)
- {
- char *cP = m_cszHardDriveSerialNumber;
- // ignore first 5 characters from western digital hard drives if
- // the first four characters are WD-W
- if ( ! strncmp (m_cszHardDriveSerialNumber, "WD-W", 4))
- cP += 5;
- for ( ; cP && *cP; cP++)
- {
- if ('-' == *cP)
- continue;
- m_nComputerId *= 10;
- switch (*cP)
- {
- case '0': m_nComputerId += 0; break;
- case '1': m_nComputerId += 1; break;
- case '2': m_nComputerId += 2; break;
- case '3': m_nComputerId += 3; break;
- case '4': m_nComputerId += 4; break;
- case '5': m_nComputerId += 5; break;
- case '6': m_nComputerId += 6; break;
- case '7': m_nComputerId += 7; break;
- case '8': m_nComputerId += 8; break;
- case '9': m_nComputerId += 9; break;
- case 'a': case 'A': m_nComputerId += 10; break;
- case 'b': case 'B': m_nComputerId += 11; break;
- case 'c': case 'C': m_nComputerId += 12; break;
- case 'd': case 'D': m_nComputerId += 13; break;
- case 'e': case 'E': m_nComputerId += 14; break;
- case 'f': case 'F': m_nComputerId += 15; break;
- case 'g': case 'G': m_nComputerId += 16; break;
- case 'h': case 'H': m_nComputerId += 17; break;
- case 'i': case 'I': m_nComputerId += 18; break;
- case 'j': case 'J': m_nComputerId += 19; break;
- case 'k': case 'K': m_nComputerId += 20; break;
- case 'l': case 'L': m_nComputerId += 21; break;
- case 'm': case 'M': m_nComputerId += 22; break;
- case 'n': case 'N': m_nComputerId += 23; break;
- case 'o': case 'O': m_nComputerId += 24; break;
- case 'p': case 'P': m_nComputerId += 25; break;
- case 'q': case 'Q': m_nComputerId += 26; break;
- case 'r': case 'R': m_nComputerId += 27; break;
- case 's': case 'S': m_nComputerId += 28; break;
- case 't': case 'T': m_nComputerId += 29; break;
- case 'u': case 'U': m_nComputerId += 30; break;
- case 'v': case 'V': m_nComputerId += 31; break;
- case 'w': case 'W': m_nComputerId += 32; break;
- case 'x': case 'X': m_nComputerId += 33; break;
- case 'y': case 'Y': m_nComputerId += 34; break;
- case 'z': case 'Z': m_nComputerId += 35; break;
- }
- }
- }
- m_nComputerId %= 100000000;
- if (strstr (m_cszHardDriveModelNumber, "IBM-"))
- m_nComputerId += 300000000;
- else if (strstr (m_cszHardDriveModelNumber, "MAXTOR") ||
- strstr (m_cszHardDriveModelNumber, "Maxtor"))
- m_nComputerId += 400000000;
- else if (strstr (m_cszHardDriveModelNumber, "WDC "))
- m_nComputerId += 500000000;
- else
- m_nComputerId += 600000000;
- return m_nComputerId;
- }
- int MasterHardDiskSerial::GetSerialNo(std::vector<char> &serialNumber)
- {
- getHardDriveComputerID();
- size_t numberLength = strlen(m_cszHardDriveSerialNumber);
- if (numberLength == 0)
- return -1;
- serialNumber.resize(numberLength);
- memcpy(&serialNumber.front(), m_cszHardDriveSerialNumber, serialNumber.size());
- return 0;
- }
- char *MasterHardDiskSerial::ConvertToString (DWORD dwDiskData [256],
- int iFirstIndex,
- int iLastIndex,
- char* pcszBuf)
- {
- int iIndex = 0;
- int iPosition = 0;
- // each integer has two characters stored in it backwards
- // Removes the spaces from the serial no
- for ( iIndex = iFirstIndex; iIndex <= iLastIndex ; iIndex++ )
- {
- // get high byte for 1st character
- char ctemp = (char) (dwDiskData [iIndex] / 256);
- char cszmyspace[] = " ";
- if ( !(ctemp == *cszmyspace))
- {
- pcszBuf [iPosition++] = ctemp ;
- }
- // get low byte for 2nd character
- char ctemp1 = (char) (dwDiskData [iIndex] % 256);
- if ( !(ctemp1 == *cszmyspace))
- {
- pcszBuf [iPosition++] = ctemp1 ;
- }
- }
- // end the string
- pcszBuf[iPosition] = '\0';
- // cut off the trailing blanks
- for (iIndex = iPosition - 1; iIndex > 0 && isspace(pcszBuf [iIndex]); iIndex--)
- pcszBuf [iIndex] = '\0';
- return pcszBuf;
- }
- int MasterHardDiskSerial::GetErrorMessage(TCHAR* tszErrorMessage)
- {
- if (strlen(m_cszErrorMessage)!=0)
- {
- size_t len = 0;
- mbstowcs_s(&len, (wchar_t *)tszErrorMessage, sizeof(m_cszErrorMessage) + 1, m_cszErrorMessage, sizeof(m_cszErrorMessage));
- //mbstowcs((wchar_t *)tszErrorMessage,m_cszErrorMessage,sizeof(m_cszErrorMessage));
- return 0;
- }
- else
- return -1;
- }
- MasterHardDiskSerial::MasterHardDiskSerial()
- : m_nComputerId(0)
- {
- SecureZeroMemory(m_cszErrorMessage,sizeof(m_cszErrorMessage));
- SecureZeroMemory(m_cszHardDriveModelNumber,sizeof(m_cszHardDriveModelNumber));
- SecureZeroMemory(m_cszHardDriveSerialNumber,sizeof(m_cszHardDriveSerialNumber));
- }
- MasterHardDiskSerial::~MasterHardDiskSerial()
- {
- }
|