Esempio n. 1
0
        public bool GetIDBuffer(IntPtr handle, ref byte[] buffer)
        {
            SendCmdInParams  inParam  = new SendCmdInParams();
            SendCmdOutParams outParam = new SendCmdOutParams();
            bool             status;
            uint             bytesReturned = 0;
            byte             bDriveNum     = 0;

            inParam.irDriveRegs.bFeaturesReg     = 0;
            inParam.irDriveRegs.bSectorCountReg  = 0;
            inParam.irDriveRegs.bSectorNumberReg = 0;
            inParam.irDriveRegs.bCylLowReg       = 0;
            inParam.irDriveRegs.bCylHighReg      = 0;
            inParam.irDriveRegs.bDriveHeadReg    = (byte)(0xA0 | ((bDriveNum & 1) << 4));
            inParam.irDriveRegs.bCommandReg      = 0xEC;
            inParam.bDriveNumber = bDriveNum;
            inParam.cBufferSize  = IDENTIFY_BUFFER_SIZE;

            status = DeviceIoControl(handle,
                                     DFP_RECEIVE_DRIVE_DATA,
                                     ref inParam,
                                     (uint)Marshal.SizeOf(inParam),
                                     ref outParam,
                                     (uint)Marshal.SizeOf(outParam),
                                     ref bytesReturned,
                                     IntPtr.Zero);

            if (status == false)
            {
                return(false);
            }
            memcpy(buffer, outParam.bBuffer, 512);
            return(true);
        }
Esempio n. 2
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,
     ref SendCmdInParams lpInBuffer,
     uint nInBufferSize,
     ref SendCmdOutParams lpOutBuffer,
     uint nOutBufferSize,
     ref uint lpBytesReturned,
     [Out] IntPtr lpOverlapped);
Esempio n. 4
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());
        }
Esempio n. 5
0
        // Token: 0x06000527 RID: 1319 RVA: 0x00052AFC File Offset: 0x00050CFC
        private static HardDiskInfo GetHddInfo9x(byte driveIndex)
        {
            GetVersionOutParams getVersionOutParams = default(GetVersionOutParams);
            SendCmdInParams     sendCmdInParams     = default(SendCmdInParams);
            SendCmdOutParams    sendCmdOutParams    = default(SendCmdOutParams);
            uint   num    = 0U;
            IntPtr intPtr = AtapiDevice.CreateFile("\\\\.\\Smartvsd", 0U, 0U, IntPtr.Zero, 1U, 0U, IntPtr.Zero);

            if (intPtr == IntPtr.Zero)
            {
                throw new Exception("Open smartvsd.vxd failed.");
            }
            if (AtapiDevice.DeviceIoControl(intPtr, 475264U, IntPtr.Zero, 0U, ref getVersionOutParams, (uint)Marshal.SizeOf(getVersionOutParams), ref num, IntPtr.Zero) == 0)
            {
                AtapiDevice.CloseHandle(intPtr);
                throw new Exception("DeviceIoControl failed:DFP_GET_VERSION");
            }
            if ((getVersionOutParams.fCapabilities & 1U) == 0U)
            {
                AtapiDevice.CloseHandle(intPtr);
                throw new Exception("Error: IDE identify command not supported.");
            }
            if ((driveIndex & 1) != 0)
            {
                sendCmdInParams.irDriveRegs.bDriveHeadReg = 176;
            }
            else
            {
                sendCmdInParams.irDriveRegs.bDriveHeadReg = 160;
            }
            if (0UL != ((ulong)getVersionOutParams.fCapabilities & (ulong)((long)(16 >> (int)driveIndex))))
            {
                AtapiDevice.CloseHandle(intPtr);
                throw new Exception(string.Format("Drive {0} is a ATAPI device, we don't detect it", (int)(driveIndex + 1)));
            }
            sendCmdInParams.irDriveRegs.bCommandReg      = 236;
            sendCmdInParams.bDriveNumber                 = driveIndex;
            sendCmdInParams.irDriveRegs.bSectorCountReg  = 1;
            sendCmdInParams.irDriveRegs.bSectorNumberReg = 1;
            sendCmdInParams.cBufferSize = 512U;
            if (AtapiDevice.DeviceIoControl(intPtr, 508040U, ref sendCmdInParams, (uint)Marshal.SizeOf(sendCmdInParams), ref sendCmdOutParams, (uint)Marshal.SizeOf(sendCmdOutParams), ref num, IntPtr.Zero) == 0)
            {
                AtapiDevice.CloseHandle(intPtr);
                throw new Exception("DeviceIoControl failed: DFP_RECEIVE_DRIVE_DATA");
            }
            AtapiDevice.CloseHandle(intPtr);
            return(AtapiDevice.GetHardDiskInfo(sendCmdOutParams.bBuffer));
        }
Esempio n. 6
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);
        }
Esempio n. 7
0
 static extern int DeviceIoControl(
  IntPtr hDevice,
  uint dwIoControlCode,
  ref SendCmdInParams lpInBuffer,
  uint nInBufferSize,
  ref SendCmdOutParams 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));
        }
Esempio n. 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));
        }
Esempio n. 12
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);
 }
Esempio n. 13
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);
        }
