Read the Delphi code snippet of the serial number of the hard disk

Source: Internet
Author: User
// Obtain the serial number of the hard disk:
Function getideserialnumber: pchar; // gets the factory serial number of the hard disk;
Const
Identify_buffer_size = 512;
Type
Tideregs = packed record
Bfeaturesreg: byte;
Bsectorcounregulatory: byte;
Bsectornumberreg: byte;
Bcyllowreg: byte;
Bcylhighreg: byte;
Bdriveheadreg: byte;
Bcommandreg: byte;
Breserved: byte;
End;
Tsendcmdinparams = packed record
Cbuffersize: DWORD;
Irdriveregs: tideregs;
Bdrivenumber: byte;
Breserved: array [0 .. 2] of byte;
Dwreserved: array [0 .. 3] of DWORD;
Bbuffer: array [0 .. 0] of byte;
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;
Smodelnumber: 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
Bdrivererror: byte;
Bidestatus: byte;
Breserved: array [0 .. 1] of byte;
Dwreserved: array [0 .. 1] of DWORD;
End;
Tsendcmdoutparams = packed record
Cbuffersize: DWORD;
Driverstatus: tdriverstatus;
Bbuffer: array [0 .. 0] of byte;
End;
VaR
Hdevice: 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 );
VaR
PTR: 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
Begin // Windows NT, Windows 2000
Hdevice: = createfile ('//./physicaldrive0', generic_read or generic_write, file_1__read or file_1__write, nil, open_existing, 0, 0 );
End
Else // version Windows 95 osr2, Windows 98
Hdevice: = createfile ('//./smartvsd', 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;
With Scip do
Begin
Cbuffersize: = identify_buffer_size;
With irdriveregs do
Begin
Bsectorcounregulatory: = 1;
Bsectornumberreg: = 1;
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 ));
(Pchar (@ sserialnumber) + sizeof (sserialnumber) ^: = #0;
Result: = pchar (@ sserialnumber );
End;
End;
// ----------------------------------- Call
Edit1.text: = strpas (getideserialnumber );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.