📄 gethdidinfo3.cpp
字号:
ZeroMemory (&info, sizeof(info)); // 1. Try to load the VxD // must use the short file name path to open a VXD file //char StartupDirectory [2048]; //char shortFileNamePath [2048]; //char *p = NULL; //char vxd [2048]; // get the directory that the exe was started from //GetModuleFileName (hInst, (LPSTR) StartupDirectory, sizeof (StartupDirectory)); // cut the exe name from string //p = &(StartupDirectory [strlen (StartupDirectory) - 1]); //while (p >= StartupDirectory && *p && '\\' != *p) p--; //*p = '\0'; //GetShortPathName (StartupDirectory, shortFileNamePath, 2048); //sprintf (vxd, "\\\\.\\%s\\IDE21201.VXD", shortFileNamePath); //VxDHandle = CreateFile (vxd, 0, 0, 0, // 0, FILE_FLAG_DELETE_ON_CLOSE, 0); VxDHandle = CreateFile ("\\\\.\\IDE21201.VXD", 0, 0, 0, 0, FILE_FLAG_DELETE_ON_CLOSE, 0); if (VxDHandle != INVALID_HANDLE_VALUE) { // 2. Run VxD function DeviceIoControl (VxDHandle, m_cVxDFunctionIdesDInfo, 0, 0, pOutBufVxD, sizeof(pt_IdeDInfo), &lpBytesReturned, 0); // 3. Unload VxD CloseHandle (VxDHandle); } else MessageBox (NULL, "ERROR: Could not open IDE21201.VXD file", TITLE, MB_ICONSTOP); // 4. Translate and store data unsigned long int i = 0; 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]; // process the information for this buffer PrintIdeInfo (i, diskinfo); done = TRUE; } } // reset the thread priority back to normal // SetThreadPriority (GetCurrentThread(), THREAD_PRIORITY_NORMAL); SetPriorityClass (GetCurrentProcess (), NORMAL_PRIORITY_CLASS); return done;}#define SENDIDLENGTH sizeof (SENDCMDOUTPARAMS) + IDENTIFY_BUFFER_SIZEint ReadIdeDriveAsScsiDriveInNT (void){ int done = FALSE; int controller = 0; for (controller = 0; controller < 2; controller++) { HANDLE hScsiDriveIOCTL = 0; char driveName [256]; // Try to get a handle to PhysicalDrive IOCTL, report failure // and exit if can't. sprintf (driveName, "\\\\.\\Scsi%d:", controller); // Windows NT, Windows 2000, any rights should do hScsiDriveIOCTL = CreateFile (driveName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); // if (hScsiDriveIOCTL == INVALID_HANDLE_VALUE) // printf ("Unable to open SCSI controller %d, error code: 0x%lX\n", // controller, GetLastError ()); 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 ((char *) p -> Signature, "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;}char HardDriveSerialNumber [1024];void PrintIdeInfo (int drive, DWORD diskdata [256]){ char string1 [1024]; __int64 sectors = 0; __int64 bytes = 0; // copy the hard drive serial number to the buffer strcpy (string1, ConvertToString (diskdata, 10, 19)); if (0 == HardDriveSerialNumber [0] && // serial number must be alphanumeric // (but there can be leading spaces on IBM drives) (isalnum (string1 [0]) || isalnum (string1 [19]))) strcpy (HardDriveSerialNumber, string1);#ifdef PRINTING_TO_CONSOLE_ALLOWED switch (drive / 2) { case 0: printf ("\nPrimary Controller - "); break; case 1: printf ("\nSecondary Controller - "); break; case 2: printf ("\nTertiary Controller - "); break; case 3: printf ("\nQuaternary 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", ConvertToString (diskdata, 27, 46)); printf ("Drive Serial Number_______________: %s\n", ConvertToString (diskdata, 10, 19)); printf ("Drive Controller Revision Number__: %s\n", ConvertToString (diskdata, 23, 26)); printf ("Controller Buffer Size on Drive___: %u bytes\n", diskdata [21] * 512); printf ("Drive Type________________________: "); if (diskdata [0] & 0x0080) printf ("Removable\n"); else if (diskdata [0] & 0x0040) printf ("Fixed\n"); else printf ("Unknown\n"); // calculate size based on 28 bit or 48 bit addressing // 48 bit addressing is reflected by bit 10 of word 83 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]; // there are 512 bytes in a sector bytes = sectors * 512; printf ("Drive Size________________________: %I64d bytes\n", bytes);#else // PRINTING_TO_CONSOLE_ALLOWED // nothing to do#endif // PRINTING_TO_CONSOLE_ALLOWED}char *ConvertToString (DWORD diskdata [256], int firstIndex, int lastIndex){ static char string [1024]; int index = 0; int position = 0; // each integer has two characters stored in it backwards for (index = firstIndex; index <= lastIndex; index++) { // get high byte for 1st character string [position] = (char) (diskdata [index] / 256); position++; // get low byte for 2nd character string [position] = (char) (diskdata [index] % 256); position++; } // end the string string [position] = '\0'; // cut off the trailing blanks for (index = position - 1; index > 0 && ' ' == string [index]; index--) string [index] = '\0'; return string;}long getHardDriveComputerID (){ int done = FALSE; // char string [1024]; __int64 id = 0; OSVERSIONINFO version; strcpy (HardDriveSerialNumber, ""); memset (&version, 0, sizeof (version)); version.dwOSVersionInfoSize = sizeof (OSVERSIONINFO); GetVersionEx (&version); if (version.dwPlatformId == VER_PLATFORM_WIN32_NT) { // this works under WinNT4 or Win2K if you have admin rights#ifdef PRINTING_TO_CONSOLE_ALLOWED printf ("\nTrying to read the drive IDs using physical access\n");#endif done = ReadPhysicalDriveInNT (); // 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#ifdef PRINTING_TO_CONSOLE_ALLOWED printf ("\nTrying to read the drive IDs using the SCSI back door\n");#endif // if ( ! done) done = ReadIdeDriveAsScsiDriveInNT (); } else { // this works under Win9X and calls a VXD int attempt = 0; // try this up to 10 times to get a hard drive serial number for (attempt = 0; attempt < 10 && ! done && 0 == HardDriveSerialNumber [0]; attempt++) done = ReadDrivePortsInWin9X (); } if (done) { char *p = HardDriveSerialNumber; //WriteConstantString ("HardDriveSerialNumber", HardDriveSerialNumber); // ignore first 5 characters from western digital hard drives if // the first four characters are WD-W 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; } } }#ifdef PRINTING_TO_CONSOLE_ALLOWED printf ("\nComputer ID_______________________: %I64d\n", id);#endif return (long) id;}int main (int argc, char * argv []){ long id = getHardDriveComputerID (); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -