示例#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
            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) || ((int)hDevice == -1))
            {
                throw new Exception("CreateFile faild.");
            }
            if (DeviceIoControl(hDevice, DFP_GET_VERSION, IntPtr.Zero, 0, ref vers,
                                (uint)Marshal.SizeOf(vers), ref bytesReturned, IntPtr.Zero) == 0)
            {
                CloseHandle(hDevice);
                throw new Exception(string.Format("Drive {0} may not exists.", driveIndex + 1));
            }
            // If IDE identify command not supported, fails
            if ((vers.fCapabilities & 1) == 0)
            {
                CloseHandle(hDevice);
                throw new Exception("Error: IDE identify command not supported.");
            }
            // Identify the IDE drives
            if ((driveIndex & 1) != 0)
            {
                inParam.irDriveRegs.bDriveHeadReg = 0xb0;
            }
            else
            {
                inParam.irDriveRegs.bDriveHeadReg = 0xa0;
            }
            if ((vers.fCapabilities & (16 >> driveIndex)) != 0)
            {
                // 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 (DeviceIoControl(hDevice, DFP_RECEIVE_DRIVE_DATA, ref inParam,
                                (uint)Marshal.SizeOf(inParam), ref outParam, (uint)Marshal.SizeOf(outParam),
                                ref bytesReturned, IntPtr.Zero) == 0)
            {
                CloseHandle(hDevice);
                throw new Exception("DeviceIoControl failed: DFP_RECEIVE_DRIVE_DATA");
            }
            CloseHandle(hDevice);

            return(GetHardDiskInfo(outParam.bBuffer));
        }
示例#2
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);