C # read the original code of the hard disk serial number

xiaoxiao2021-03-06  84

Public Class IDE

{

[Structlayout (layoutkind.sequential, charset = charset.ansi)]

INTERNAL STRUCT IDSector

{

Public ushort wget wget;

Public ushort wnumcyls;

Public ushort wreserved;

Public ushort wnumheads;

Public ushort wbytespertrack;

Public ushort wbytespector;

Public Ushort WSectorspertrack;

[Marshalas (UnmanagedType.ByValarray, SizeConst = 3)]

Public ushort [] wvendorunique;

[Marshalas (UnmanagedType.Byvaltstr, SizeConst = 20)]

Public String Sserialnumber;

Public ushort wbuffertype;

Public ushort wbuffers;

Public ushort weccsize;

[Marshalas (unmanagedtype.byvaltstr, sizeconst = 8)]

Public String Sfirmwarerev;

[Marshalas (unmanagedtype.byvaltstr, sizeconst = 40)]]]

Public String Smodelnumber;

Public Ushort WMOREVENDORUNIQUE;

Public Ushort WDOUBLORDIO;

Public ushort wcapabilities;

Public Ushort Wreserved1;

Public ushort wpiotiming;

Public ushort wdmatiming;

Public Ushort WBS;

Public ushort wnumcurrentcyls;

Public ushort wnumcurrenthead;

Public ushort wnumcurrentsectorspertrack;

Public uint ulcurrentsectorcapacity;

Public Ushort WmultsectorStuff;

Public uint UltotaralddressAplansectors;

Public Ushort WSINGLEWORDDMA;

Public ushort wmultiworddma;

[Marshalas (unmanagedtype.byvalarray, sizeconst = 128)]]

Public Byte [] BRESERVED;

}

[Structlayout (layoutkind.sequential)]

Internal struct driverstatus

{

Public Byte BDriveRerror;

Public byte bidestatus;

[Marshalas (UnmanagedType.ByValarray, SizeConst = 2)]

Public Byte [] BRESERVED;

[Marshalas (UnmanagedType.ByValarray, SizeConst = 2)]

Public uint [] dwreserved;

}

[Structlayout (layoutkind.sequential)]

INTERNAL STRUCT Sendcmdoutparams

{

Public uint cbuffersize;

Public DriversTatus DriversTATUS DRIVERSTAS

[Marshalas (UnmanagedType.ByValarray, SizeConst = 513)]]

Public Byte [] BBuffer;

}

[Structlayout (layoutkind.sequential, charset = charset.ansi)]

INTERNAL STRUCT SRB_IO_CONTROL

{

Public uint headerLength;

[Marshalas (unmanagedtype.byvaltstr, sizeconst = 8)]

Public string signature;

Public uint timeout;

Public uint controlcode;

Public uint returncode;

Public uint length;

}

[Structlayout (layoutkind.sequential)]

INTERNAL STRUCT IdeRegs

{

Public ByTe BfeaturesReg;

Public Byte Bsectorcountreg;

Public byte bsectornumberreg;

Public Byte Bcyllowreg;

Public Byte bcylhighreg;

Public Byte BDriveHeadReg;

Public Byte BCommandreg;

Public Byte Breserved;

}

[Structlayout (layoutkind.sequential)]

INTERNAL STRUCT Sendcmdinparams

{

Public uint cbuffersize;

Public ideregs IrdriveRIVEREGS;

Public Byte BDrivenumber;

[Marshalas (UnmanagedType.ByValarray, SizeConst = 3)]

Public Byte [] BRESERVED;

[Marshalas (unmanagedtype.byvalarray, sizeconst = 4)]

Public uint [] dwreserved;

Public Byte Bbuffer;

}

[Structlayout (layoutkind.sequential)]

Internal struct getversionoutparams

{

Public Byte Bversion;

Public Byte Brevision;

Public Byte Breserved;

Public BYTE BIDEDEVICEMAP;

Public uint fcapabilities;

[Marshalas (unmanagedtype.byvalarray, sizeconst = 4)]

Public uint [] dwreserved; // forfuture us.

}

[DLLIMPORT ("kernel32.dll")]]]]

Private static extern int closeHandle (uint hobject);

