DELPHI中讀取硬碟的物理序列號

2021-03-31 08:56:29 字數 4785 閱讀 5692

讀取硬碟的物理序列號

//獲得硬碟序列號

function getideserialnumber() : pchar; stdcall;

const

identify_buffer_size = 512;

type

tideregs = packed record

bfeaturesreg: byte; // used for specifying **art "***mands".

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

b***mandreg: byte; // actual ide ***mand.

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 ***mand 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 char;

wbuffertype: word;

wbuffersize: word;

weccsize: word;

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

**odelnumber: array[0..39] of char;

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 為 **art_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 : pchar;

i : integer;

c : char;

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('//./**artvsd', 0, 0, nil, create_new, 0, 0);

if hdevice = invalid_handle_value then exit;

try

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

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

cbbytesreturned := 0;

// set up data structures for identify ***mand.

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;

b***mandreg := $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));

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

result := pchar(@sserialnumber);

end;

end;

Delphi讀取硬碟的物理序列號

因要用到硬碟的物理序列號,從網上找了很多,最終確定使用了以下的一段程式 function tfrmmain.getideserialnumber pchar stdcall const identify buffer size 512 type tideregs packed record bfea...

Delphi中的字串

delphi中的字串 一 各種字串 字串是object pascal所有資料型別中最有用的型別。許多函式以字串為傳遞引數。由於在delphi中字串的定義和使用有各種方式,包括pascal中典型的字串 string delphi支援的長字串 ansistring 類似於c語言的字元陣列 array o...

用好Delphi中的字串

一 各種字串 字串是object pascal所有資料型別中最有用的型別。許多函式以字串為傳遞引數。由於在delphi中字串的定義和使用有各種方式,包括pascal中典型的字串 string delphi支援的長字串 ansistring 類似於c語言的字元陣列 array of char 指向字元...

用好Delphi中的字串

用好delphi中的字串 一 各種字串 字串是object pascal所有資料型別中最有用的型別。許多函式以字串為傳遞引數。由於在delphi中字串的定義和使用有各種方式,包括pascal中典型的字串 string delphi支援的長字串 ansistring 類似於c語言的字元陣列 array...

用好Delphi中的字串

一 各種字串 字串是object pascal所有資料型別中最有用的型別。許多函式以字串為傳遞引數。由於在delphi中字串的定義和使用有各種方式,包括pascal中典型的字 符串 string delphi支援的長字串 ansistring 類似於c語言的字元陣列 array of char 指向...