示例#1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="driveIndex"></param>
        /// <returns></returns>
        private static HDiskInfo GetHddInfo9X(byte driveIndex)
        {
            GetVersionOutParams vers     = new 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 UnauthorizedAccessException("打开 smartvsd.vxd 文件失败。");
            }
            if (0 == DeviceIoControl(hDevice, SMART_GET_VERSION,
                                     IntPtr.Zero, 0,
                                     ref vers, (uint)Marshal.SizeOf(vers), ref bytesReturned, IntPtr.Zero))
            {
                CloseHandle(hDevice);
                throw new IOException(string.Format(ResourcesApi.Win32_DeviceIoControlErr, "SMART_GET_VERSION"));
            }
            // 如果 IDE 的鉴定命令不被识别或失败
            if (0 == (vers.fCapabilities & 1))
            {
                CloseHandle(hDevice);
                throw new IOException(ResourcesApi.Win32_DeviceIoControlNotSupport);
            }
            if (0 != (driveIndex & 1))
            {
                inParam.irDriveRegs.bDriveHeadReg = 0xb0;
            }
            else
            {
                inParam.irDriveRegs.bDriveHeadReg = 0xa0;
            }
            if (0 != (vers.fCapabilities & (16 >> driveIndex)))
            {
                // 检测出IDE为ATAPI类型,无法处理
                CloseHandle(hDevice);
                throw new IOException(ResourcesApi.Win32_DeviceIoControlNotSupport);
            }
            else
            {
                inParam.irDriveRegs.bCommandReg = 0xec;
            }
            inParam.bDriveNumber = driveIndex;
            inParam.irDriveRegs.bSectorCountReg  = 1;
            inParam.irDriveRegs.bSectorNumberReg = 1;
            inParam.cBufferSize = BUFFER_SIZE;
            if (0 == DeviceIoControl(hDevice, SMART_RCV_DRIVE_DATA, ref inParam, (uint)Marshal.SizeOf(inParam), ref outParam, (uint)Marshal.SizeOf(outParam), ref bytesReturned, IntPtr.Zero))
            {
                CloseHandle(hDevice);
                throw new IOException(string.Format(ResourcesApi.Win32_DeviceIoControlErr, "SMART_RCV_DRIVE_DATA"));
            }
            // 关闭文件句柄
            CloseHandle(hDevice);
            ChangeByteOrder(outParam.bBuffer.sModelNumber);
            ChangeByteOrder(outParam.bBuffer.sSerialNumber);
            ChangeByteOrder(outParam.bBuffer.sFirmwareRev);
            return(GetHardDiskInfo(outParam.bBuffer));
        }
 static extern int DeviceIoControl(
     IntPtr hDevice,
     uint dwIoControlCode,
     IntPtr lpInBuffer,
     uint nInBufferSize,
     ref GetVersionOutParams lpOutBuffer,
     uint nOutBufferSize,
     ref uint lpBytesReturned,
     [Out] IntPtr lpOverlapped);
