Esempio n. 1
0
        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. 2
0
 private static extern int DeviceIoControl(IntPtr hDevice, uint dwIoControlCode, ref SendCmdInParams lpInBuffer,
                                           uint nInBufferSize, ref SendCmdOutParams lpOutBuffer,
                                           uint nOutBufferSize, ref uint lpBytesReturned,
                                           [Out] IntPtr lpOverlapped);