*******如何获取硬盘的ID ? 急用 *******(50分)

G

grj1

Unregistered / Unconfirmed
GUEST, unregistred user!
如何获取硬盘的ID ? &nbsp;我这有源码,但是读不出来。<br><br>原码如下:<br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; StdCtrls, registry;<br><br>type<br>&nbsp; TSrbIoControl = packed record<br>&nbsp; &nbsp; HeaderLength : ULONG;<br>&nbsp; &nbsp; Signature : Array[0..7] of Char;<br>&nbsp; &nbsp; Timeout : ULONG;<br>&nbsp; &nbsp; ControlCode : ULONG;<br>&nbsp; &nbsp; ReturnCode : ULONG;<br>&nbsp; &nbsp; Length : ULONG;<br>&nbsp; end;<br>&nbsp; SRB_IO_CONTROL = TSrbIoControl;<br>&nbsp; PSrbIoControl = ^TSrbIoControl;<br><br>&nbsp; TIDERegs = packed record<br>&nbsp; &nbsp; bFeaturesReg : Byte; // Used for specifying SMART "commands".<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. Must be zero.<br>&nbsp; end;<br>&nbsp; IDEREGS = TIDERegs;<br>&nbsp; PIDERegs = ^TIDERegs;<br><br>&nbsp; TSendCmdInParams = packed record<br>&nbsp; &nbsp; cBufferSize : DWORD;<br>&nbsp; &nbsp; irDriveRegs : TIDERegs;<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;<br>&nbsp; end;<br>&nbsp; SENDCMDINPARAMS = TSendCmdInParams;<br>&nbsp; PSendCmdInParams = ^TSendCmdInParams;<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 : ULONG;<br>&nbsp; &nbsp; wMultSectorStuff : Word;<br>&nbsp; &nbsp; ulTotalAddressableSectors : ULONG;<br>&nbsp; &nbsp; wSingleWordDMA : Word;<br>&nbsp; &nbsp; wMultiWordDMA : Word;<br>&nbsp; &nbsp; bReserved : Array[0..127] of Byte;<br>&nbsp; end;<br>&nbsp; PIdSector = ^TIdSector;<br><br><br>const<br>&nbsp; IDENTIFY_BUFFER_SIZE = 512;<br>&nbsp; DataSize = sizeof(TSendCmdInParams)-1+IDENTIFY_BUFFER_SIZE;<br>&nbsp; IOCTL_SCSI_MINIPORT = $0004d008;<br>&nbsp; IOCTL_SCSI_MINIPORT_IDENTIFY = $001b0501;<br>&nbsp; IDE_ID_FUNCTION = $EC;<br>&nbsp; BufferSize=1280;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Label1: TLabel;<br>&nbsp; &nbsp; procedure FormCreate(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br>&nbsp; serial:string;<br>&nbsp; inputserial:string;<br><br>implementation<br>uses<br>&nbsp; unit2;<br>{$R *.DFM}<br>function encrypt(serial:string):string;<br>var<br>&nbsp; i:dword;<br>&nbsp; len:dword;<br>&nbsp; r:dword;<br>begin<br>&nbsp; r:=0;<br>&nbsp; len:=length(serial);<br>&nbsp; for i:=1 to len do<br>&nbsp; begin<br>&nbsp; &nbsp; r:=r+dword(serial);<br>&nbsp; &nbsp; r:=r*10;<br>&nbsp; end;<br>&nbsp; result:=inttostr(r);<br>end;<br><br>procedure ChangeByteOrder( var Data; Size : Integer );<br>var ptr : PChar;<br>&nbsp; i : Integer;<br>&nbsp; c : Char;<br>begin<br>&nbsp; ptr := @Data;<br>&nbsp; for i := 0 to (Size shr 1)-1 do<br>&nbsp; begin<br>&nbsp; &nbsp; c := ptr^;<br>&nbsp; &nbsp; ptr^ := (ptr+1)^;<br>&nbsp; &nbsp; (ptr+1)^ := c;<br>&nbsp; &nbsp; Inc(ptr,2);<br>&nbsp; end;<br>end;<br><br>function readhdserial:string;<br>var<br>&nbsp; hDevice : THandle;<br>&nbsp; cbBytesReturned : DWORD;<br>&nbsp; pInData : PSendCmdInParams;<br>&nbsp; pOutData : Pointer; // PSendCmdOutParams<br>&nbsp; Buffer : Array[0..BufferSize-1] of Byte;<br>&nbsp; srbControl : TSrbIoControl absolute Buffer;<br>begin<br>&nbsp; result:='';<br>&nbsp; FillChar(Buffer,BufferSize,#0);<br><br>&nbsp; //通过MS的S.M.A.R.T.接口,直接从RING3调用<br>&nbsp; //API DeviceIoControl()来获取硬盘信息<br>&nbsp; // Get SCSI port handle<br>&nbsp; hDevice := CreateFile( '//./Scsi0:',<br>&nbsp; &nbsp; GENERIC_READ or GENERIC_WRITE,<br>&nbsp; &nbsp; FILE_SHARE_READ or FILE_SHARE_WRITE,<br>&nbsp; &nbsp; nil, OPEN_EXISTING, 0, 0 );<br>&nbsp; if hDevice=INVALID_HANDLE_VALUE then Exit;<br>&nbsp; try<br>&nbsp; &nbsp; srbControl.HeaderLength := SizeOf(SRB_IO_CONTROL);<br>&nbsp; &nbsp; System.Move('SCSIDISK',srbControl.Signature,8);<br>&nbsp; &nbsp; srbControl.Timeout := 2;<br>&nbsp; &nbsp; srbControl.Length := DataSize;<br>&nbsp; &nbsp; srbControl.ControlCode := IOCTL_SCSI_MINIPORT_IDENTIFY;<br>&nbsp; &nbsp; pInData := PSendCmdInParams(PChar(@Buffer)<br>&nbsp; &nbsp; +SizeOf(SRB_IO_CONTROL));<br>&nbsp; &nbsp; pOutData := pInData;<br>&nbsp; &nbsp; with pInData^ do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; cBufferSize := IDENTIFY_BUFFER_SIZE;<br>&nbsp; &nbsp; &nbsp; bDriveNumber := 0;<br>&nbsp; &nbsp; &nbsp; with irDriveRegs do<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; bFeaturesReg := 0;<br>&nbsp; &nbsp; &nbsp; &nbsp; bSectorCountReg := 1;<br>&nbsp; &nbsp; &nbsp; &nbsp; bSectorNumberReg := 1;<br>&nbsp; &nbsp; &nbsp; &nbsp; bCylLowReg := 0;<br>&nbsp; &nbsp; &nbsp; &nbsp; bCylHighReg := 0;<br>&nbsp; &nbsp; &nbsp; &nbsp; bDriveHeadReg := $A0;<br>&nbsp; &nbsp; &nbsp; &nbsp; bCommandReg := IDE_ID_FUNCTION;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; if not DeviceIoControl( hDevice, IOCTL_SCSI_MINIPORT,<br>&nbsp; &nbsp; &nbsp; @Buffer, BufferSize, @Buffer, BufferSize,<br>&nbsp; &nbsp; &nbsp; cbBytesReturned, nil ) then<br>&nbsp; &nbsp; &nbsp; Exit;<br>&nbsp; &nbsp; with PIdSector(PChar(pOutData)+16)^ do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; ChangeByteOrder(sSerialNumber,SizeOf(sSerialNumber));<br>&nbsp; &nbsp; &nbsp; SetString(Result,sSerialNumber,SizeOf(sSerialNumber));<br>&nbsp; &nbsp; end;<br>&nbsp; finally<br>&nbsp; &nbsp; CloseHandle(hDevice);<br>&nbsp; end;<br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>var<br>&nbsp; reg:tregistry;<br>&nbsp; s:string;<br>begin<br>&nbsp; serial:=readhdserial();<br>&nbsp; inputserial:=encrypt(serial);<br>&nbsp; reg:=tregistry.create;<br>&nbsp; reg.RootKey:=HKEY_CLASSES_ROOT;<br>&nbsp; if not reg.openKey('/License/'+serial,false) then<br>&nbsp; //打开注册表项,没有该项,则是第一次启动<br>&nbsp; begin<br>&nbsp; &nbsp; Application.CreateForm(TOKBottomDlg, OKBottomDlg);<br>&nbsp; &nbsp; OKBottomDlg.showmodal;<br>&nbsp; &nbsp; OKBottomDlg.Destroy;<br>&nbsp; &nbsp; //输入序列号正确,则<br>&nbsp; &nbsp; //将已安装信息写入注册表<br>&nbsp; &nbsp; reg.createkey('/License/'+serial);<br>&nbsp; &nbsp; reg.openkey('/License/'+serial,false);<br>&nbsp; &nbsp; reg.writestring('installed','true');<br>&nbsp; &nbsp; //将序列号写入注册表<br>&nbsp; &nbsp; reg.Createkey('/License/'+inputserial);<br>&nbsp; &nbsp; reg.OpenKey('/License/'+inputserial,false);<br>&nbsp; &nbsp; reg.WriteString('1',inputserial);<br>&nbsp; &nbsp; reg.closekey;<br>&nbsp; end<br>&nbsp; else<br>&nbsp; begin//是今后的启动<br>&nbsp; &nbsp; reg.CloseKey;<br>&nbsp; &nbsp; //检查序列号<br>&nbsp; &nbsp; reg.openkey('/License/'+inputserial,false);<br>&nbsp; &nbsp; s:=reg.readstring('1');<br>&nbsp; &nbsp; reg.closekey;<br>&nbsp; &nbsp; if inputserial&lt;&gt;s then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; Application.CreateForm(TOKBottomDlg, OKBottomDlg);<br>&nbsp; &nbsp; &nbsp; OKBottomDlg.showmodal;<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>end;<br><br>end.<br><br>通过跟踪发现:下面的两行执行,跳出了程序,不知为何,请高手指点。<br>&nbsp;if not DeviceIoControl( hDevice, IOCTL_SCSI_MINIPORT,<br>&nbsp; &nbsp; &nbsp; @Buffer, BufferSize, @Buffer, BufferSize,<br>&nbsp; &nbsp; &nbsp; cbBytesReturned, nil ) then<br>&nbsp; &nbsp; &nbsp; Exit;<br><br>
 

Similar threads

I
回复
0
查看
578
import
I
I
回复
0
查看
657
import
I
I
回复
0
查看
377
import
I
I
回复
0
查看
553
import
I
顶部