delphi 获取硬盘序列号

 
  1. // 更多关于 S.M.A.R.T. ioctl 的信息可查看:
  2. // http://www.microsoft.com/hwdev/download/respec/iocltapi.rtf
  3. // MSDN库中也有一些简单的例子
  4. // Windows Development -> Win32 Device Driver Kit ->
  5. // SAMPLE: SmartApp.exe Accesses SMART stats in IDE drives
  6. // 还可以查看 http://www.mtgroup.ru/~alexk
  7. // IdeInfo.zip - 一个简单的使用了S.M.A.R.T. Ioctl API的Delphi应用程序
  8. // 注意:
  9. // WinNT/Win2000 - 你必须拥有对硬盘的读/写访问权限
  10. // Win98
  11. // SMARTVSD.VXD 必须安装到 /windows/system/iosubsys
  12. // (不要忘记在复制后重新启动系统)
  13. function GetIdeSerialNumber : string;
  14. const
  15.   IDENTIFY_BUFFER_SIZE = 512;
  16. type
  17.   TIDERegs = packed record
  18.     bFeaturesReg : BYTE; // Used for specifying SMART "commands".
  19.     bSectorCountReg : BYTE; // IDE sector count register
  20.     bSectorNumberReg : BYTE; // IDE sector number register
  21.     bCylLowReg : BYTE; // IDE low order cylinder value
  22.     bCylHighReg : BYTE; // IDE high order cylinder value
  23.     bDriveHeadReg : BYTE; // IDE drive/head register
  24.     bCommandReg : BYTE; // Actual IDE command.
  25.     bReserved : BYTE; // reserved for future use. Must be zero.
  26.   end;
  27.   TSendCmdInParams = packed record
  28.     // Buffer size in bytes
  29.     cBufferSize : DWORD;
  30.     // Structure with drive register values.
  31.     irDriveRegs : TIDERegs;
  32.     // Physical drive number to send command to (0,1,2,3).
  33.     bDriveNumber : BYTE;
  34.     bReserved : Array[0..2] of Byte;
  35.     dwReserved : Array[0..3] of DWORD;
  36.     bBuffer : Array[0..0] of Byte; // Input buffer.
  37.   end;
  38.   TIdSector = packed record
  39.     wGenConfig : Word;
  40.     wNumCyls : Word;
  41.     wReserved : Word;
  42.     wNumHeads : Word;
  43.     wBytesPerTrack : Word;
  44.     wBytesPerSector : Word;
  45.     wSectorsPerTrack : Word;
  46.     wVendorUnique : Array[0..2] of Word;
  47.     sSerialNumber : Array[0..19] of CHAR;
  48.     wBufferType : Word;
  49.     wBufferSize : Word;
  50.     wECCSize : Word;
  51.     sFirmwareRev : Array[0..7] of Char;
  52.     sModelNumber : Array[0..39] of Char;
  53.     wMoreVendorUnique : Word;
  54.     wDoubleWordIO : Word;
  55.     wCapabilities : Word;
  56.     wReserved1 : Word;
  57.     wPIOTiming : Word;
  58.     wDMATiming : Word;
  59.     wBS : Word;
  60.     wNumCurrentCyls : Word;
  61.     wNumCurrentHeads : Word;
  62.     wNumCurrentSectorsPerTrack : Word;
  63.     ulCurrentSectorCapacity : DWORD;
  64.     wMultSectorStuff : Word;
  65.     ulTotalAddressableSectors : DWORD;
  66.     wSingleWordDMA : Word;
  67.     wMultiWordDMA : Word;
  68.     bReserved : Array[0..127] of BYTE;
  69.   end;
  70.   PIdSector = ^TIdSector;
  71.   TDriverStatus = packed record
  72.     // 驱动器返回的错误代码,无错则返回0
  73.     bDriverError : Byte;
  74.     // IDE出错寄存器的内容,只有当bDriverError 为 SMART_IDE_ERROR 时有效
  75.     bIDEStatus : Byte;
  76.     bReserved : Array[0..1] of Byte;
  77.     dwReserved : Array[0..1] of DWORD;
  78.   end;
  79.   TSendCmdOutParams = packed record
  80.     // bBuffer的大小
  81.     cBufferSize : DWORD;
  82.     // 驱动器状态
  83.     DriverStatus : TDriverStatus;
  84.     // 用于保存从驱动器读出的数据的缓冲区,实际长度由cBufferSize决定
  85.     bBuffer : Array[0..0] of BYTE;
  86.   end;
  87. var
  88.   hDevice : THandle;
  89.   cbBytesReturned : DWORD;
  90.   ptr : PChar;
  91.   SCIP : TSendCmdInParams;
  92.   aIdOutCmd : Array [0..(SizeOf(TSendCmdOutParams)+IDENTIFY_BUFFER_SIZE-1)-1] of Byte;
  93.   IdOutCmd : TSendCmdOutParams absolute aIdOutCmd;
  94.   procedure ChangeByteOrder( var Data; Size : Integer );
  95.   var ptr : PChar;
  96.   i : Integer;
  97.   c : Char;
  98.   begin
  99.     ptr := @Data;
  100.     for i := 0 to (Size shr 1)-1 do
  101.     begin
  102.       c := ptr^;
  103.       ptr^ := (ptr+1)^;
  104.       (ptr+1)^ := c;
  105.       Inc(ptr,2);
  106.     end;
  107.   end;
  108. begin
  109.   Result := ''// 如果出错则返回空串
  110.   if SysUtils.Win32Platform=VER_PLATFORM_WIN32_NT then // Windows NT, Windows 2000
  111.   begin
  112.   // 提示! 改变名称可适用于其它驱动器,如第二个驱动器: '//./PhysicalDrive1/'
  113.     hDevice := CreateFile( '//./PhysicalDrive0', GENERIC_READ or GENERIC_WRITE,
  114.     FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING, 0, 0 );
  115.   end else // Version Windows 95 OSR2, Windows 98
  116.     hDevice := CreateFile( '//./SMARTVSD', 0, 0, nil, CREATE_NEW, 0, 0 );
  117.   if hDevice=INVALID_HANDLE_VALUE then Exit;
  118.   try
  119.     FillChar(SCIP,SizeOf(TSendCmdInParams)-1,#0);
  120.     FillChar(aIdOutCmd,SizeOf(aIdOutCmd),#0);
  121.     cbBytesReturned := 0;
  122.     // Set up data structures for IDENTIFY command.
  123.     with SCIP do
  124.     begin
  125.       cBufferSize := IDENTIFY_BUFFER_SIZE;
  126.       // bDriveNumber := 0;
  127.       with irDriveRegs do
  128.       begin
  129.         bSectorCountReg := 1;
  130.         bSectorNumberReg := 1;
  131.         // if Win32Platform=VER_PLATFORM_WIN32_NT then bDriveHeadReg := $A0
  132.         // else bDriveHeadReg := $A0 or ((bDriveNum and 1) shl 4);
  133.         bDriveHeadReg := $A0;
  134.         bCommandReg := $EC;
  135.       end;
  136.     end;
  137.     if not DeviceIoControl( hDevice, $0007c088, @SCIP, SizeOf(TSendCmdInParams)-1,
  138.       @aIdOutCmd, SizeOf(aIdOutCmd), cbBytesReturned, nil ) then Exit;
  139.   finally
  140.     CloseHandle(hDevice);
  141.   end;
  142.   with PIdSector(@IdOutCmd.bBuffer)^ do
  143.   begin
  144.     ChangeByteOrder( sSerialNumber, SizeOf(sSerialNumber) );
  145.     (PChar(@sSerialNumber)+SizeOf(sSerialNumber))^ := #0;
  146.     Result := PChar(@sSerialNumber);
  147.   end;
  148. end;
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值