示例#3
0
        private static unsafe string GetHardDiskSerialNumber()
        {
            byte num = 0;
            GetVersionOutParams structure = new GetVersionOutParams();
            SendCmdInParams     params2   = new SendCmdInParams();
            SendCmdOutParams    params3   = new SendCmdOutParams();
            uint   lpBytesReturned        = 0;
            IntPtr hDevice = CreateFile($"\\.\PhysicalDrive{num}", 0xc0000000, 3, IntPtr.Zero, 3, 0, IntPtr.Zero);

            if (hDevice == IntPtr.Zero)
            {
                throw new Exception("CreateFile faild.");
            }
            GetVersionOutParams *paramsPtr1 = &structure;

            if (DeviceIoControl(hDevice, 0x74080, IntPtr.Zero, 0, ref (GetVersionOutParams) ref paramsPtr1, (uint)Marshal.SizeOf <GetVersionOutParams>(structure), ref lpBytesReturned, IntPtr.Zero) == 0)
            {
                CloseHandle(hDevice);
                throw new Exception($"Drive {num + 1} may not exists.");
            }
            if ((structure.fCapabilities & 1) == 0)
            {
                CloseHandle(hDevice);
                throw new Exception("Error: IDE identify command not supported.");
            }
            params2.irDriveRegs.bDriveHeadReg = ((num & 1) == 0) ? 160 : 0xb0;
            if ((structure.fCapabilities & (0x10 >> (num & 0x1f))) != 0)
            {
                CloseHandle(hDevice);
                throw new Exception($"Drive {num + 1} is a ATAPI device, we don''t detect it.");
            }
            params2.irDriveRegs.bCommandReg      = 0xec;
            params2.bDriveNumber                 = num;
            params2.irDriveRegs.bSectorCountReg  = 1;
            params2.irDriveRegs.bSectorNumberReg = 1;
            params2.cBufferSize = 0x200;
            SendCmdInParams * paramsPtr2 = &params2;
            SendCmdOutParams *paramsPtr3 = &params3;

            if (DeviceIoControl(hDevice, 0x7c088, ref (SendCmdInParams) ref paramsPtr2, (uint)Marshal.SizeOf <SendCmdInParams>(params2), ref (SendCmdOutParams) ref paramsPtr3, (uint)Marshal.SizeOf <SendCmdOutParams>(params3), ref lpBytesReturned, IntPtr.Zero) == 0)
            {
                CloseHandle(hDevice);
                throw new Exception("DeviceIoControl failed: DFP_RECEIVE_DRIVE_DATA");
            }
            CloseHandle(hDevice);
            IdSector bBuffer = params3.bBuffer;

            ChangeByteOrder(bBuffer.sSerialNumber);
            return(Encoding.ASCII.GetString(bBuffer.sSerialNumber).Trim());
        }
示例#4
0
        /// <summary>
        /// ��ȡ9X�ܹ���Ӳ����Ϣ
        /// </summary>
        /// <param name="driveIndex"></param>
        /// <returns></returns>
        private static HardDiskInfo GetHddInfo9x(byte driveIndex)
        {
            GetVersionOutParams vers = new 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.");
            }
            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, 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.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(outParam),
             ref bytesReturned,
             IntPtr.Zero))
            {
                CloseHandle(hDevice);
                throw new Exception("DeviceIoControl failed: DFP_RECEIVE_DRIVE_DATA");
            }
            CloseHandle(hDevice);

            return GetHardDiskInfo(outParam.bBuffer);
        }
示例#5
0
 static extern int DeviceIoControl(
  IntPtr hDevice,
  uint dwIoControlCode,
  IntPtr lpInBuffer,
  uint nInBufferSize,
  ref GetVersionOutParams lpOutBuffer,
  uint nOutBufferSize,
  ref uint lpBytesReturned,
  [Out] IntPtr lpOverlapped);
        /// <summary>
        /// 获取NT架构的硬盘信息
        /// </summary>
        /// <param name="driveIndex"></param>
        /// <returns></returns>
        private static HardDiskInfo GetHddInfoNT(byte driveIndex)
        {
            GetVersionOutParams vers     = new GetVersionOutParams();
            SendCmdInParams     inParam  = new SendCmdInParams();
            SendCmdOutParams    outParam = new SendCmdOutParams();
            uint bytesReturned           = 0;


            // We start in NT/Win2000
            IntPtr hDevice = CreateFile(
                string.Format(@"\\.\PhysicalDrive{0}", driveIndex),
                GENERIC_READ | GENERIC_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);
                throw new Exception(string.Format("Drive {0} may not exists.", driveIndex + 1));
            }
            // If IDE identify command 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)))
            {
                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.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(outParam),
                    ref bytesReturned,
                    IntPtr.Zero))
            {
                CloseHandle(hDevice);
                throw new Exception("DeviceIoControl failed: DFP_RECEIVE_DRIVE_DATA");
            }
            CloseHandle(hDevice);


            return(GetHardDiskInfo(outParam.bBuffer));
        }
        /// <summary>
        /// 获取9X架构的硬盘信息
        /// </summary>
        /// <param name="driveIndex"></param>
        /// <returns></returns>
        private static HardDiskInfo GetHddInfo9x(byte driveIndex)
        {
            GetVersionOutParams vers     = new 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.");
            }
            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, 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.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(outParam),
                    ref bytesReturned,
                    IntPtr.Zero))
            {
                CloseHandle(hDevice);
                throw new Exception("DeviceIoControl failed: DFP_RECEIVE_DRIVE_DATA");
            }
            CloseHandle(hDevice);


            return(GetHardDiskInfo(outParam.bBuffer));
        }
        private static HardDiskInfo GetHddInfoNT(byte driveIndex)
        {
            GetVersionOutParams vers     = new GetVersionOutParams();
            SendCmdInParams     inParam  = new SendCmdInParams();
            SendCmdOutParams    outParam = new SendCmdOutParams();
            uint bytesReturned           = 0;

            // We start in NT/Win2000
            // open a handle to PhysicalDriveN instead
            IntPtr hDevice = CreateFile(@"\\.\PhysicalDrive0", FileAccess.ReadWrite, FileShare.ReadWrite, IntPtr.Zero, FileMode.Open, System.IO.FileAttributes.Normal, 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 command 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.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(outParam),
                    ref bytesReturned,
                    IntPtr.Zero))
            {
                CloseHandle(hDevice);
                throw new Exception("DeviceIoControl failed: DFP_RECEIVE_DRIVE_DATA");
            }
            CloseHandle(hDevice);

            return(GetHardDiskInfo(outParam.bBuffer));
        }
