The hard disk Serial Number (Serial Number) is not equal to the Volume Name. Although the latter is easy to obtain, it will be overwritten after formatting the partition, which is unreliable. Unfortunately, many friends often cannot tell this point.
To obtain the physical serial number of the hard disk, You Can Use WMI, that is, Win32_PhysicalMedia.SerialNumber. Unfortunately, WMI of Windows 98/ME does not support this class, and an exception occurs during access.
Inspired by Lu Lin's example, we can also use S. m. a. r. t. interface. You can call the DeviceIoControl () API in RING3 to obtain the hard disk information without writing VXD or DRIVER. In this way, this problem is solved. I have encapsulated it and used a lot of P/Invoke technology, a complete Library. Windows 98-2003 is supported.
It is easy to use:
HardDiskInfo hdd = AtapiDevice. GetHddInfo (0); // the first hard disk
Console. WriteLine ("Module Number: {0}", hdd. ModuleNumber );
Console. WriteLine ("Serial Number: {0}", hdd. SerialNumber );
Console. WriteLine ("Firmware: {0}", hdd. Firmware );
Console. WriteLine ("Capacity: {0} M", hdd. Capacity );
The following is all the code:
Using System;
Using System. Runtime. InteropServices;
Using System. Text;
Namespace Sunmast. Hardware
{
[Serializable]
Public struct HardDiskInfo
{
/// <Summary>
/// Model
/// </Summary>
Public string ModuleNumber;
/// <Summary>
/// Firmware version
/// </Summary>
Public string Firmware;
/// <Summary>
/// Serial number
/// </Summary>
Public string SerialNumber;
/// <Summary>
/// Capacity, in MB
/// </Summary>
Public uint Capacity;
}
# Region Internal Structs
[StructLayout (LayoutKind. Sequential, Pack = 1)]
Internal struct GetVersionOutParams
{
Public byte bVersion;
Public byte bRevision;
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 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;
[Financialas (UnmanagedType. ByValArray, SizeConst = 3)]
Public byte [] bReserved;
[Financialas (UnmanagedType. ByValArray, SizeConst = 4)]
Public uint [] dwReserved;
Public byte bBuffer;
}
[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 cBufferSize;
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 wBytesPerTrack;
Public ushort wBytesPerSector;
Public ushort wSectorsPerTrack;
[Financialas (UnmanagedType. ByValArray, SizeConst = 3)]
Public 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 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 ulCurrentSectorCapacity;
Public ushort wMultSectorStuff;
Public uint ulTotalAddressableSectors;
Public ushort wSingleWordDMA;
Public ushort wMultiWordDMA;
[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 dwDesiredAccess,
Uint dww.mode,
IntPtr lpSecurityAttributes,
Uint dwCreationDisposition,
Uint 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.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_COMMAND = 0x0007c084;
Const uint DFP_RECEIVE_DRIVE_DATA = 0x0007c088;
Const uint GENERIC_READ = 0x80000000;
Const uint GENERIC_WRITE = 0x40000000;
Const uint file_1__read = 0x00000001;
Const uint file_pai_write = 0x00000002;
Const uint CREATE_NEW = 1;
Const uint OPEN_EXISTING = 3;
# Endregion
# Region GetHddInfo
/// <Summary>
/// Obtain hard disk Information
/// </Summary>
/// <Param name = "driveIndex"> hard disk number </param>
/// <Returns> hard disk Information </returns>
/// <Remarks>
/// Reference lu0 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 SMARTVSD. 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 GetHddInfo9x (byte driveIndex)
{
GetVersionOutParams vers = new GetVersionOutParams ();
SendCmdInParams inParam = new SendCmdInParams ();
SendCmdOutParams outParam = new SendCmdOutParams ();
Uint bytesReturned = 0;
IntPt