[DLLIMPORT ("kernel32.dll")]]]]

Private static extern int DeviceioControl (uint HDevice,

Uint dwiocontrolcode,

Ref sendcmdinparams lpinbuffer,

Int ninbuffersize,

Ref sendcmdoutparams LPOUTBUFFER,

Int noutbuffersize,

Ref uint lpbytesreturned,

INT LPOVERLAPPED;

[DLLIMPORT ("kernel32.dll")] private statino, UINT HDEVICE,

Uint dwiocontrolcode,

Int lpinbuffer,

Int ninbuffersize,

Ref getversionoutparams lpoutbuffer,

Int noutbuffersize,

Ref uint lpbytesreturned,

INT LPOVERLAPPED;

[DLLIMPORT ("kernel32.dll")]]]]

Private static extern uint createfile (String lpfilename,

Uint dwdesiredAccess,

Uint dwsharemode,

Int LPSecurityAttributes,

uint dwcreationdisposition,

Uint dwflagsandattributes,

int hTemplateFile;

Private const uint generic_read = 0x80000000;

Private const uint generic_write = 0x40000000;

Private const uint file_share_read = 0x00000001;

Private const uint file_share_write = 0x00000002;

PRIVATE CONST uint open_existing = 3;

Private const uint invalid_handle_value = 0xffffffff;

PRIVATE CONST UINT DFP_GET_VERSION = 0x00074080;

Private const Ide_atapi_identify = 0xa1; // returns id sector for atapi.

Private const Id IDE_ATA_IDENTIFY = 0xec; // Returns ID Sector for ATA.

PRIVATE const Int IdIndify_buffer_size = 512;

Private const uint dfp_receive_drive_data = 0x0007c088;

Public Static String Read (Byte Drive)

{

Operatingsystem OS = Environment.osversion;

IF (Os.Platform! = PlatformID.WIN32NT) throw new NotSupportedException ("WindowsNT / 2000 / XP");

/ / I don't have NT4, please don't test the NT4 can not use it.

// if (Os.Version.major <5) Throw new NotSupportedException ("WindowsNT / 2000 / XP");

String drivename = ".//physicaldrive" drive.tostring ();

Uint device = createfile (Drivename,

Generic_read | generic_write,

File_share_read | file_share_write,

0, Open_EXISTING, 0, 0);

IF (device == invalid_handle_value) Return ""

GetversionOutparams Verpara = new getversionoutparams (); uint bytrv = 0;

IF (0! = DeviceioControl (Device, DFP_GET_VERSION,

0, 0, REF VERPARA, MARSHAL.SIZEOF (Verpara),

REF BYTRV, 0)))

{

IF (Verpara.BidedEviceMap> 0)

{

BYTE BIDCMD = (Byte) ((Verpara.BidedEviceMap >> Drive & 0x10)! = 0)? IDE_ATAPI_IDENTIFY: IDE_ATA_IDENTIFY);

Sendcmdinparams scip = new sendcmdinparams ();

Sendcmdoutparams scop = new sendcmdoutparams ();

Scip.cbuffersize = Identify_buffer_size;

Scip.irdriveRegs.BfeaturesReg = 0;

Scip.irdriveRegs.bsectorCountReg = 1;

Scip.irdriveRegs.bcyllowReg = 0;

Scip.irdriveRegs.bcylhighReg = 0;

Scip.irdriveRegs.bdriveHeadReg = (Byte) (0xA0 | ((Drive & 1) << 4));

Scip.irdriveRegs.bcommandreg = BIDCMD;

Scip.BDriveNumber = drive;

IF (0! = DeviceioControl (Device, DFP_RECEIVE_DRIVE_DATA,

Ref scip, marshal.sizeof (scip), Ref scop,

Marshal.sizeof (Scop), Ref Bytrv, 0)))

{

Stringbuilder s = new stringbuilder ();

For (INT i = 20; i <40; i = 2)

{

S.Append ((CHAR) (Scop.BBuffer [i 1]));

S.Append (CHAR) Scop.Bbuffer [I]);

}

CloseHandle (Device);

Return S.toString (). Trim ();

}

}

}

CloseHandle (Device);

""; "

}

}

转载请注明原文地址:https://www.9cbs.com/read-107627.html

New Post(0)