示例#9
0
        /// <summary>
        /// 获取在NT平台下指定序列号的硬盘信息。
        /// </summary>
        /// <param name="driveIndex">物理磁盘的数量。</param>
        /// <returns></returns>
        private static HDiskInfo GetHddInfoNT(byte driveIndex)
        {
            GetVersionOutParams vers     = new GetVersionOutParams();
            SendCmdInParams     inParam  = new SendCmdInParams();
            SendCmdOutParams    outParam = new SendCmdOutParams();
            uint bytesReturned           = 0;

            // 使用 Win2000 或 Xp下的方法获取硬件信息

            // 获取设备的句柄。
            IntPtr hDevice =
                CreateFile(string.Format(@"\\.\PhysicalDrive{0}", driveIndex), GENERIC_READ | GENERIC_WRITE,
                           FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);

            // 开始检查
            if (hDevice == IntPtr.Zero)
            {
                throw new UnauthorizedAccessException("执行 Win32 API 函数 CreateFile 失败。");
            }
            if (0 == DeviceIoControl(hDevice, SMART_GET_VERSION, IntPtr.Zero, 0, ref vers,
                                     (uint)Marshal.SizeOf(vers),
                                     ref bytesReturned,
                                     IntPtr.Zero))
            {
                CloseHandle(hDevice);
                throw new IOException(string.Format(ResourcesApi.Win32_DeviceIoControlErr, "SMART_GET_VERSION"));
            }
            // 检测IDE控制命令是否支持
            if (0 == (vers.fCapabilities & 1))
            {
                CloseHandle(hDevice);
                throw new IOException(ResourcesApi.Win32_DeviceIoControlNotSupport);
            }
            // 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 IOException(ResourcesApi.Win32_DeviceIoControlNotSupport);
            }
            else
            {
                inParam.irDriveRegs.bCommandReg = 0xec;
            }
            inParam.bDriveNumber = driveIndex;
            inParam.irDriveRegs.bSectorCountReg  = 1;
            inParam.irDriveRegs.bSectorNumberReg = 1;
            inParam.cBufferSize = 512;

            if (0 == DeviceIoControl(
                    hDevice,
                    SMART_RCV_DRIVE_DATA,
                    ref inParam,
                    (uint)Marshal.SizeOf(inParam),
                    ref outParam,
                    (uint)Marshal.SizeOf(outParam),
                    ref bytesReturned,
                    IntPtr.Zero))
            {
                CloseHandle(hDevice);
                throw new IOException(
                          string.Format(ResourcesApi.Win32_DeviceIoControlErr, "SMART_RCV_DRIVE_DATA"));
            }
            CloseHandle(hDevice);
            ChangeByteOrder(outParam.bBuffer.sModelNumber);
            ChangeByteOrder(outParam.bBuffer.sSerialNumber);
            ChangeByteOrder(outParam.bBuffer.sFirmwareRev);
            return(GetHardDiskInfo(outParam.bBuffer));
        }
