Read hard disk serial number ID

Source: Internet
Author: User
Namespace ConsoleApplication {using System; using Microshaoft; // <summary> // Class1 abstract description. /// </Summary> public class Class1 {/// <summary> /// main entry point of the application. /// </Summary> // [STAThread] static void Main (string [] args) {// TODO: Add code here to start the application // Console. writeLine ("Hello World"); string s = AtapiDevice. getHddInfo (0 ). serialNumber; Console. writeLine (s); Console. writeLine (Environment. version. toString () ;}} namespace Microshaoft {using System; using System. runtime. interopServices; using System. text; [Serializable] public struct HardDiskInfo {// <su Mmary> // model /// </summary> public string ModuleNumber; // <summary> // Firmware version /// </summary> public string Firmware; /// <summary> /// serial number /// </summary> public string SerialNumber; /// <summary> /// capacity, in the unit of M /// </summary> public uint Capacity;} # region Internal Structs [StructLayout (LayoutKind. sequential, Pack = 1)] internal struct GetVersionOutParams {public byte bVersion; public byte bRevi Sion; public byte bReserved; public byte bIDEDeviceMap; public uint fCapabilities; [financialas (UnmanagedType. byValArray, SizeConst = 4)] public uint [] dwReserved; // For future use .} [StructLayout (LayoutKind. sequential, Pack = 1)] internal struct IdeRegs {public byte bFeaturesReg; public byte bsectorcounregulatory; public byte bSectorNumberReg; public byte bCylLowReg; public byte bCylHighReg; public Te bDriveHeadReg; public byte bCommandReg; public byte bReserved;} [StructLayout (LayoutKind. sequential, Pack = 1)] internal struct SendCmdInParams {public uint cBufferSize; public IdeRegs irDriveRegs; public byte bDriveNumber; [financialas (UnmanagedType. byValArray, SizeConst = 3)] public byte [] bReserved; [financialas (UnmanagedType. byValArray, SizeConst = 4)] public uint [] dwReserved; public byte bB Uffer;} [StructLayout (LayoutKind. sequential, Pack = 1)] internal struct DriverStatus {public byte bDriverError; public byte bIDEStatus; [financialas (UnmanagedType. byValArray, SizeConst = 2)] public byte [] bReserved; [financialas (UnmanagedType. byValArray, SizeConst = 2)] public uint [] dwReserved;} [StructLayout (LayoutKind. sequential, Pack = 1)] internal struct SendCmdOutParams {public uint cBufferS Ize; public DriverStatus; public IdSector bBuffer;} [StructLayout (LayoutKind. sequential, Pack = 1, Size = 512)] internal struct IdSector {public ushort wGenConfig; public ushort wNumCyls; public ushort wReserved; public ushort wNumHeads; public ushort listener; public ushort wSectorsPerTrack; [financialas (UnmanagedType. byValArray, SizeConst = 3)] p Ublic ushort [] wVendorUnique; [financialas (UnmanagedType. byValArray, SizeConst = 20)] public byte [] sSerialNumber; public ushort wBufferType; public ushort wBufferSize; public ushort wECCSize; [financialas (UnmanagedType. byValArray, SizeConst = 8)] public byte [] sFirmwareRev; [financialas (UnmanagedType. byValArray, SizeConst = 40)] public byte [] sModelNumber; public ushort wMoreVendorUnique; public ushort Listener; public ushort wCapabilities; public ushort listener; public ushort wDMATiming; public ushort wBS; public ushort wNumCurrentCyls; public ushort listener; public uint listener; public ushort wMultSectorStuff; public uint ulTotalAddressableSectors; public ushort wSingleWordDMA; public ushort w MultiWordDMA; [financialas (UnmanagedType. byValArray, sizeconst= 128)] public byte [] bReserved ;} # endregion // <summary> // ATAPI driver // </summary> public class AtapiDevice {# region DllImport [DllImport ("kernel32.dll", SetLastError = true)] static extern int CloseHandle (IntPtr hObject); [DllImport ("kernel32.dll", SetLastError = true)] static extern IntPtr CreateFile (string lpFileName, uint Interval, uint dwShareMode, IntPtr interval, uint dwCreationDisposition, uint interval, IntPtr interval); [DllImport ("interval")] static extern int DeviceIoControl (IntPtr hDevice, uint dwIoControlCode, IntPtr lpInBuffer, uint nInBufferSize, ref GetVersionOutParams lpOutBuffer, uint nOutBufferSize, ref uint lpBytesReturned, [Out] IntPtr lpOverl Apped); [DllImport ("plugin")] static extern int DeviceIoControl (IntPtr hDevice, uint dwIoControlCode, ref SendCmdInParams lpInBuffer, uint nInBufferSize, ref limit lpOutBuffer, uint limit, ref uint limit, [Out] IntPtr lpOverlapped); const uint DFP_GET_VERSION = 0x00074080; const uint DFP_SEND_DRIVE_COMMAND = 0x0007c084; const uint DFP_RECEIVE_DRIVE_DA TA = 0x0007c088; const uint GENERIC_READ = 0x80000000; const uint GENERIC_WRITE = 0x40000000; const uint file_0000_read = 0x00000001; const uint file_0000_write = 0x00000002; const uint CREATE_NEW = 1; const uint OPEN_EXISTING = 3; # endregion # region GetHddInfo // <summary> // obtain the hard disk information /// </summary> /// <param name = "driveIndex"> hard disk number </param> /// <returns> hard disk Information </returns> /// <remarks> /// refer to lu0's article.: Http://lu0s1.3322.org/App/2k1103.html // by sunmast for everyone // thanks lu0 for his great works // in Windows 98/ME, S. m.A. r. T is not installed by default. copy VXD to the % SYSTEM % \ IOSUBSYS directory. /// In Windows 2000/2003, you need the permissions of the Administrators group. /// </Remarks> /// <example> // AtapiDevice. getHddInfo () // </example> public static HardDiskInfo GetHddInfo (byte driveIndex) {switch (Environment. OSVersion. platform) {case PlatformID. win32Windows: return GetHddInfo9x (driveIndex); case PlatformID. win32NT: return GetHddInfoNT (driveIndex); case PlatformID. win32S: throw new NotSupportedException ("Win32s is not supported. "); case PlatformID. winCE: Throw new NotSupportedException ("WinCE is not supported. "); default: throw new NotSupportedException (" Unknown Platform. ") ;}# region GetHddInfo9x private static HardDiskInfo upload (byte driveIndex) {GetVersionOutParams vers = new GetVersionOutParams (); SendCmdInParams inParam = new SendCmdInParams (); sendCmdOutParams outParam = new SendCmdOutParams (); uint bytesReturned = 0; IntPtr hDevi Ce = CreateFile (@"\\. \ Smartvsd ", 0, 0, IntPtr. zero, CREATE_NEW, 0, IntPtr. zero); if (hDevice = IntPtr. zero) {throw new Exception ("Open smartvsd. vxd failed. ");} if (0 = DeviceIoControl (hDevice, DFP_GET_VERSION, IntPtr. zero, 0, ref vers, (uint) Marshal. sizeOf (vers), ref bytesReturned, IntPtr. zero) {CloseHandle (hDevice); throw new Exception ("DeviceIoControl failed: DFP_GET_VE RSION ");} // If IDE identify command not supported, fails if (0 = (vers. fCapabilities & 1) {CloseHandle (hDevice); throw new Exception ("Error: IDE identify command not supported. ");} if (0! = (DriveIndex & 1) {inParam. irDriveRegs. bDriveHeadReg = 0xb0;} else {inParam. irDriveRegs. bDriveHeadReg = 0xa0;} if (0! = (Vers. fCapabilities & (16> driveIndex) {// We don't detect a ATAPI device. closeHandle (hDevice); throw new Exception (string. format ("Drive {0} is a ATAPI device, we don't detect it", driveIndex + 1);} else {inParam. irDriveRegs. bCommandReg = 0xec;} inParam. bDriveNumber = driveIndex; inParam. irDriveRegs. bsectorcounreg = 1; inParam. irDriveRegs. bSectorNumberReg = 1; inParam. cBufferSize = 5 12; if (0 = DeviceIoControl (hDevice, DFP_RECEIVE_DRIVE_DATA, ref inParam, (uint) Marshal. sizeOf (inParam), ref outParam, (uint) Marshal. sizeOf (outParam), ref bytesReturned, IntPtr. zero) {CloseHandle (hDevice); throw new Exception ("DeviceIoControl failed: DFP_RECEIVE_DRIVE_DATA");} CloseHandle (hDevice); return GetHardDiskInfo (outParam. bBuffer) ;}# endregion # region GetHddInfoNT private Static HardDiskInfo GetHddInfoNT (byte driveIndex) {response vers = new response (); SendCmdInParams inParam = new SendCmdInParams (); Response outParam = new response (); uint bytesReturned = 0; // We start in NT/Win2000 IntPtr hDevice = CreateFile (string. format (@"\\. \ PhysicalDrive {0} ", driveIndex), GENERIC_READ | GENERIC_WRITE, file_pai_read | file_pai_wri TE, IntPtr. zero, OPEN_EXISTING, 0, IntPtr. zero); if (hDevice = IntPtr. zero) {throw new Exception ("CreateFile faild. ");} if (0 = DeviceIoControl (hDevice, DFP_GET_VERSION, IntPtr. zero, 0, ref vers, (uint) Marshal. sizeOf (vers), ref bytesReturned, IntPtr. zero) {CloseHandle (hDevice); throw new Exception (string. format ("Drive {0} may not exists. ", driveIndex + 1);} // If IDE identify c Ommand not supported, fails if (0 = (vers. fCapabilities & 1) {CloseHandle (hDevice); throw new Exception ("Error: IDE identify command not supported. ");} // Identify the IDE drives if (0! = (DriveIndex & 1) {inParam. irDriveRegs. bDriveHeadReg = 0xb0;} else {inParam. irDriveRegs. bDriveHeadReg = 0xa0;} if (0! = (Vers. fCapabilities & (16> driveIndex) {// We don't detect a ATAPI device. closeHandle (hDevice); throw new Exception (string. format ("Drive {0} is a ATAPI device, we don't detect it. ", driveIndex + 1);} else {inParam. irDriveRegs. bCommandReg = 0xec;} inParam. bDriveNumber = driveIndex; inParam. irDriveRegs. bsectorcounreg = 1; inParam. irDriveRegs. bSectorNumberReg = 1; inParam. cBufferSize = 512; if (0 = DeviceIoControl (hDevice, DFP_RECEIVE_DRIVE_DATA, ref inParam, (uint) Marshal. sizeOf (inParam), ref outParam, (uint) Marshal. sizeOf (outParam), ref bytesReturned, IntPtr. zero) {CloseHandle (hDevice); throw new Exception ("DeviceIoControl failed: DFP_RECEIVE_DRIVE_DATA");} CloseHandle (hDevice); return GetHardDiskInfo (outParam. bBuffer) ;}# endregion private static HardDiskInfo GetHardDiskInfo (IdSector phdinfo) {HardDiskInfo hddInfo = new HardDiskInfo (); ChangeByteOrder (phdinfo. sModelNumber); hddInfo. moduleNumber = Encoding. ASCII. getString (phdinfo. sModelNumber ). trim (); ChangeByteOrder (phdinfo. sFirmwareRev); hddInfo. firmware = Encoding. ASCII. getString (phdinfo. sFirmwareRev ). trim (); ChangeByteOrder (phdinfo. sSerialNumber); hddInfo. serialNumber = Encoding. ASCII. getString (phdinfo. sSerialNumber ). trim (); hddInfo. capacity = phdinfo. ulTotalAddressableSectors/2/1024; return hddInfo;} private static void ChangeByteOrder (byte [] charArray) {byte temp; for (int I = 0; I <charArray. length; I + = 2) {temp = charArray [I]; charArray [I] = charArray [I + 1]; charArray [I + 1] = temp ;}} # endregion }}

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.