XE 獲取硬碟序列號

2021-09-07 22:32:28 字數 4854 閱讀 1590

function tdmcommonfun.getideserialnumber: ansistring; 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 for future use. must be zero.

end;

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 ansichar;

wbuffertype: word;

wbuffersize: word;

weccsize: word;

sfirmwarerev: array [0 .. 7] of ansichar;

smodelnumber: array [0 .. 39] of ansichar;

wmorevendorunique: word;

wdoublewordio: 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

// 驅動器返回的錯誤**,無錯則返回0

bdrivererror: byte;

// ide出錯暫存器的內容,只有當bdrivererror 為 smart_ide_error 時有效

bidestatus: byte;

breserved: array [0 .. 1] of byte;

dwreserved: array [0 .. 1] of dword;

end;

tsendcmdoutparams = packed record

// bbuffer的大小

cbuffersize: dword;

// 驅動器狀態

driverstatus: tdriverstatus;

// 用於儲存從驅動器讀出的資料的緩衝區,實際長度由cbuffersize決定

bbuffer: array [0 .. 0] of byte;

end;

varhdevice: 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);

varptr: pansichar;

i: integer;

c: ansichar;

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;

end;

begin

result := ''; // 如果出錯則返回空串

if sysutils.win32platform = ver_platform_win32_nt then

// windows nt, windows 2000

begin

// 提示! 改變名稱可適用於其它驅動器,如第二個驅動器: '\\.\physicaldrive1\'

hdevice := createfile('\\.\physicaldrive0', generic_read or generic_write,

file_share_read or file_share_write, nil, open_existing, 0, 0);

endelse // version windows 95 osr2, windows 98

hdevice := createfile('\\.\smartvsd', 0, 0, nil, create_new, 0, 0);

if hdevice = invalid_handle_value then

exit;

tryfillchar(scip, sizeof(tsendcmdinparams) - 1, #0);

fillchar(aidoutcmd, sizeof(aidoutcmd), #0);

cbbytesreturned := 0;

// set up data structures for identify command.

with scip do

begin

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) then

exit;

finally

closehandle(hdevice);

end;

with pidsector(@idoutcmd.bbuffer)^ do

begin

changebyteorder(sserialnumber, sizeof(sserialnumber));

(pansichar(@sserialnumber) + sizeof(sserialnumber))^ := #0;

result := trim(strpas(pansichar(@sserialnumber)));

end;

end;

XE 獲取硬碟序列號

function tdmcommonfun.getideserialnumber ansistring stdcall const identify buffer size 512 type tideregs packed record bfeaturesreg byte used for spec...

獲取硬碟序列號(VC)

說明 經過上百臺計算機測試,ide和sata硬碟可以獲取序列號 scsi硬碟沒有測試環境,無法測試成功與否。請有條件的朋友幫忙測試下,給個訊息,謝謝。虛擬機器上測試不了,不用測試了。ide測試出來全部是0000等數字。網路流傳的版本不少,下下來測試修改了下。include include inclu...

獲取硬碟物理序列號

獲取硬碟物理序列號 最近才做完了這個獲取 ide 硬碟物理序列號的程式。宣告一下,這個程式是我根據 lynn mcguire 的那個 diskid32 的源 做了些自以為是的改動得到的,只能在 nt 平台下獲得第一塊 ide 硬碟的物理序列號。同時,這個程式用到了不少未公開的 windows 的結構...