示例#10
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="driveIndex"></param>
 /// <returns></returns>
 private static HDiskInfo GetHddInfo9X(byte driveIndex)
 {
     GetVersionOutParams vers = new 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 UnauthorizedAccessException("打开 smartvsd.vxd 文件失败。");
     if (0 == DeviceIoControl(hDevice, SMART_GET_VERSION,
         IntPtr.Zero, 0,
         ref vers, (uint)Marshal.SizeOf(vers), ref bytesReturned, IntPtr.Zero))
     {
         CloseHandle(hDevice);
         throw new IOException(string.Format(ResourcesApi.Win32_DeviceIoControlErr, "SMART_GET_VERSION"));
     }
     // 如果 IDE 的鉴定命令不被识别或失败
     if (0 == (vers.fCapabilities & 1))
     {
         CloseHandle(hDevice);
         throw new IOException(ResourcesApi.Win32_DeviceIoControlNotSupport);
     }
     if (0 != (driveIndex & 1))
         inParam.irDriveRegs.bDriveHeadReg = 0xb0;
     else
         inParam.irDriveRegs.bDriveHeadReg = 0xa0;
     if (0 != (vers.fCapabilities & (16 >> driveIndex)))
     {
         // 检测出IDE为ATAPI类型,无法处理
         CloseHandle(hDevice);
         throw new IOException(ResourcesApi.Win32_DeviceIoControlNotSupport);
     }
     else
         inParam.irDriveRegs.bCommandReg = 0xec;
     inParam.bDriveNumber = driveIndex;
     inParam.irDriveRegs.bSectorCountReg = 1;
     inParam.irDriveRegs.bSectorNumberReg = 1;
     inParam.cBufferSize = BUFFER_SIZE;
     if (0 == DeviceIoControl(hDevice, SMART_RCV_DRIVE_DATA, ref inParam, (uint)Marshal.SizeOf(inParam), ref outParam, (uint)Marshal.SizeOf(outParam), ref bytesReturned, IntPtr.Zero))
     {
         CloseHandle(hDevice);
         throw new IOException(string.Format(ResourcesApi.Win32_DeviceIoControlErr, "SMART_RCV_DRIVE_DATA"));
     }
     // 关闭文件句柄
     CloseHandle(hDevice);
     ChangeByteOrder(outParam.bBuffer.sModelNumber);
     ChangeByteOrder(outParam.bBuffer.sSerialNumber);
     ChangeByteOrder(outParam.bBuffer.sFirmwareRev);
     return GetHardDiskInfo(outParam.bBuffer);
 }
