Read the physical serial number of the hard disk

zhaozj2021-02-17  48

Read the physical serial number of the hard disk

// get hard disk serial number function GetIdeSerialNumber (): PChar; stdcall; const IDENTIFY_BUFFER_SIZE = 512; type TIDERegs = packed record bFeaturesReg: BYTE; // Used for specifying SMART "commands" bSectorCountReg:. BYTE; // IDE sector count register bSectorNumberReg : BYTE; // IDE sector number register bCylLowReg: BYTE; // IDE low order cylinder value bCylHighReg: BYTE; // IDE high order cylinder value bDriveHeadReg: BYTE; // IDE drive / head register bCommandReg: BYTE; // Actual IDE Command. breserved: Byte; // reserved forfuture. Must be zero.

TSendCmdInParams = packed record // Buffer size in bytes cBufferSize: DWORD; // Structure with drive register values ​​irDriveRegs:. TIDERegs; // Physical drive number to send command to (0,1,2,3) bDriveNumber: BYTE; bReserved. : array [0..2] of byte; dwreserved: array [0..3] of dword; bbuffer: array [0..0] of byte; // input buffer. End;

TIdSector = packed record wGenConfig: Word; wNumCyls: Word; wReserved: Word; wNumHeads: Word; wBytesPerTrack: Word; wBytesPerSector: Word; wSectorsPerTrack: Word; wVendorUnique: array [0..2] of Word; sSerialNumber: array [0. .19] OF CHAR; WORCSIZE: WORD; Sfirmwarerev: Array [0..7] of char; smodelnumber: array [0..39] of char; wmorevendorunique: Word; WDOUBLORDIO: WORD; wCapabilities: Word; wReserved1: Word; wPIOTiming: Word; wDMATiming: Word; wBS: Word; wNumCurrentCyls: Word; wNumCurrentHeads: Word; wNumCurrentSectorsPerTrack: Word; ulCurrentSectorCapacity: DWORD; wMultSectorStuff: Word; ulTotalAddressableSectors: DWORD; wSingleWordDMA: Word; wMultiWordDMA: Word; BRESERVED: Array [0..127] of byte; end; pidsector = ^ TIDSector;

TDRIVERSTATUS = PACKED RECORD / / The error code returned by the drive, returns 0 bdrivererror: Byte; // IDE error register, only when bDriveReRror is smart_ide_error: BYTE; BRESERVED: Array [0..1] of Byte; dwreserved: array [0..1] of dword; end;

Tsendcmdoutparams = Packed RECORD / / BBUFFER size CBuffersize: DWORD; // Drive State DriversTus: TDRIVERSTATUS; / / The actual length is determined by CBuffersize BBuffer: array [0..0] for saving data read from the drive. of BYTE; end; var hDevice: THandle; cbBytesReturned: DWORD; SCIP: TSendCmdInParams; aIdOutCmd: array [0 .. (SizeOf (TSendCmdOutParams) IDENTIFY_BUFFER_SIZE - 1) - 1] of Byte; IdOutCmd: TSendCmdOutParams absolute aIdOutCmd;

Procedure Changebyteorder (var data; size: integer; var ptr: pchar; i: integer; c: char; begin ptr: = @data;

For i: = 0 to (Size SHR 1) - 1 Do Begin C: = Ptr ^; PTR ^: = (PTR 1) ^; (PTR 1) ^: = C; Inc (PTR, 2); END ; Begin results: = ''; // If an error returns an empty string if system_win32platform = ver_platform_win32_nt life // WINDOWS NT, Windows 2000 BEGIN / / Tip! Change name can be applied to other drives, such as the second drive : '//./PhysicalDrive1/' hDevice: = CreateFile ( '//./ PhysicalDrive0', GENERIC_READ or GENERIC_WRITE, FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING, 0, 0); end else // Version Windows 95 OSR2, Windows 98 HDevice: = CreateFile ('//./ SmartVsd', 0, 0, NIL, CREATE_NEW, 0, 0);

if HDevice = INVALID_HANDLE_VALUE THEN EXIT;

Try Fillchar (Scip, Sizeof (Tsendcmdinparams) - 1, # 0); Fillchar (Aidoutcmd, SizeOf (AidoutCMD), # 0); CbbytesReturned: = 0;

// SET UP DATA STRUCTURES for Identify Command. With scip do beg cbuffersize: = Identify_buffer_size;

// bdrivenumber: = 0; with irdriveRegs do begin bsectorcountreg: = 1; bsectorNumberReg: = 1;

// if Win32Platform = VER_PLATFORM_WIN32_NT then bDriveHeadReg: = $ A0 // else bDriveHeadReg: = $ A0 or ((bDriveNum and 1) shl 4); bDriveHeadReg: = $ A0; bCommandReg: = $ EC; end; end;

IF not DeviceioControl (HDevice, $ 0007c088, @scip, sizeof (tsendcmdinparams) - 1, @Aidoutcmd, sizeof (aidoutcmd), cbbytesreturned, nil)....

with PIdSector (@ IdOutCmd.bBuffer) ^ do begin ChangeByteOrder (sSerialNumber, SizeOf (sSerialNumber)); (Pchar (@sSerialNumber) SizeOf (sSerialNumber)) ^: = # 0; Result: = Pchar (@sSerialNumber);

END;

转载请注明原文地址:https://www.9cbs.com/read-28945.html

New Post(0)