如何获得硬盘号 ( 积分: 100 )

  • 主题发起人 主题发起人 ~~newnman
  • 开始时间 开始时间
N

~~newnman

Unregistered / Unconfirmed
GUEST, unregistred user!
如何获得硬盘号,可以通过什么方法呢?
 
如何获得硬盘号,可以通过什么方法呢?
 
//获得硬盘序列号<br>function GetIdeSerialNumber() : PChar; stdcall;<br>const<br> &nbsp;IDENTIFY_BUFFER_SIZE = 512;<br>type<br> &nbsp;TIDERegs = packed record<br> &nbsp; &nbsp; bFeaturesReg: BYTE; // Used for specifying SMART &quot;commands&quot;.<br> &nbsp; &nbsp; bSectorCountReg: BYTE; // IDE sector count register<br> &nbsp; &nbsp; bSectorNumberReg: BYTE; // IDE sector number register<br> &nbsp; &nbsp; bCylLowReg: BYTE; // IDE low order cylinder value<br> &nbsp; &nbsp; bCylHighReg: BYTE; // IDE high order cylinder value<br> &nbsp; &nbsp; bDriveHeadReg: BYTE; // IDE drive/head register<br> &nbsp; &nbsp; bCommandReg: BYTE; // Actual IDE command.<br> &nbsp; &nbsp; bReserved: BYTE; // reserved for future use. Must be zero.<br> &nbsp;end;<br><br> &nbsp;TSendCmdInParams = packed record<br> &nbsp; &nbsp;// Buffer size in bytes<br> &nbsp; &nbsp;cBufferSize: DWORD;<br> &nbsp; &nbsp;// Structure with drive register values.<br> &nbsp; &nbsp;irDriveRegs: TIDERegs;<br> &nbsp; &nbsp;// Physical drive number to send command to (0,1,2,3).<br> &nbsp; &nbsp;bDriveNumber: BYTE;<br> &nbsp; &nbsp;bReserved: array[0..2] of Byte;<br> &nbsp; &nbsp;dwReserved: array[0..3] of DWORD;<br> &nbsp; &nbsp;bBuffer: array[0..0] of Byte; // Input buffer.<br> &nbsp;end;<br><br> &nbsp;TIdSector = packed record<br> &nbsp; &nbsp;wGenConfig: Word;<br> &nbsp; &nbsp;wNumCyls: Word;<br> &nbsp; &nbsp;wReserved: Word;<br> &nbsp; &nbsp;wNumHeads: Word;<br> &nbsp; &nbsp;wBytesPerTrack: Word;<br> &nbsp; &nbsp;wBytesPerSector: Word;<br> &nbsp; &nbsp;wSectorsPerTrack: Word;<br> &nbsp; &nbsp;wVendorUnique: array[0..2] of Word;<br> &nbsp; &nbsp;sSerialNumber: array[0..19] of CHAR;<br> &nbsp; &nbsp;wBufferType: Word;<br> &nbsp; &nbsp;wBufferSize: Word;<br> &nbsp; &nbsp;wECCSize: Word;<br> &nbsp; &nbsp;sFirmwareRev: array[0..7] of Char;<br> &nbsp; &nbsp;sModelNumber: array[0..39] of Char;<br> &nbsp; &nbsp;wMoreVendorUnique: Word;<br> &nbsp; &nbsp;wDoubleWordIO: Word;<br> &nbsp; &nbsp;wCapabilities: Word;<br> &nbsp; &nbsp;wReserved1: Word;<br> &nbsp; &nbsp;wPIOTiming: Word;<br> &nbsp; &nbsp;wDMATiming: Word;<br> &nbsp; &nbsp;wBS: Word;<br> &nbsp; &nbsp;wNumCurrentCyls: Word;<br> &nbsp; &nbsp;wNumCurrentHeads: Word;<br> &nbsp; &nbsp;wNumCurrentSectorsPerTrack: Word;<br> &nbsp; &nbsp;ulCurrentSectorCapacity: DWORD;<br> &nbsp; &nbsp;wMultSectorStuff: Word;<br> &nbsp; &nbsp;ulTotalAddressableSectors: DWORD;<br> &nbsp; &nbsp;wSingleWordDMA: Word;<br> &nbsp; &nbsp;wMultiWordDMA: Word;<br> &nbsp; &nbsp;bReserved: array[0..127] of BYTE;<br> &nbsp;end;<br><br> &nbsp;PIdSector = ^TIdSector;<br><br> &nbsp;TDriverStatus = packed record<br> &nbsp; &nbsp;// 驱动器返回的错误代码,无错则返回0<br> &nbsp; &nbsp;bDriverError: Byte;<br> &nbsp; &nbsp;// IDE出错寄存器的内容,只有当bDriverError 为 SMART_IDE_ERROR 时有效<br> &nbsp; &nbsp;bIDEStatus: Byte;<br> &nbsp; &nbsp;bReserved: array[0..1] of Byte;<br> &nbsp; &nbsp;dwReserved: array[0..1] of DWORD;<br> &nbsp;end;<br><br> &nbsp;TSendCmdOutParams = packed record<br> &nbsp; &nbsp;// bBuffer的大小<br> &nbsp; &nbsp;cBufferSize: DWORD;<br> &nbsp; &nbsp;// 驱动器状态<br> &nbsp; &nbsp;DriverStatus: TDriverStatus;<br> &nbsp; &nbsp;// 用于保存从驱动器读出的数据的缓冲区,实际长度由cBufferSize决定<br> &nbsp; &nbsp;bBuffer: array[0..0] of BYTE;<br> &nbsp;end;<br>var<br> &nbsp;hDevice : THandle;<br> &nbsp;cbBytesReturned : DWORD;<br> &nbsp;SCIP : TSendCmdInParams;<br> &nbsp;aIdOutCmd : array[0..(SizeOf(TSendCmdOutParams) + IDENTIFY_BUFFER_SIZE - 1) - 1] of Byte;<br> &nbsp;IdOutCmd : TSendCmdOutParams absolute aIdOutCmd;<br><br> &nbsp;procedure ChangeByteOrder(var Data; Size: Integer);<br> &nbsp;var<br> &nbsp; &nbsp;ptr : PChar;<br> &nbsp; &nbsp;i : Integer;<br> &nbsp; &nbsp;c : Char;<br> &nbsp;begin<br> &nbsp; &nbsp;ptr := @Data;<br><br> &nbsp; &nbsp;for I := 0 to (Size shr 1) - 1 do<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;c := ptr^;<br> &nbsp; &nbsp; &nbsp;ptr^ := (ptr + 1)^;<br> &nbsp; &nbsp; &nbsp;(ptr + 1)^ := c;<br> &nbsp; &nbsp; &nbsp;Inc(ptr, 2);<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br>begin<br> &nbsp;Result := ''; // 如果出错则返回空串<br><br> &nbsp;if SysUtils.Win32Platform = VER_PLATFORM_WIN32_NT then &nbsp;// Windows NT, Windows 2000<br> &nbsp;begin<br> &nbsp; &nbsp;// 提示! 改变名称可适用于其它驱动器,如第二个驱动器: '//./PhysicalDrive1/'<br> &nbsp; &nbsp;hDevice := CreateFile('//./PhysicalDrive0', GENERIC_READ or GENERIC_WRITE,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING, 0, 0);<br> &nbsp;end<br> &nbsp;else // Version Windows 95 OSR2, Windows 98<br> &nbsp; &nbsp;hDevice := CreateFile('//./SMARTVSD', 0, 0, nil, CREATE_NEW, 0, 0);<br><br> &nbsp; &nbsp;if hDevice = INVALID_HANDLE_VALUE then Exit;<br><br> &nbsp; &nbsp;try<br> &nbsp; &nbsp; &nbsp;FillChar(SCIP, SizeOf(TSendCmdInParams) - 1, #0);<br> &nbsp; &nbsp; &nbsp;FillChar(aIdOutCmd, SizeOf(aIdOutCmd), #0);<br> &nbsp; &nbsp; &nbsp;cbBytesReturned := 0;<br><br> &nbsp; &nbsp; &nbsp;// Set up data structures for IDENTIFY command.<br> &nbsp; &nbsp; &nbsp;with SCIP do<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;cBufferSize := IDENTIFY_BUFFER_SIZE;<br><br> &nbsp; &nbsp; &nbsp; &nbsp;// bDriveNumber := 0;<br> &nbsp; &nbsp; &nbsp; &nbsp;with irDriveRegs do<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;bSectorCountReg := 1;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;bSectorNumberReg := 1;<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// if Win32Platform=VER_PLATFORM_WIN32_NT then bDriveHeadReg := $A0<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// else bDriveHeadReg := $A0 or ((bDriveNum and 1) shl 4);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;bDriveHeadReg := $A0;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;bCommandReg := $EC;<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;end;<br><br> &nbsp; &nbsp; &nbsp;if not DeviceIoControl(hDevice, $0007C088, @SCIP, SizeOf(TSendCmdInParams) - 1,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@aIdOutCmd, SizeOf(aIdOutCmd), cbBytesReturned, nil) then Exit;<br> &nbsp; &nbsp;finally<br> &nbsp; &nbsp; &nbsp;CloseHandle(hDevice);<br> &nbsp; &nbsp;end;<br><br> &nbsp; &nbsp;with PIdSector(@IdOutCmd.bBuffer)^ do<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;ChangeByteOrder(sSerialNumber, SizeOf(sSerialNumber));<br> &nbsp; &nbsp; &nbsp;(Pchar(@sSerialNumber) + SizeOf(sSerialNumber))^ := #0;<br><br> &nbsp; &nbsp; &nbsp;Result := Pchar(@sSerialNumber);<br><br> &nbsp; &nbsp; &nbsp;end;<br>end;
 
搜索一下,有很多贴子,我以前也问过的。
 
function GetHardDiskSerial(const DriveLetter: Char): string; <br>var <br> &nbsp;NotUsed: &nbsp; &nbsp; DWORD; <br> &nbsp;VolumeFlags: DWORD; <br> &nbsp;VolumeInfo: &nbsp;array[0..MAX_PATH] of Char; <br> &nbsp;VolumeSerialNumber: DWORD; <br>begin <br> &nbsp;GetVolumeInformation(PChar(DriveLetter + ':/'), <br> &nbsp; &nbsp;nil, SizeOf(VolumeInfo), @VolumeSerialNumber, NotUsed, <br> &nbsp; &nbsp;VolumeFlags, nil, 0); <br> &nbsp;Result := Format('Label = %s &nbsp; VolSer = %8.8X', <br> &nbsp; &nbsp;[VolumeInfo, VolumeSerialNumber]) <br>end; <br><br><br>procedure TForm1.Button1Click(Sender: TObject); <br>begin <br> &nbsp;ShowMessage(GetHardDiskSerial('c')); <br>end;
 

Similar threads

后退
顶部