コード例 #1
0
        internal static STORAGE_DEVICE_NUMBER GetDiskProperty(IntPtr handle, ref STORAGE_DEVICE_DESCRIPTOR devDescriptor)
        {
            STORAGE_PROPERTY_QUERY query   = new STORAGE_PROPERTY_QUERY();
            STORAGE_DEVICE_NUMBER  devInfo = new STORAGE_DEVICE_NUMBER();
            int    outBytes = 0;
            bool   result;
            IntPtr queryStructPtr;
            IntPtr devDescriptorStructPtr;
            IntPtr devInfoStructPtr;

            query.PropertyId = STORAGE_PROPERTY_ID.StorageDeviceProperty;
            query.QueryType  = STORAGE_QUERY_TYPE.PropertyStandardQuery;

            queryStructPtr         = NativeDisk.StructToPtr(query);
            devDescriptorStructPtr = NativeDisk.StructToPtr(devDescriptor);
            devInfoStructPtr       = NativeDisk.StructToPtr(devInfo);

            result = NativeDisk.DeviceIoControl(handle, NativeDisk.IOCTL_STORAGE_QUERY_PROPERTY, queryStructPtr, Marshal.SizeOf(query), devDescriptorStructPtr, Marshal.SizeOf(devDescriptor), ref outBytes, IntPtr.Zero);

            Marshal.FreeHGlobal(queryStructPtr);
            Marshal.FreeHGlobal(devDescriptorStructPtr);

            if (result)
            {
                result = NativeDisk.DeviceIoControl(handle, NativeDisk.IOCTL_STORAGE_GET_DEVICE_NUMBER, IntPtr.Zero, 0, devInfoStructPtr, Marshal.SizeOf(devInfo), ref outBytes, IntPtr.Zero);
                if (!result)
                {
                    Marshal.FreeHGlobal(devInfoStructPtr);

                    var exception = new Win32Exception(Marshal.GetLastWin32Error());
                    throw new Exception(string.Format("Error occured when trying to get disk property IOCTL_STORAGE_GET_DEVICE_NUMBER.\nError code: {0}\nMessage: {1}", exception.NativeErrorCode, exception.Message));
                }
            }
            else
            {
                if (NativeDisk.DeviceIoControl(handle, NativeDisk.IOCTL_STORAGE_CHECK_VERIFY2, IntPtr.Zero, 0, IntPtr.Zero, 0, ref outBytes, IntPtr.Zero))
                {
                    Marshal.FreeHGlobal(devInfoStructPtr);

                    var exception = new Win32Exception(Marshal.GetLastWin32Error());
                    throw new Exception(string.Format("Error occured when trying to get disk property IOCTL_STORAGE_CHECK_VERIFY2.\nError code: {0}\nMessage: {1}", exception.NativeErrorCode, exception.Message));
                }
            }
            var resultStruct = Marshal.PtrToStructure <STORAGE_DEVICE_NUMBER>(devInfoStructPtr);

            Marshal.FreeHGlobal(devInfoStructPtr);

            return(resultStruct);
        }
コード例 #2
0
        internal static PARTITION_INFORMATION_EX GetDiskPartitionInfo(IntPtr handle)
        {
            bool result;
            int  outBytes = 0;
            PARTITION_INFORMATION_EX partitionInfo = new PARTITION_INFORMATION_EX();
            IntPtr partitionInfoStrPtr;

            partitionInfoStrPtr = NativeDisk.StructToPtr(partitionInfo);

            result = NativeDisk.DeviceIoControl(handle, NativeDisk.IOCTL_DISK_GET_PARTITION_INFO_EX, IntPtr.Zero, 0, partitionInfoStrPtr, Marshal.SizeOf(partitionInfo), ref outBytes, IntPtr.Zero);
            if (!result)
            {
                Marshal.FreeHGlobal(partitionInfoStrPtr);

                var exception = new Win32Exception(Marshal.GetLastWin32Error());
                throw new Exception(string.Format("Error occured when trying to get disk partition info.\nError code: {0}\nMessage: {1}", exception.NativeErrorCode, exception.Message));
            }
            var resultStruct = partitionInfo = Marshal.PtrToStructure <PARTITION_INFORMATION_EX>(partitionInfoStrPtr);

            Marshal.FreeHGlobal(partitionInfoStrPtr);
            return(resultStruct);
        }
コード例 #3
0
        internal static ulong GetNumberOfSectors(IntPtr handle, ref ulong sectorSize)
        {
            int foo = 0;
            DISK_GEOMETRY_EX diskGeometry = new DISK_GEOMETRY_EX();
            bool             result;
            IntPtr           diskGeometryPtr = NativeDisk.StructToPtr(diskGeometry);

            result = NativeDisk.DeviceIoControl(handle, NativeDisk.IOCTL_DISK_GET_DRIVE_GEOMETRY_EX, IntPtr.Zero, 0, diskGeometryPtr, Marshal.SizeOf(diskGeometry), ref foo, IntPtr.Zero);

            if (!result)
            {
                var exception = new Win32Exception(Marshal.GetLastWin32Error());
                throw new Exception(string.Format("Error occured when trying to get number of sectors.\nError code: {0}\nMessage: {1}", exception.NativeErrorCode, exception.Message));
            }

            diskGeometry = Marshal.PtrToStructure <DISK_GEOMETRY_EX>(diskGeometryPtr);
            Marshal.FreeHGlobal(diskGeometryPtr);

            sectorSize = (ulong)diskGeometry.Geometry.BytesPerSector;

            return((ulong)(diskGeometry.DiskSize / diskGeometry.Geometry.BytesPerSector));
        }