Ignore:
Timestamp:
06/19/10 10:14:24 (2 years ago)
Author:
lowjoel
Message:
  • Don't define a shortcut function for DeviceIoControl? since that's a rather low-level API and we don't want to confuse things further.
  • Properly implement the querying of the Physical drive a volume belongs to using the IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS Control Code
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/eraser/Eraser.Util/NativeMethods/Kernel.cs

    r2196 r2206  
    646646        public const uint FSCTL_LOCK_VOLUME = 0x90018; 
    647647        public const uint FSCTL_UNLOCK_VOLUME = 0x9001C; 
     648 
     649        [DllImport("Kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] 
     650        [return: MarshalAs(UnmanagedType.Bool)] 
     651        public static extern bool DeviceIoControl(SafeFileHandle hDevice, 
     652            uint dwIoControlCode, IntPtr lpInBuffer, uint nInBufferSize, 
     653            out NTFS_VOLUME_DATA_BUFFER lpOutBuffer, uint nOutBufferSize, 
     654            out uint lpBytesReturned, IntPtr lpOverlapped); 
     655 
     656        /// <summary> 
     657        /// Retrieves information about the specified NTFS file system volume. 
     658        /// </summary> 
     659        public const int FSCTL_GET_NTFS_VOLUME_DATA = (9 << 16) | (25 << 2); 
     660 
     661        /// <summary> 
     662        /// Removes the boot signature from the master boot record, so that the disk will 
     663        /// be formatted from sector zero to the end of the disk. Partition information 
     664        /// is no longer stored in sector zero. 
     665        /// </summary> 
    648666        public const uint IOCTL_DISK_DELETE_DRIVE_LAYOUT = 
    649667            (0x00000007 << 16) | ((0x01 | 0x02) << 14) | (0x0040 << 2); 
     
    679697        [DllImport("Kernel32.dll", SetLastError = true)] 
    680698        [return: MarshalAs(UnmanagedType.Bool)] 
    681         private extern static bool DeviceIoControl(SafeFileHandle hDevice, 
     699        public extern static bool DeviceIoControl(SafeFileHandle hDevice, 
    682700            uint dwIoControlCode, IntPtr lpInBuffer, uint nInBufferSize, 
    683701            out long lpOutBuffer, uint nOutBufferSize, out uint lpBytesReturned, 
    684702            IntPtr lpOverlapped); 
    685703 
    686         public static bool DeviceIoControl(SafeFileHandle hDevice, 
    687             uint dwIoControlCode, IntPtr lpInBuffer, uint nInBufferSize, 
    688             out long lpOutBuffer, out uint lpBytesReturned, IntPtr lpOverlapped) 
    689         { 
    690             return DeviceIoControl(hDevice, dwIoControlCode, lpInBuffer, nInBufferSize, 
    691                 out lpOutBuffer, sizeof(long), out lpBytesReturned, lpOverlapped); 
    692         } 
    693  
    694704        /// <summary> 
    695705        /// Retrieves the length of the specified disk, volume, or partition. 
     
    698708            (0x00000007 << 16) | (0x0001 << 14) | (0x0017 << 2); 
    699709 
    700         [DllImport("Kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] 
    701         [return: MarshalAs(UnmanagedType.Bool)] 
    702         public static extern bool DeviceIoControl(SafeFileHandle hDevice, 
    703             uint dwIoControlCode, IntPtr lpInBuffer, uint nInBufferSize, 
    704             out NTFS_VOLUME_DATA_BUFFER lpOutBuffer, uint nOutBufferSize, 
    705             out uint lpBytesReturned, IntPtr lpOverlapped); 
    706  
    707         /// <summary> 
    708         /// Retrieves information about the specified NTFS file system volume. 
    709         /// </summary> 
    710         public const int FSCTL_GET_NTFS_VOLUME_DATA = (9 << 16) | (25 << 2); 
     710        /// <summary> 
     711        /// Retrieves the physical location of a specified volume on one or more disks. 
     712        /// </summary> 
     713        public const int IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS = 
     714            ((0x00000056) << 16) | ((0) << 14) | ((0) << 2) | (0); 
     715 
     716        /// <summary> 
     717        /// Represents a physical location on a disk. 
     718        /// </summary> 
     719        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] 
     720        public struct VOLUME_DISK_EXTENTS 
     721        { 
     722            /// <summary> 
     723            /// The number of disks in the volume (a volume can span multiple disks). 
     724            ///  
     725            /// An extent is a contiguous run of sectors on one disk. When the number 
     726            /// of extents returned is greater than one (1), the error code 
     727            /// ERROR_MORE_DATA is returned. You should call DeviceIoControl again, 
     728            /// allocating enough buffer space based on the value of NumberOfDiskExtents 
     729            /// after the first DeviceIoControl call. 
     730            /// </summary> 
     731            public uint NumberOfDiskExtents; 
     732 
     733            /// <summary> 
     734            /// The first extent in the set. Subsequent extents are found after this 
     735            /// structure. 
     736            /// </summary> 
     737            public DISK_EXTENT Extent; 
     738        } 
     739 
     740        /// <summary> 
     741        /// Represents a disk extent. 
     742        /// </summary> 
     743        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] 
     744        public struct DISK_EXTENT 
     745        { 
     746            /// <summary> 
     747            /// The number of the disk that contains this extent. 
     748            ///  
     749            /// This is the same number that is used to construct the name of the disk, 
     750            /// for example, the X in PhysicalDriveX or HarddiskX. 
     751            /// </summary> 
     752            public uint DiskNumber; 
     753 
     754            /// <summary> 
     755            /// The offset from the beginning of the disk to the extent, in bytes. 
     756            /// </summary> 
     757            public long StartingOffset; 
     758 
     759            /// <summary> 
     760            /// The number of bytes in this extent. 
     761            /// </summary> 
     762            public long ExtentLength; 
     763        } 
    711764 
    712765        /// <summary> 
Note: See TracChangeset for help on using the changeset viewer.