示例#11
0
        /// <summary>
        /// 获取在NT平台下指定序列号的硬盘信息。
        /// </summary>
        /// <param name="driveIndex">物理磁盘的数量。</param>
        /// <returns></returns>
        private static HDiskInfo GetHddInfoNT(byte driveIndex)
        {
            GetVersionOutParams vers = new GetVersionOutParams();
            SendCmdInParams inParam = new SendCmdInParams();
            SendCmdOutParams outParam = new SendCmdOutParams();
            uint bytesReturned = 0;

            // 使用 Win2000 或 Xp下的方法获取硬件信息

            // 获取设备的句柄。
            IntPtr hDevice =
                CreateFile(string.Format(@"\\.\PhysicalDrive{0}", driveIndex), GENERIC_READ | GENERIC_WRITE,
                           FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);

            // 开始检查
            if (hDevice == IntPtr.Zero)
                throw new UnauthorizedAccessException("执行 Win32 API 函数 CreateFile 失败。");
            if (0 == DeviceIoControl(hDevice, SMART_GET_VERSION, IntPtr.Zero, 0, ref vers,
                (uint)Marshal.SizeOf(vers),
                ref bytesReturned,
                IntPtr.Zero))
            {
                CloseHandle(hDevice);
                throw new IOException(string.Format(ResourcesApi.Win32_DeviceIoControlErr, "SMART_GET_VERSION"));
            }
            // 检测IDE控制命令是否支持
            if (0 == (vers.fCapabilities & 1))
            {
                CloseHandle(hDevice);
                throw new IOException(ResourcesApi.Win32_DeviceIoControlNotSupport);
            }
            // 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 IOException(ResourcesApi.Win32_DeviceIoControlNotSupport);
            }
            else
                inParam.irDriveRegs.bCommandReg = 0xec;
            inParam.bDriveNumber = driveIndex;
            inParam.irDriveRegs.bSectorCountReg = 1;
            inParam.irDriveRegs.bSectorNumberReg = 1;
            inParam.cBufferSize = 512;

            if (0 == DeviceIoControl(
                hDevice,
                SMART_RCV_DRIVE_DATA,
                ref inParam,
                (uint)Marshal.SizeOf(inParam),
                ref outParam,
                (uint)Marshal.SizeOf(outParam),
                ref bytesReturned,
                IntPtr.Zero))
            {
                CloseHandle(hDevice);
                throw new IOException(
                        string.Format(ResourcesApi.Win32_DeviceIoControlErr, "SMART_RCV_DRIVE_DATA"));
            }
            CloseHandle(hDevice);
            ChangeByteOrder(outParam.bBuffer.sModelNumber);
            ChangeByteOrder(outParam.bBuffer.sSerialNumber);
            ChangeByteOrder(outParam.bBuffer.sFirmwareRev);
            return GetHardDiskInfo(outParam.bBuffer);
        }
示例#12
0
        /// <summary>
        /// 获取NT架构的硬盘信息
        /// </summary>
        /// <param name="driveIndex"></param>
        /// <returns></returns>
        private static HardDiskInfo GetHddInfoNt(byte driveIndex)
        {
            var  vers          = new GetVersionOutParams();
            var  inParam       = new SendCmdInParams();
            var  outParam      = new SendCmdOutParams();
            uint bytesReturned = 0;


            // We start in NT/Win2000
            var hDevice = CreateFile(
                string.Format(@"\\.\PhysicalDrive{0}", driveIndex),
                GenericRead | GenericWrite,
                FileShareRead | FileShareWrite,
                IntPtr.Zero,
                OpenExisting,
                0,
                IntPtr.Zero);

            if (hDevice == IntPtr.Zero)
            {
                throw new Exception("CreateFile faild.");
            }
            if (0 == DeviceIoControl(
                    hDevice,
                    DfpGetVersion,
                    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 command 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)))
            {
                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.bSectorCountReg  = 1;
            inParam.irDriveRegs.bSectorNumberReg = 1;
            inParam.cBufferSize = 512;


            if (0 == DeviceIoControl(
                    hDevice,
                    DfpReceiveDriveData,
                    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));
        }
示例#13
-1
        /// <summary>
        /// ��ȡNT�ܹ���Ӳ����Ϣ
        /// </summary>
        /// <param name="driveIndex"></param>
        /// <returns></returns>
        private static HardDiskInfo GetHddInfoNT(byte driveIndex)
        {
            GetVersionOutParams vers = new GetVersionOutParams();
            SendCmdInParams inParam = new SendCmdInParams();
            SendCmdOutParams outParam = new SendCmdOutParams();
            uint bytesReturned = 0;

            // We start in NT/Win2000
            IntPtr hDevice = CreateFile(
             string.Format(@"\\.\PhysicalDrive{0}", driveIndex),
             GENERIC_READ | GENERIC_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);
                throw new Exception(string.Format("Drive {0} may not exists.", driveIndex + 1));
            }
            // If IDE identify command 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)))
            {
                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.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(outParam),
             ref bytesReturned,
             IntPtr.Zero))
            {
                CloseHandle(hDevice);
                throw new Exception("DeviceIoControl failed: DFP_RECEIVE_DRIVE_DATA");
            }
            CloseHandle(hDevice);

            return GetHardDiskInfo(outParam.bBuffer);
        }