📄 diskid32.cpp
字号:
// // The SCSI-2 device type // UCHAR DeviceType; // // The SCSI-2 device type modifier (if any) - this may be zero // UCHAR DeviceTypeModifier; // // Flag indicating whether the device's media (if any) is removable. This // field should be ignored for media-less devices // BOOLEAN RemovableMedia; // // Flag indicating whether the device can support mulitple outstanding // commands. The actual synchronization in this case is the responsibility // of the port driver. // BOOLEAN CommandQueueing; // // Byte offset to the zero-terminated ascii string containing the device's // vendor id string. For devices with no such ID this will be zero // ULONG VendorIdOffset; // // Byte offset to the zero-terminated ascii string containing the device's // product id string. For devices with no such ID this will be zero // ULONG ProductIdOffset; // // Byte offset to the zero-terminated ascii string containing the device's // product revision string. For devices with no such string this will be // zero // ULONG ProductRevisionOffset; // // Byte offset to the zero-terminated ascii string containing the device's // serial number. For devices with no serial number this will be zero // ULONG SerialNumberOffset; // // Contains the bus type (as defined above) of the device. It should be // used to interpret the raw device properties at the end of this structure // (if any) // STORAGE_BUS_TYPE BusType; // // The number of bytes of bus-specific data which have been appended to // this descriptor // ULONG RawPropertiesLength; // // Place holder for the first byte of the bus specific property data // UCHAR RawDeviceProperties[1];} STORAGE_DEVICE_DESCRIPTOR, *PSTORAGE_DEVICE_DESCRIPTOR; // function to decode the serial numbers of IDE hard drives // using the IOCTL_STORAGE_QUERY_PROPERTY command char * flipAndCodeBytes (char * str){ static char flipped [1000]; int num = strlen (str); strcpy (flipped, ""); for (int i = 0; i < num; i += 4) { for (int j = 1; j >= 0; j--) { int sum = 0; for (int k = 0; k < 2; k++) { sum *= 16; switch (str [i + j * 2 + k]) { case '0': sum += 0; break; case '1': sum += 1; break; case '2': sum += 2; break; case '3': sum += 3; break; case '4': sum += 4; break; case '5': sum += 5; break; case '6': sum += 6; break; case '7': sum += 7; break; case '8': sum += 8; break; case '9': sum += 9; break; case 'a': sum += 10; break; case 'b': sum += 11; break; case 'c': sum += 12; break; case 'd': sum += 13; break; case 'e': sum += 14; break; case 'f': sum += 15; break; } } if (sum > 0) { char sub [2]; sub [0] = (char) sum; sub [1] = 0; strcat (flipped, sub); } } } return flipped;}typedef struct _MEDIA_SERAL_NUMBER_DATA { ULONG SerialNumberLength; ULONG Result; ULONG Reserved[2]; UCHAR SerialNumberData[1];} MEDIA_SERIAL_NUMBER_DATA, *PMEDIA_SERIAL_NUMBER_DATA;int ReadPhysicalDriveInNTWithZeroRights (void){ int done = FALSE; int drive = 0; for (drive = 0; drive < MAX_IDE_DRIVES; drive++) { HANDLE hPhysicalDriveIOCTL = 0; // Try to get a handle to PhysicalDrive IOCTL, report failure // and exit if can't. char driveName [256]; sprintf (driveName, "\\\\.\\PhysicalDrive%d", drive); // Windows NT, Windows 2000, Windows XP - admin rights not required hPhysicalDriveIOCTL = CreateFile (driveName, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); // if (hPhysicalDriveIOCTL == INVALID_HANDLE_VALUE) // printf ("Unable to open physical drive %d, error code: 0x%lX\n", // drive, GetLastError ()); if (hPhysicalDriveIOCTL != INVALID_HANDLE_VALUE) { STORAGE_PROPERTY_QUERY query; DWORD cbBytesReturned = 0; char buffer [10000]; memset ((void *) & query, 0, sizeof (query)); query.PropertyId = StorageDeviceProperty; query.QueryType = PropertyStandardQuery; memset (buffer, 0, sizeof (buffer)); if ( DeviceIoControl (hPhysicalDriveIOCTL, IOCTL_STORAGE_QUERY_PROPERTY, & query, sizeof (query), & buffer, sizeof (buffer), & cbBytesReturned, NULL) ) { STORAGE_DEVICE_DESCRIPTOR * descrip = (STORAGE_DEVICE_DESCRIPTOR *) & buffer; char serialNumber [1000]; strcpy (serialNumber, flipAndCodeBytes ( & buffer [descrip -> SerialNumberOffset])); if (0 == HardDriveSerialNumber [0] && // serial number must be alphanumeric // (but there can be leading spaces on IBM drives) (isalnum (serialNumber [0]) || isalnum (serialNumber [19]))) strcpy (HardDriveSerialNumber, serialNumber);#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, & buffer [descrip -> VendorIdOffset], & buffer [descrip -> ProductIdOffset], & buffer [descrip -> ProductRevisionOffset], serialNumber);#endif } else { DWORD err = GetLastError ();#ifdef PRINTING_TO_CONSOLE_ALLOWED printf ("\nDeviceIOControl IOCTL_STORAGE_QUERY_PROPERTY error = %d\n", err);#endif } memset (buffer, 0, sizeof (buffer)); if ( DeviceIoControl (hPhysicalDriveIOCTL, IOCTL_STORAGE_GET_MEDIA_SERIAL_NUMBER, NULL, 0, & buffer, sizeof (buffer), & cbBytesReturned, NULL) ) { MEDIA_SERIAL_NUMBER_DATA * mediaSerialNumber = (MEDIA_SERIAL_NUMBER_DATA *) & buffer; char serialNumber [1000]; strcpy (serialNumber, (char *) mediaSerialNumber -> SerialNumberData); if (0 == HardDriveSerialNumber [0] && // serial number must be alphanumeric // (but there can be leading spaces on IBM drives) (isalnum (serialNumber [0]) || isalnum (serialNumber [19]))) strcpy (HardDriveSerialNumber, serialNumber);#ifdef PRINTING_TO_CONSOLE_ALLOWED printf ("\n**** MEDIA_SERIAL_NUMBER_DATA for drive %d ****\n" "Serial Number = %s\n", drive, serialNumber);#endif } else { DWORD err = GetLastError ();#ifdef PRINTING_TO_CONSOLE_ALLOWED switch (err) { case 1: printf ("\nDeviceIOControl IOCTL_STORAGE_GET_MEDIA_SERIAL_NUMBER error = \n" " The request is not valid for this device.\n\n"); break; case 50: printf ("\nDeviceIOControl IOCTL_STORAGE_GET_MEDIA_SERIAL_NUMBER error = \n" " The request is not supported for this device.\n\n"); break; default: printf ("\nDeviceIOControl IOCTL_STORAGE_GET_MEDIA_SERIAL_NUMBER error = %d\n\n", err); }#endif } CloseHandle (hPhysicalDriveIOCTL); } } return done;} // DoIDENTIFY // FUNCTION: Send an IDENTIFY command to the drive // bDriveNum = 0-3 // bIDCmd = IDE_ATA_IDENTIFY or IDE_ATAPI_IDENTIFYBOOL 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 drive 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) );}// --------------------------------------------------- // (* Output Bbuffer for the VxD (rt_IdeDinfo record) *)typedef struct _rt_IdeDInfo_{ BYTE IDEExists[4]; BYTE DiskExists[8]; WORD DisksRawInfo[8*256];} rt_IdeDInfo, *pt_IdeDInfo; // (* IdeDinfo "data fields" *)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; HANDLE VxDHandle = 0; pt_IdeDInfo pOutBufVxD = 0; DWORD lpBytesReturned = 0; // set the thread priority high so that we get exclusive access to the disk BOOL status = // SetThreadPriority (GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL); SetPriorityClass (GetCurrentProcess (), REALTIME_PRIORITY_CLASS); // SetPriorityClass (GetCurrentProcess (), HIGH_PRIORITY_CLASS);#ifdef PRINTING_TO_CONSOLE_ALLOWED if (0 == status) // printf ("\nERROR: Could not SetThreadPriority, LastError: %d\n", GetLastError ()); printf ("\nERROR: Could not SetPriorityClass, LastError: %d\n", GetLastError ());#endif // 1. Make an output buffer for the VxD rt_IdeDInfo info; pOutBufVxD = &info; // ***************** // KLUDGE WARNING!!! // HAVE to zero out the buffer space for the IDE information! // If this is NOT done then garbage could be in the memory // locations indicating if a disk exists or not. ZeroMemory (&info, sizeof(info)); // 1. Try to load the VxD
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -