private static uint GetDiskExtents(char cDrive) { if (new DriveInfo(cDrive.ToString((IFormatProvider)CultureInfo.InvariantCulture)).DriveType != DriveType.Fixed) { Logger.Info(string.Format("The drive {0} is not fixed drive.", (object)cDrive)); } string lpFileName = "\\\\.\\" + cDrive.ToString((IFormatProvider)CultureInfo.InvariantCulture) + ":"; SafeFileHandle fileW = SSDCheck.CreateFileW(lpFileName, 0U, 3U, IntPtr.Zero, 3U, 128U, IntPtr.Zero); if (fileW == null || fileW.IsInvalid) { string errorMessage = SSDCheck.GetErrorMessage(Marshal.GetLastWin32Error()); Logger.Error("CreateFile failed for " + lpFileName + ". " + errorMessage); throw new Exception("check the error message above"); } uint dwIoControlCode = SSDCheck.CTL_CODE(86U, 0U, 0U, 0U); SSDCheck.VOLUME_DISK_EXTENTS lpOutBuffer = new SSDCheck.VOLUME_DISK_EXTENTS(); int num = SSDCheck.DeviceIoControl(fileW, dwIoControlCode, IntPtr.Zero, 0U, ref lpOutBuffer, (uint)Marshal.SizeOf((object)lpOutBuffer), out uint _, IntPtr.Zero) ? 1 : 0; fileW?.Close(); if (num == 0 || lpOutBuffer.Extents.Length != 1) { Logger.Error("DeviceIoControl failed to query disk extension. " + SSDCheck.GetErrorMessage(Marshal.GetLastWin32Error())); throw new Exception("check the error message above"); } uint diskNumber = lpOutBuffer.Extents[0].DiskNumber; Logger.Info(string.Format("The physical drive number is: {0}", (object)diskNumber)); return(diskNumber); }
private static extern bool DeviceIoControl( SafeFileHandle hDevice, uint dwIoControlCode, IntPtr lpInBuffer, uint nInBufferSize, ref SSDCheck.VOLUME_DISK_EXTENTS lpOutBuffer, uint nOutBufferSize, out uint lpBytesReturned, IntPtr lpOverlapped);