请帮我看看是否这样取得硬盘ID(100分)

  • 主题发起人 主题发起人 jony123
  • 开始时间 开始时间
J

jony123

Unregistered / Unconfirmed
GUEST, unregistred user!
function GetHDSerialNumber(Drv : ShortString): LongInt;<br>{$IFDEF WIN32}<br>var<br>&nbsp; pdw : pDWord;<br>&nbsp; mc, fl : dword;<br>{$ENDIF}<br>begin<br>&nbsp; {$IfDef WIN32}<br>&nbsp; New(pdw);<br>&nbsp; GetVolumeInformation(nil,nil,0,pdw,mc,fl,nil,0);<br>&nbsp; Result := pdw^;<br>&nbsp; dispose(pdw);<br>&nbsp; {$ELSE}<br>&nbsp; Result := GetWinFlags;<br>&nbsp; {$ENDIF}<br>end; &nbsp; &nbsp; &nbsp;// &nbsp; (检测硬盘id)<br>问题出在,这段程序在C盘和D盘运行时得到ID都不一样,好像得出来的不是真正的硬盘ID<br>不知如何才能得出硬盘唯一的ID<br>
 
物理序列号才是唯一的!<br><br>附代码一段:<br>得到硬盘物理序号 &nbsp; &nbsp; &nbsp;<br>unit hdid;<br>interface<br>uses<br>&nbsp; windows, controls,sysutils,forms;<br>&nbsp; //, graphics, dialogs, classes, messages,stdctrls;<br>type<br>&nbsp; tsrbiocontrol = packed record<br>&nbsp; &nbsp; headerlength : ulong;<br>&nbsp; &nbsp; signature &nbsp; &nbsp;: array[0..7] of char;<br>&nbsp; &nbsp; timeout &nbsp; &nbsp; &nbsp;: ulong;<br>&nbsp; &nbsp; controlcode &nbsp;: ulong;<br>&nbsp; &nbsp; returncode &nbsp; : ulong;<br>&nbsp; &nbsp; length &nbsp; &nbsp; &nbsp; : ulong;<br>&nbsp; end;<br>&nbsp; srb_io_control = tsrbiocontrol;<br>&nbsp; psrbiocontrol = ^tsrbiocontrol;<br><br>&nbsp; tideregs = packed record<br>&nbsp; &nbsp; bfeaturesreg &nbsp; &nbsp; : byte; // used for specifying smart "commands".<br>&nbsp; &nbsp; bsectorcountreg &nbsp;: byte; // ide sector count register<br>&nbsp; &nbsp; bsectornumberreg : byte; // ide sector number register<br>&nbsp; &nbsp; bcyllowreg &nbsp; &nbsp; &nbsp; : byte; // ide low order cylinder value<br>&nbsp; &nbsp; bcylhighreg &nbsp; &nbsp; &nbsp;: byte; // ide high order cylinder value<br>&nbsp; &nbsp; bdriveheadreg &nbsp; &nbsp;: byte; // ide drive/head register<br>&nbsp; &nbsp; bcommandreg &nbsp; &nbsp; &nbsp;: byte; // actual ide command.<br>&nbsp; &nbsp; breserved &nbsp; &nbsp; &nbsp; &nbsp;: byte; // reserved. &nbsp;must be zero.<br>&nbsp; end;<br>&nbsp; ideregs &nbsp; = tideregs;<br>&nbsp; pideregs &nbsp;= ^tideregs;<br><br>&nbsp; tsendcmdinparams = packed record<br>&nbsp; &nbsp; cbuffersize &nbsp;: dword;<br>&nbsp; &nbsp; irdriveregs &nbsp;: tideregs;<br>&nbsp; &nbsp; bdrivenumber : byte;<br>&nbsp; &nbsp; breserved &nbsp; &nbsp;: array[0..2] of byte;<br>&nbsp; &nbsp; dwreserved &nbsp; : array[0..3] of dword;<br>&nbsp; &nbsp; bbuffer &nbsp; &nbsp; &nbsp;: array[0..0] of byte;<br>&nbsp; end;<br>&nbsp; sendcmdinparams &nbsp; = tsendcmdinparams;<br>&nbsp; psendcmdinparams &nbsp;= ^tsendcmdinparams;<br><br>&nbsp; tidsector = packed record<br>&nbsp; &nbsp; wgenconfig &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : word;<br>&nbsp; &nbsp; wnumcyls &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : word;<br>&nbsp; &nbsp; wreserved &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: word;<br>&nbsp; &nbsp; wnumheads &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: word;<br>&nbsp; &nbsp; wbytespertrack &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : word;<br>&nbsp; &nbsp; wbytespersector &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: word;<br>&nbsp; &nbsp; wsectorspertrack &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : word;<br>&nbsp; &nbsp; wvendorunique &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: array[0..2] of word;<br>&nbsp; &nbsp; sserialnumber &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: array[0..19] of char;<br>&nbsp; &nbsp; wbuffertype &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: word;<br>&nbsp; &nbsp; wbuffersize &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: word;<br>&nbsp; &nbsp; weccsize &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : word;<br>&nbsp; &nbsp; sfirmwarerev &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : array[0..7] of char;<br>&nbsp; &nbsp; smodelnumber &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : array[0..39] of char;<br>&nbsp; &nbsp; wmorevendorunique &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: word;<br>&nbsp; &nbsp; wdoublewordio &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: word;<br>&nbsp; &nbsp; wcapabilities &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: word;<br>&nbsp; &nbsp; wreserved1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : word;<br>&nbsp; &nbsp; wpiotiming &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : word;<br>&nbsp; &nbsp; wdmatiming &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : word;<br>&nbsp; &nbsp; wbs &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: word;<br>&nbsp; &nbsp; wnumcurrentcyls &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: word;<br>&nbsp; &nbsp; wnumcurrentheads &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : word;<br>&nbsp; &nbsp; wnumcurrentsectorspertrack : word;<br>&nbsp; &nbsp; ulcurrentsectorcapacity &nbsp; &nbsp;: ulong;<br>&nbsp; &nbsp; wmultsectorstuff &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : word;<br>&nbsp; &nbsp; ultotaladdressablesectors &nbsp;: ulong;<br>&nbsp; &nbsp; wsingleworddma &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : word;<br>&nbsp; &nbsp; wmultiworddma &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: word;<br>&nbsp; &nbsp; breserved &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: array[0..127] of byte;<br>&nbsp; end;<br>&nbsp; pidsector = ^tidsector;<br><br>const<br>&nbsp; ide_id_function = $ec;<br>&nbsp; identify_buffer_size &nbsp; &nbsp; &nbsp; = 512;<br>&nbsp; dfp_receive_drive_data &nbsp; &nbsp; &nbsp; &nbsp;= $0007c088;<br>&nbsp; ioctl_scsi_miniport &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = $0004d008;<br>&nbsp; ioctl_scsi_miniport_identify &nbsp;= $001b0501;<br>&nbsp; datasize = sizeof(tsendcmdinparams)-1+identify_buffer_size;<br>&nbsp; buffersize = sizeof(srb_io_control)+datasize;<br>&nbsp; w9xbuffersize = identify_buffer_size+16;<br>type<br>&nbsp; thdidform = class(tform)<br>&nbsp; private<br>&nbsp; &nbsp; { private declarations }<br>&nbsp; public<br><br>&nbsp; &nbsp; { public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; hdidform: thdidform;<br>&nbsp; function getidediskserialnumber : string;<br><br>implementation<br><br>{$r *.dfm}<br>procedure changebyteorder( var data; size : integer );<br>&nbsp; var ptr : pchar;<br>&nbsp; &nbsp; &nbsp; i : integer;<br>&nbsp; &nbsp; &nbsp; c : char;<br>&nbsp; begin<br>&nbsp; &nbsp; ptr := @data;<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><br>function getidediskserialnumber : string;<br><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>&nbsp; if win32platform=ver_platform_win32_nt then<br>&nbsp; &nbsp; begin // windows nt, windows 2000<br>&nbsp; &nbsp; &nbsp; // get scsi port handle<br>&nbsp; &nbsp; &nbsp; hdevice := createfile( '//./scsi0:',<br>&nbsp; &nbsp; &nbsp; &nbsp; generic_read or generic_write,<br>&nbsp; &nbsp; &nbsp; &nbsp; file_share_read or file_share_write,<br>&nbsp; &nbsp; &nbsp; &nbsp; nil, open_existing, 0, 0 );<br>&nbsp; &nbsp; &nbsp; if hdevice=invalid_handle_value then exit;<br>&nbsp; &nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; &nbsp; srbcontrol.headerlength := sizeof(srb_io_control);<br>&nbsp; &nbsp; &nbsp; &nbsp; system.move('scsidisk',srbcontrol.signature,8);<br>&nbsp; &nbsp; &nbsp; &nbsp; srbcontrol.timeout &nbsp; &nbsp; &nbsp;:= 2;<br>&nbsp; &nbsp; &nbsp; &nbsp; srbcontrol.length &nbsp; &nbsp; &nbsp; := datasize;<br>&nbsp; &nbsp; &nbsp; &nbsp; srbcontrol.controlcode &nbsp;:= ioctl_scsi_miniport_identify;<br>&nbsp; &nbsp; &nbsp; &nbsp; pindata := psendcmdinparams(pchar(@buffer)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+sizeof(srb_io_control));<br>&nbsp; &nbsp; &nbsp; &nbsp; poutdata := pindata;<br>&nbsp; &nbsp; &nbsp; &nbsp; with pindata^ do<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cbuffersize &nbsp;:= identify_buffer_size;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bdrivenumber := 0;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; with irdriveregs do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bfeaturesreg &nbsp; &nbsp; := 0;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bsectorcountreg &nbsp;:= 1;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bsectornumberreg := 1;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bcyllowreg &nbsp; &nbsp; &nbsp; := 0;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bcylhighreg &nbsp; &nbsp; &nbsp;:= 0;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bdriveheadreg &nbsp; &nbsp;:= $a0;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bcommandreg &nbsp; &nbsp; &nbsp;:= ide_id_function;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; if not deviceiocontrol( hdevice, ioctl_scsi_miniport,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @buffer, buffersize, @buffer, buffersize,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cbbytesreturned, nil ) then exit;<br>&nbsp; &nbsp; &nbsp; finally<br>&nbsp; &nbsp; &nbsp; &nbsp; closehandle(hdevice);<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end<br>&nbsp; else<br>&nbsp; &nbsp; begin // windows 95 osr2, windows 98<br>&nbsp; &nbsp; &nbsp; hdevice := createfile( '//./smartvsd', 0, 0, nil,<br>&nbsp; &nbsp; &nbsp; &nbsp; create_new, 0, 0 );<br>&nbsp; &nbsp; &nbsp; if hdevice=invalid_handle_value then exit;<br>&nbsp; &nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; &nbsp; pindata := psendcmdinparams(@buffer);<br>&nbsp; &nbsp; &nbsp; &nbsp; poutdata := @pindata^.bbuffer;<br>&nbsp; &nbsp; &nbsp; &nbsp; with pindata^ do<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cbuffersize &nbsp;:= identify_buffer_size;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bdrivenumber := 0;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; with irdriveregs do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bfeaturesreg &nbsp; &nbsp; := 0;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bsectorcountreg &nbsp;:= 1;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bsectornumberreg := 1;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bcyllowreg &nbsp; &nbsp; &nbsp; := 0;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bcylhighreg &nbsp; &nbsp; &nbsp;:= 0;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bdriveheadreg &nbsp; &nbsp;:= $a0;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bcommandreg &nbsp; &nbsp; &nbsp;:= ide_id_function;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; if not deviceiocontrol( hdevice, dfp_receive_drive_data,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pindata, sizeof(tsendcmdinparams)-1, poutdata,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; w9xbuffersize, cbbytesreturned, nil ) then exit;<br>&nbsp; &nbsp; &nbsp; finally<br>&nbsp; &nbsp; &nbsp; &nbsp; closehandle(hdevice);<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end;<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>end;<br><br>end.<br><br>//win98要 c:/windows/system/的smartvsd.vxd拷贝到 c:/windows/system/iosubsys 然后重启生效 &nbsp;<br>//2000 and nt do not need<br>得到硬盘物理序号:<br>hdsn:=trim(getidediskserialnumber);
 
接受答案了.
 
请教:<br>&nbsp; &nbsp; 为什么我在win2000下不能用上述函数取得ID,返回一空值,在98下却不将smartvsd.vxd<br>拷到iosubsys下也可得到正确值
 
后退
顶部