private static extern bool ImDiskCreateDevice(IntPtr hWndStatusText, ref DISK_GEOMETRY DiskGeometry, ref long ImageOffset, uint Flags, [In, MarshalAs(UnmanagedType.LPWStr)] string Filename, [MarshalAs(UnmanagedType.Bool)] bool NativePath, [In, MarshalAs(UnmanagedType.LPWStr)] string MountPoint);
public async Task DeviceIOControlAsync_NotOverlapped_Works() { const uint IOCTL_DISK_GET_DRIVE_GEOMETRY = 0x070000; const string drive = @"\\.\PhysicalDrive0"; var data = new DISK_GEOMETRY[1]; using (var device = CreateFile( filename: drive, access: 0, share: Kernel32.FileShare.FILE_SHARE_READ | Kernel32.FileShare.FILE_SHARE_WRITE, securityAttributes: IntPtr.Zero, creationDisposition: CreationDisposition.OPEN_EXISTING, flagsAndAttributes: 0, SafeObjectHandle.Null)) { Assert.False(device.IsInvalid); var ret = (int) await DeviceIoControlAsync <byte, DISK_GEOMETRY>( device, (int)IOCTL_DISK_GET_DRIVE_GEOMETRY, Array.Empty <byte>(), data, CancellationToken.None); Assert.Equal(Marshal.SizeOf <DISK_GEOMETRY>(), ret); var pdg = data[0]; Assert.NotEqual(0u, pdg.BytesPerSector); Assert.NotEqual(0, pdg.Cylinders); Assert.Equal(MEDIA_TYPE.FixedMedia, pdg.MediaType); Assert.NotEqual(0u, pdg.SectorsPerTrack); Assert.NotEqual(0u, pdg.TracksPerCylinder); } }
public unsafe void DeviceIOControl_Works() { const uint IOCTL_DISK_GET_DRIVE_GEOMETRY = 0x070000; const string drive = @"\\.\PhysicalDrive0"; using (var device = CreateFile( filename: drive, access: 0, share: Kernel32.FileShare.FILE_SHARE_READ | Kernel32.FileShare.FILE_SHARE_WRITE, securityAttributes: (SECURITY_ATTRIBUTES *)null, creationDisposition: CreationDisposition.OPEN_EXISTING, flagsAndAttributes: 0, SafeObjectHandle.Null)) { Assert.False(device.IsInvalid); DISK_GEOMETRY pdg = default; Assert.True(DeviceIoControl( device, (int)IOCTL_DISK_GET_DRIVE_GEOMETRY, null, 0, &pdg, sizeof(DISK_GEOMETRY), out int pBytesReturned, (OVERLAPPED *)null)); Assert.NotEqual(0u, pdg.BytesPerSector); Assert.NotEqual(0, pdg.Cylinders); Assert.Equal(MEDIA_TYPE.FixedMedia, pdg.MediaType); Assert.NotEqual(0u, pdg.SectorsPerTrack); Assert.NotEqual(0u, pdg.TracksPerCylinder); } }
public DiskGeometry GetDiskGeometry(SafeHandle hDevice) { DiskGeometry geometry = new DiskGeometry(); SafeAllocHandle <DISK_GEOMETRY> diskGeoPtr = null; try { diskGeoPtr = new SafeAllocHandle <DISK_GEOMETRY>(); bool success = DeviceIoControl(hDevice, IOCTL_DISK_GET_DRIVE_GEOMETRY, IntPtr.Zero, 0, diskGeoPtr, diskGeoPtr.SizeOf, out uint bytesReturns, IntPtr.Zero); if (!success || bytesReturns == 0) { m_Win32Error = Marshal.GetLastWin32Error(); return(null); } DISK_GEOMETRY diskGeo = diskGeoPtr.ToStructure(); geometry.MediaType = diskGeo.MediaType; geometry.Cylinders = diskGeo.Cylinders; geometry.TracksPerCylinder = diskGeo.TracksPerCylinder; geometry.SectorsPerTrack = diskGeo.SectorsPerTrack; geometry.BytesPerSector = diskGeo.BytesPerSector; } finally { if (diskGeoPtr != null) { diskGeoPtr.Close(); } } return(geometry); }
/// <summary> /// Constructor. /// </summary> /// <param name="volume">The volume identifier of the physical drive.</param> /// <param name="size">The disk size, in bytes.</param> /// <param name="geometry">The disk geometry of the physical drive.</param> /// <param name="partitionStyle">The style of the partitions on the drive.</param> public DriveInfo(uint driveNumber, Int64 size, DISK_GEOMETRY geometry, PARTITION_STYLE partitionStyle) { DriveNumber = driveNumber; Size = size; Geometry = geometry; PartitionStyle = partitionStyle; Partitions = new List <PARTITION_INFORMATION_EX>(); }
public static ulong GetDiskSize(SafeFileHandle handle) { uint dummy; DISK_GEOMETRY diskGeo = new DISK_GEOMETRY(); Win32.DeviceIoControl(handle, EIOControlCode.DiskGetDriveGeometry, IntPtr.Zero, 0, ref diskGeo, (uint)Marshal.SizeOf(typeof(DISK_GEOMETRY)), out dummy, IntPtr.Zero); return((ulong)((DISK_GEOMETRY)diskGeo).DiskSize); }
public static extern bool DeviceIoControl( IntPtr hDevice, UInt32 dwIoControlCode, IntPtr lpInBuffer, Int32 nInBufferSize, ref DISK_GEOMETRY lpOutBuffer, Int32 nOutBufferSize, out int lpBytesReturned, IntPtr lpOverlapped);
public static extern bool DeviceIoControl( SafeFileHandle hDevice, uint dwIoControlCode, IntPtr lpInBuffer, uint nInBufferSize, ref DISK_GEOMETRY lpOutBuffer, uint nOutBufferSize, out uint lpBytesReturned, IntPtr lpOverlapped );
public static CylinderHeadSector LbaToChs(Int64 lba, DISK_GEOMETRY geometry) { CylinderHeadSector chs; chs.Cylinders = (lba / (geometry.TracksPerCylinder * geometry.SectorsPerTrack)); Int64 tmp = lba % (geometry.TracksPerCylinder * geometry.SectorsPerTrack); chs.TracksPerCylinder = (uint)(tmp / geometry.SectorsPerTrack); chs.SectorsPerTrack = (uint)((tmp % geometry.SectorsPerTrack) + 1); return(chs); }
private static void CreateDevice_Cpl(long diskSize, uint tracksPerCylinder, uint sectorsPerTrack, uint bytesPerSector, long imageOffset, ImDiskFlags flags, string filename, bool nativePath, string mountPoint, ref uint deviceNumber, IntPtr statusControl) { DISK_GEOMETRY diskGeometry = new DISK_GEOMETRY { Cylinders = diskSize, TracksPerCylinder = tracksPerCylinder, SectorsPerTrack = sectorsPerTrack, BytesPerSector = bytesPerSector }; ImDiskCreateDeviceEx(statusControl, ref deviceNumber, ref diskGeometry, ref imageOffset, (uint)flags, filename, nativePath, mountPoint); }
/// <summary> /// Gets disk geometry /// </summary> internal bool GetDriveGeometry(ref DISK_GEOMETRY diskGeo, int driveID, SafeFileHandle handle) { //This will serve as our output buffer var outBuff = IntPtr.Zero; //Our result bool bResult; //Dummy uint dummy; //Do our DeviceIoControl stuff bResult = DeviceIoControl(handle, (uint)EIOControlCode.IOCTL_DISK_GET_DRIVE_GEOMETRY, IntPtr.Zero, 0, ref diskGeo, (uint)Marshal.SizeOf(typeof(DISK_GEOMETRY)), out dummy, IntPtr.Zero); handle.Close(); return(bResult); }
public static DISK_GEOMETRY GetGeometry(SafeFileHandle Handle) { if (Handle.IsInvalid || Handle.IsClosed) { throw new Exception("Native.GetGeometry: Invalid handle specified. It is closed or invalid."); } var geom = new DISK_GEOMETRY(); uint returnedBytes; if (DeviceIoControl(Handle.DangerousGetHandle(), (uint)IOCTL_CONTROL_CODE_CONSTANTS.IOCTL_DISK_GET_DRIVE_GEOMETRY, IntPtr.Zero, 0, ref geom, (uint)Marshal.SizeOf(typeof(DISK_GEOMETRY)), out returnedBytes, IntPtr.Zero)) { return(geom); } Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); return(new DISK_GEOMETRY()); }
private void InitiateCommon(string dosName, FileAccess access) { Debug.WriteLine("Initiating with " + dosName); DiskHandle = PlatformShim.CreateDeviceHandle(dosName, access); DosDeviceName = dosName; if (DiskHandle.IsInvalid) { throw new ArgumentException("Invalid diskName: " + dosName); } _access = access; _deviceIo = new DiskDeviceWrapper(DiskHandle); _diskFs = new FileStream(DiskHandle, _access); _diskInfo = _deviceIo.DiskGetDriveGeometry(); _deviceLength = _deviceIo.DiskGetLengthInfo(); }
public static extern bool DeviceIoControl(IntPtr hDevice, uint dwIoControlCode, IntPtr lpInBuffer, uint nInBufferSize, ref DISK_GEOMETRY lpOutBuffer, uint nOutBufferSize, out uint lpBytesReturned, IntPtr lpOverlapped);
public static DISK_GEOMETRY GetGeometry(SafeFileHandle Handle) { if (Handle.IsInvalid || Handle.IsClosed) throw new Exception("Native.GetGeometry: Invalid handle specified. It is closed or invalid."); var geom = new DISK_GEOMETRY(); uint returnedBytes; if (DeviceIoControl(Handle.DangerousGetHandle(), (uint)IOCTL_CONTROL_CODE_CONSTANTS.IOCTL_DISK_GET_DRIVE_GEOMETRY, IntPtr.Zero, 0, ref geom, (uint)Marshal.SizeOf(typeof(DISK_GEOMETRY)), out returnedBytes, IntPtr.Zero)) return geom; Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); return new DISK_GEOMETRY(); }
/// <summary> /// Gets disk geometry /// </summary> internal bool GetDriveGeometry(ref DISK_GEOMETRY diskGeo, int driveID, SafeFileHandle handle) { //This will serve as our output buffer var outBuff = IntPtr.Zero; //Our result bool bResult; //Dummy uint dummy; //Do our DeviceIoControl stuff bResult = DeviceIoControl(handle, (uint)EIOControlCode.IOCTL_DISK_GET_DRIVE_GEOMETRY, IntPtr.Zero, 0, ref diskGeo, (uint)Marshal.SizeOf(typeof(DISK_GEOMETRY)), out dummy, IntPtr.Zero); handle.Close(); return (bResult); }
private static extern bool DeviceIoControl(SafeFileHandle hVol, DeviceIoControlCode controlCode, IntPtr inBuffer, int nInBufferSize, ref DISK_GEOMETRY outBuffer, int outBufferSize, ref int bytesReturned, IntPtr overlapped);
public static Int64 ChsToLba(CylinderHeadSector chs, DISK_GEOMETRY geometry) { return((chs.Cylinders * geometry.TracksPerCylinder * geometry.SectorsPerTrack) + (chs.TracksPerCylinder * geometry.SectorsPerTrack) + (chs.SectorsPerTrack - 1)); }
/// <summary> /// Constructor. /// </summary> /// <param name="volume">The volume identifier of the physical drive.</param> /// <param name="size">The disk size, in bytes.</param> /// <param name="geometry">The disk geometry of the physical drive.</param> /// <param name="partitionStyle">The style of the partitions on the drive.</param> public DriveInfo(uint driveNumber, Int64 size, DISK_GEOMETRY geometry, PARTITION_STYLE partitionStyle) { DriveNumber = driveNumber; Size = size; Geometry = geometry; PartitionStyle = partitionStyle; Partitions = new List<PARTITION_INFORMATION_EX>(); }
public static CylinderHeadSector LbaToChs(Int64 lba, DISK_GEOMETRY geometry) { CylinderHeadSector chs; chs.Cylinders = (lba / (geometry.TracksPerCylinder * geometry.SectorsPerTrack)); Int64 tmp = lba % (geometry.TracksPerCylinder * geometry.SectorsPerTrack); chs.TracksPerCylinder = (uint)(tmp / geometry.SectorsPerTrack); chs.SectorsPerTrack = (uint)((tmp % geometry.SectorsPerTrack) + 1); return chs; }
public static Int64 ChsToLba(CylinderHeadSector chs, DISK_GEOMETRY geometry) { return (chs.Cylinders * geometry.TracksPerCylinder * geometry.SectorsPerTrack) + (chs.TracksPerCylinder * geometry.SectorsPerTrack) + (chs.SectorsPerTrack - 1); }
public SDManagement(String RootDir) { var de1 = new DiskExtents(); var bytesReturned = 0; try { driveLetter = RootDir; var path = @"\\.\" + driveLetter; using (var sfh = CreateFile(path, EFileAccess.GenericRead | EFileAccess.GenericWrite, EFileShare.Read | EFileShare.Write, IntPtr.Zero, ECreationDisposition.OpenExisting, 0, IntPtr.Zero)) { if (sfh.IsInvalid) { throw new Win32Exception(Marshal.GetLastWin32Error(), "Handle.IsInvalid"); } using (new FileStream(sfh, FileAccess.Read)) { if (DeviceIoControl(sfh, DeviceIoControlCode.IoctlVolumeGetVolumeDiskExtents, IntPtr.Zero, 0, ref de1, Marshal.SizeOf(de1), ref bytesReturned, IntPtr.Zero)) { SDPhysicalName = @"\\.\PhysicalDrive" + de1.first.DiskNumber.ToString(CultureInfo.InvariantCulture); var dg1 = new DISK_GEOMETRY(); if (!DeviceIoControl(sfh, DeviceIoControlCode.IOCTL_DISK_GET_DRIVE_GEOMETRY, IntPtr.Zero, 0, ref dg1, Marshal.SizeOf(dg1), ref bytesReturned, IntPtr.Zero)) { throw new Win32Exception(Marshal.GetLastWin32Error(), "IOCTL_DISK_GET_DRIVE_GEOMETRY"); } SDSize = dg1.DiskSize; if (!DeviceIoControl(sfh, DeviceIoControlCode.FSCTL_LOCK_VOLUME, IntPtr.Zero, 0, ref de1, Marshal.SizeOf(de1), ref bytesReturned, IntPtr.Zero)) { throw new Win32Exception(Marshal.GetLastWin32Error(), "FSCTL_LOCK_VOLUME"); } if (!DeviceIoControl(sfh, DeviceIoControlCode.FSCTL_DISMOUNT_VOLUME, IntPtr.Zero, 0, ref de1, Marshal.SizeOf(de1), ref bytesReturned, IntPtr.Zero)) { throw new Win32Exception(Marshal.GetLastWin32Error(), "FSCTL_DISMOUNT_VOLUME"); } //if (IsWinVistaOrHigher()) { deviceName = GetDeviceName(driveLetter); if (!String.IsNullOrEmpty(deviceName) && !DefineDosDevice(EParam.DDD_RAW_TARGET_PATH | EParam.DDD_REMOVE_DEFINITION | EParam.DDD_EXACT_MATCH_ON_REMOVE, driveLetter, deviceName)) { throw new Win32Exception(Marshal.GetLastWin32Error(), "DefineDosDevice"); } } } var lerror = Marshal.GetLastWin32Error(); if (lerror != 0) { if (lerror == 1) { throw new Win32Exception(lerror, String.Format(@"IncorrectFunction The drive ""{0}"" is removable and removed, like a CDRom with nothing in it.)", driveLetter)); } if (lerror == 122) { throw new Win32Exception(lerror, "ErrorInsufficientBuffer"); } throw new NotSupportedException("Multiple Disc Volume"); } } } } catch (Exception e) { if (!e.Message.StartsWith("External component has thrown an exception.")) { throw; } } diskHandle = CreateFile(SDPhysicalName, EFileAccess.GenericRead | EFileAccess.GenericWrite, EFileShare.Read | EFileShare.Write, IntPtr.Zero, ECreationDisposition.OpenExisting, EFileAttributes.Device | EFileAttributes.NoBuffering | EFileAttributes.Write_Through, IntPtr.Zero); if (diskHandle.IsInvalid) { throw new Win32Exception(Marshal.GetLastWin32Error(), "Handle.IsInvalid"); } if (!DeviceIoControl(diskHandle, DeviceIoControlCode.FSCTL_LOCK_VOLUME, IntPtr.Zero, 0, ref de1, Marshal.SizeOf(de1), ref bytesReturned, IntPtr.Zero)) { throw new Win32Exception(Marshal.GetLastWin32Error(), "FSCTL_LOCK_VOLUME"); } fileStream = new FileStream(diskHandle, FileAccess.ReadWrite); }