收藏 分享(赏)

如何得到硬盘序列号[C#].doc

上传人:kpmy5893 文档编号:8077644 上传时间:2019-06-07 格式:DOC 页数:12 大小:67KB
下载 相关 举报
如何得到硬盘序列号[C#].doc_第1页
第1页 / 共12页
如何得到硬盘序列号[C#].doc_第2页
第2页 / 共12页
如何得到硬盘序列号[C#].doc_第3页
第3页 / 共12页
如何得到硬盘序列号[C#].doc_第4页
第4页 / 共12页
如何得到硬盘序列号[C#].doc_第5页
第5页 / 共12页
点击查看更多>>
资源描述

1、本文章转载 发布,转载请保留版权本文章转载 www.wendaotuiguangyuan.biz 发布,转载请保留版权硬盘序列号(Serial Number)不等于卷标号( Volume Name) ,后者虽然很容易得到,但是格式化分区后就会重写,不可靠。遗憾的是很多朋友往往分不清这一点。要得到硬盘的物理序列号,可以通过 WMI,也就是 Win32_PhysicalMedia.SerialNumber。可惜的是 Windows 98/ME 的 WMI 并不支持这个类,访问时会出现异常。受陆麟的例子的启发,我们还可以通过 S.M.A.R.T.接口,直接从 RING3 调用 API Device

2、IoControl()来获取硬盘信息,而不需要写 VXD 或者 DRIVER。这样这个问题就解决了,我对它进行了封装,大量使用了 P/Invoke 技术,一个完整的 Library。支持 Windows 98-2003。使用上很简单:HardDiskInfo hdd = AtapiDevice.GetHddInfo(0); / 第一个硬盘Console.WriteLine(“Module Number: 0“, hdd.ModuleNumber);Console.WriteLine(“Serial Number: 0“, hdd.SerialNumber);Console.WriteLine(

3、“Firmware: 0“, hdd.Firmware);Console.WriteLine(“Capacity: 0 M“, hdd.Capacity);下面是全部代码:using System;using System.Runtime.InteropServices;using System.Text;namespace Sunmast.HardwareSerializablepublic struct HardDiskInfo/ / 型号/ public string ModuleNumber;/ / 固件版本/ 本文章转载 发布,转载请保留版权本文章转载 发布,转载请保留版权pub

4、lic string Firmware;/ / 序列号/ public string SerialNumber;/ / 容量,以 M 为单位/ public uint Capacity;#region Internal StructsStructLayout(LayoutKind.Sequential, Pack=1)internal struct GetVersionOutParamspublic byte bVersion;public byte bRevision;public byte bReserved;public byte bIDEDeviceMap;public uint fC

5、apabilities;MarshalAs(UnmanagedType.ByValArray, SizeConst=4)public uint dwReserved; / For future use.StructLayout(LayoutKind.Sequential, Pack=1)internal struct IdeRegspublic byte bFeaturesReg;public byte bSectorCountReg;public byte bSectorNumberReg;public byte bCylLowReg;public byte bCylHighReg;publ

6、ic byte bDriveHeadReg;public byte bCommandReg;public byte bReserved;StructLayout(LayoutKind.Sequential, Pack=1)internal struct SendCmdInParams本文章转载 发布,转载请保留版权本文章转载 发布,转载请保留版权public uint cBufferSize;public IdeRegs irDriveRegs;public byte bDriveNumber;MarshalAs(UnmanagedType.ByValArray, SizeConst=3)

7、public byte bReserved;MarshalAs(UnmanagedType.ByValArray, SizeConst=4)public uint dwReserved;public byte bBuffer;StructLayout(LayoutKind.Sequential, Pack=1)internal struct DriverStatuspublic byte bDriverError;public byte bIDEStatus;MarshalAs(UnmanagedType.ByValArray, SizeConst=2)public byte bReserve

8、d;MarshalAs(UnmanagedType.ByValArray, SizeConst=2)public uint dwReserved;StructLayout(LayoutKind.Sequential, Pack=1)internal struct SendCmdOutParamspublic uint cBufferSize;public DriverStatus DriverStatus;public IdSector bBuffer;StructLayout(LayoutKind.Sequential, Pack=1, Size=512)internal struct Id

9、Sectorpublic ushort wGenConfig;public ushort wNumCyls;public ushort wReserved;public ushort wNumHeads;public ushort wBytesPerTrack;public ushort wBytesPerSector;public ushort wSectorsPerTrack;MarshalAs(UnmanagedType.ByValArray, SizeConst=3)本文章转载 发布,转载请保留版权本文章转载 发布,转载请保留版权public ushort wVendorUniqu

10、e;MarshalAs(UnmanagedType.ByValArray, SizeConst=20)public byte sSerialNumber;public ushort wBufferType;public ushort wBufferSize;public ushort wECCSize;MarshalAs(UnmanagedType.ByValArray, SizeConst=8)public byte sFirmwareRev;MarshalAs(UnmanagedType.ByValArray, SizeConst=40)public byte sModelNumber;p

11、ublic ushort wMoreVendorUnique;public ushort wDoubleWordIO;public ushort wCapabilities;public ushort wReserved1;public ushort wPIOTiming;public ushort wDMATiming;public ushort wBS;public ushort wNumCurrentCyls;public ushort wNumCurrentHeads;public ushort wNumCurrentSectorsPerTrack;public uint ulCurr

12、entSectorCapacity;public ushort wMultSectorStuff;public uint ulTotalAddressableSectors;public ushort wSingleWordDMA;public ushort wMultiWordDMA;MarshalAs(UnmanagedType.ByValArray, SizeConst=128)public byte bReserved;#endregion/ / ATAPI 驱动器相关/ public class AtapiDevice#region DllImportDllImport(“kerne

13、l32.dll“, SetLastError=true)本文章转载 发布,转载请保留版权本文章转载 发布,转载请保留版权static extern int CloseHandle(IntPtr hObject);DllImport(“kernel32.dll“, SetLastError=true)static extern IntPtr CreateFile(string lpFileName,uint dwDesiredAccess,uint dwShareMode,IntPtr lpSecurityAttributes,uint dwCreationDisposition,uint

14、dwFlagsAndAttributes,IntPtr hTemplateFile);DllImport(“kernel32.dll“)static extern int DeviceIoControl(IntPtr hDevice,uint dwIoControlCode,IntPtr lpInBuffer,uint nInBufferSize,ref GetVersionOutParams lpOutBuffer,uint nOutBufferSize,ref uint lpBytesReturned,Out IntPtr lpOverlapped);DllImport(“kernel32

15、.dll“)static extern int DeviceIoControl(IntPtr hDevice,uint dwIoControlCode,ref SendCmdInParams lpInBuffer,uint nInBufferSize,ref SendCmdOutParams lpOutBuffer,uint nOutBufferSize,ref uint lpBytesReturned,Out IntPtr lpOverlapped);const uint DFP_GET_VERSION = 0x00074080;const uint DFP_SEND_DRIVE_COMMA

16、ND = 0x0007c084;const uint DFP_RECEIVE_DRIVE_DATA = 0x0007c088;const uint GENERIC_READ = 0x80000000;本文章转载 发布,转载请保留版权本文章转载 发布,转载请保留版权const uint GENERIC_WRITE = 0x40000000;const uint FILE_SHARE_READ = 0x00000001;const uint FILE_SHARE_WRITE = 0x00000002;const uint CREATE_NEW = 1;const uint OPEN_EXIST

17、ING = 3;#endregion#region GetHddInfo/ / 获得硬盘信息/ / 硬盘序号/ 硬盘信息/ / 参考 lu0 的文章:http:/lu0s1.3322.org/App/2k1103.html/ by sunmast for everyone/ thanks lu0 for his great works/ 在 Windows 98/ME 中, S.M.A.R.T 并不缺省安装,请将 SMARTVSD.VXD 拷贝到%SYSTEM%IOSUBSYS 目录下。/ 在 Windows 2000/2003 下,需要 Administrators 组的权限。/ / / A

18、tapiDevice.GetHddInfo()/ 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

19、not supported.“);case PlatformID.WinCE:throw new NotSupportedException(“WinCE is not supported.“);default:throw new NotSupportedException(“Unknown Platform.“);本文章转载 发布,转载请保留版权本文章转载 发布,转载请保留版权#region GetHddInfo9xprivate static HardDiskInfo GetHddInfo9x(byte driveIndex)GetVersionOutParams vers = new

20、 GetVersionOutParams();SendCmdInParams inParam = new SendCmdInParams();SendCmdOutParams outParam = new SendCmdOutParams();uint bytesReturned = 0;IntPtr hDevice = CreateFile(“.Smartvsd“,0,0,IntPtr.Zero,CREATE_NEW,0,IntPtr.Zero);if (hDevice = IntPtr.Zero)throw new Exception(“Open smartvsd.vxd failed.“

21、);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_VERSION“);/ If IDE identify command not supported, failsif (0 = (vers.fCapabilities throw new Exc

22、eption(“Error: IDE identify command not supported.“);本文章转载 发布,转载请保留版权本文章转载 发布,转载请保留版权if (0 != (driveIndex elseinParam.irDriveRegs.bDriveHeadReg = 0xa0;if (0 != (vers.fCapabilities throw new Exception(string.Format(“Drive 0 is a ATAPI device, we dont detect it“,driveIndex + 1);elseinParam.irDriveRe

23、gs.bCommandReg = 0xec;inParam.bDriveNumber = driveIndex;inParam.irDriveRegs.bSectorCountReg = 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(outPar

24、am),ref bytesReturned,IntPtr.Zero)CloseHandle(hDevice);throw new Exception(“DeviceIoControl failed: DFP_RECEIVE_DRIVE_DATA“);CloseHandle(hDevice);return GetHardDiskInfo(outParam.bBuffer);本文章转载 发布,转载请保留版权本文章转载 发布,转载请保留版权#endregion#region GetHddInfoNTprivate static HardDiskInfo GetHddInfoNT(byte dri

25、veIndex)GetVersionOutParams vers = new GetVersionOutParams();SendCmdInParams inParam = new SendCmdInParams();SendCmdOutParams outParam = new SendCmdOutParams();uint bytesReturned = 0;/ We start in NT/Win2000IntPtr hDevice = CreateFile(string.Format(“.PhysicalDrive0“,driveIndex),GENERIC_READ | GENERI

26、C_WRITE,FILE_SHARE_READ | FILE_SHARE_WRITE,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);

27、throw new Exception(string.Format(“Drive 0 may not exists.“,driveIndex + 1);/ If IDE identify command not supported, failsif (0 = (vers.fCapabilities 本文章转载 发布,转载请保留版权本文章转载 发布,转载请保留版权throw new Exception(“Error: IDE identify command not supported.“);/ Identify the IDE drivesif (0 != (driveIndex else

28、inParam.irDriveRegs.bDriveHeadReg=0xa0;if (0 != (vers.fCapabilities throw new Exception(string.Format(“Drive 0 is a ATAPI device, we dont detect it.“,driveIndex + 1);elseinParam.irDriveRegs.bCommandReg = 0xec;inParam.bDriveNumber = driveIndex;inParam.irDriveRegs.bSectorCountReg = 1;inParam.irDriveRe

29、gs.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_D

30、RIVE_DATA“);CloseHandle(hDevice);本文章转载 发布,转载请保留版权本文章转载 发布,转载请保留版权return GetHardDiskInfo(outParam.bBuffer);#endregionprivate static HardDiskInfo GetHardDiskInfo(IdSector phdinfo)HardDiskInfo hddInfo = new HardDiskInfo();ChangeByteOrder(phdinfo.sModelNumber);hddInfo.ModuleNumber = Encoding.ASCII.Get

31、String(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.ulTotalAd

32、dressableSectors / 2 / 1024;return hddInfo;private static void ChangeByteOrder(byte charArray)byte temp;for(int i = 0; i charArray.Length; i += 2)temp = charArrayi;charArrayi = charArrayi+1;charArrayi+1 = temp;#endregion本文章转载 发布,转载请保留版权本文章转载 发布,转载请保留版权注:在 Windows 98/ME 中,S.M.A.R.T 并不缺省安装,请将 SMARTVSD.VXD 拷贝到%SYSTEM%IOSUBSYS 目录下。在 Windows 2000/2003 下,需要 Administrators 组的权限。不要在装有 SCSI 硬盘的机器上尝试了,因为 SCSI 硬盘根本不存在序列号。最终版权归陆麟所有,任何人不得将此代码占为己有。

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报