Esempio n. 14
0
        //-----------SMART------
        public static bool getSmartTable(List <deviceModle> devices)
        {
            System.IntPtr         hDevice;
            string                diskname;
            uint                  bytesReturned = 0;
            SendCmdInParams       inParam       = new SendCmdInParams();
            SendCmdOutParamsSmart outParam      = new SendCmdOutParamsSmart();

            foreach (deviceModle thisDevice in devices)
            {
                try {
                    diskname = "\\\\.\\PhysicalDrive" + thisDevice.PhysicalDriveNumber.ToString();

                    hDevice = Kernel.CreateFile(diskname,
                                                Kernel.GENERIC_READ | Kernel.GENERIC_WRITE,
                                                Kernel.FILE_SHARE_READ | Kernel.FILE_SHARE_WRITE,
                                                0,
                                                Kernel.OPEN_EXISTING,
                                                0,
                                                0);
                    if (hDevice.ToInt32() <= 0)
                    {
                        continue;
                    }

                    if (thisDevice.busType.Equals("PCIe"))
                    {
                        TStorageQueryWithBuffer      nptwb     = new TStorageQueryWithBuffer();
                        TStorageProtocolSpecificData SDATASize = new TStorageProtocolSpecificData();

                        nptwb.ProtocolSpecific.ProtocolType                = TStroageProtocolType.ProtocolTypeNvme;
                        nptwb.ProtocolSpecific.DataType                    = (uint)TStorageProtocolNVMeDataType.NVMeDataTypeLogPage;
                        nptwb.ProtocolSpecific.ProtocolDataRequestValue    = 0x02;
                        nptwb.ProtocolSpecific.ProtocolDataRequestSubValue = 0xFFFFFFFF;
                        nptwb.ProtocolSpecific.ProtocolDataOffset          = (uint)Marshal.SizeOf(SDATASize);
                        nptwb.ProtocolSpecific.ProtocolDataLength          = 512;
                        nptwb.Query.PropertyId = TStoragePropertyId.StorageAdapterProtocolSpecificProperty;
                        nptwb.Query.QueryType  = TStorageQueryType.PropertyStandardQuery;
                        UInt32 dwReturned = 0;

                        if (0 != Kernel.DeviceIoControl(
                                hDevice,
                                Kernel.IOCTL_STORAGE_QUERY_PROPERTY,
                                ref nptwb,
                                (uint)Marshal.SizeOf(nptwb),
                                ref nptwb,
                                (uint)Marshal.SizeOf(nptwb),
                                ref dwReturned, // out's length
                                IntPtr.Zero))
                        {
                            int   StartIndex = 1, EndIndex = 2;
                            ulong RawData = 0;

                            while (EndIndex >= StartIndex)
                            {
                                RawData *= 256;
                                RawData += nptwb.Buffer[EndIndex];
                                EndIndex--;
                            }

                            thisDevice.deviceTemperature = ((long)RawData - 273) + "℃";
                        }
                    }
                    else
                    {
                        thisDevice.SmartAttributes = new Helper.SmartAttributeCollection();
                        thisDevice.SmartAttributes.AddRange(Helper.GetSmartRegisters(Resource.SmartAttributes));

                        inParam.cBufferSize                  = 512;
                        inParam.bDriveNumber                 = (byte)thisDevice.PhysicalDriveNumber;
                        inParam.irDriveRegs.bFeaturesReg     = 0xD0;
                        inParam.irDriveRegs.bSectorCountReg  = 1;
                        inParam.irDriveRegs.bSectorNumberReg = 1;
                        inParam.irDriveRegs.bCylLowReg       = 0x4F;
                        inParam.irDriveRegs.bCylHighReg      = 0xC2;
                        inParam.irDriveRegs.bDriveHeadReg    = 0xA0;
                        inParam.irDriveRegs.bCommandReg      = 0xB0;

                        if (0 != Kernel.DeviceIoControl(hDevice,
                                                        Kernel.SMART_RCV_DRIVE_DATA,
                                                        ref inParam,
                                                        (uint)Marshal.SizeOf(inParam),
                                                        ref outParam,
                                                        (uint)Marshal.SizeOf(outParam),
                                                        ref bytesReturned,
                                                        IntPtr.Zero))
                        {
                            for (int i = 0; i < 42; ++i)
                            {
                                Byte[] bytes = outParam.bBuffer;

                                int id = bytes[i * 12 + 2];

                                int flags = bytes[i * 12 + 4]; // least significant status byte, +3 most significant byte, but not used so ignored.
                                //bool advisory = (flags & 0x1) == 0x0;
                                bool failureImminent = (flags & 0x1) == 0x1;
                                //bool onlineDataCollection = (flags & 0x2) == 0x2;

                                int value      = bytes[i * 12 + 5];
                                int worst      = bytes[i * 12 + 6];
                                int vendordata = BitConverter.ToInt32(bytes, i * 12 + 7);
                                //if (id == 0) continue;

                                foreach (Helper.SmartAttribute attr in thisDevice.SmartAttributes)
                                {
                                    if (attr.Register == id)
                                    {
                                        attr.Current = value;
                                        attr.Worst   = worst;
                                        attr.Data    = vendordata;
                                        attr.IsOK    = failureImminent == false;
                                    }
                                }
                            }
                        }
                    }
                    Kernel.CloseHandle(hDevice);
                } catch (Exception ex) {
                    System.Diagnostics.Trace.WriteLine(ex.ToString());
                }
            }
            return(true);
        }
Esempio n. 15
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));
        }
Esempio n. 16
-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);
        }