Esempio n. 1
0
        internal static bool IsVolumeMounted(IntPtr handle)
        {
            int  bytesReturned = 0;
            bool result;

            result = NativeDisk.DeviceIoControl(handle, NativeDisk.FSCTL_IS_VOLUME_MOUNTED, IntPtr.Zero, 0, IntPtr.Zero, 0, ref bytesReturned, IntPtr.Zero);

            return(!result);
        }
Esempio n. 2
0
        internal static void DiskUpdateProperties(IntPtr handle)
        {
            int bytesReturned = 0;

            var result = NativeDisk.DeviceIoControl(handle, NativeDisk.IOCTL_DISK_UPDATE_PROPERTIES, IntPtr.Zero, 0, IntPtr.Zero, 0, ref bytesReturned, IntPtr.Zero);

            if (!result)
            {
                var exception = new Win32Exception(Marshal.GetLastWin32Error());
                throw new Exception(string.Format("Error occured when trying to update disk properties.\nError code: {0}\nMessage: {1}", exception.NativeErrorCode, exception.Message));
            }
        }
Esempio n. 3
0
        public static ulong GetDeviceLength(IntPtr handle)
        {
            ulong length = 0;

            unsafe
            {
                int    returnLength = 0;
                IntPtr lengthPtr    = new IntPtr(&length);

                NativeDisk.DeviceIoControl(handle, NativeDisk.IOCTL_DISK_GET_LENGTH_INFO, IntPtr.Zero, 0, lengthPtr, sizeof(ulong), ref returnLength, IntPtr.Zero);
            }

            return(length);
        }
Esempio n. 4
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);
        }
Esempio n. 5
0
        internal static bool UnmountVolume(IntPtr handle)
        {
            int  bytesReturned = 0;
            bool result;

            result = NativeDisk.DeviceIoControl(handle, NativeDisk.FSCTL_DISMOUNT_VOLUME, IntPtr.Zero, 0, IntPtr.Zero, 0, ref bytesReturned, IntPtr.Zero);

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

            return(result);
        }
Esempio n. 6
0
        internal static void DiskSetDriveLayoutEx(IntPtr handle, DRIVE_LAYOUT_INFORMATION_EX layout)
        {
            var buffSize      = Marshal.SizeOf(layout);
            var buffer        = Marshal.AllocHGlobal(buffSize);
            int bytesReturned = 0;

            Marshal.StructureToPtr(layout, buffer, false);
            var result = NativeDisk.DeviceIoControl(handle, NativeDisk.IOCTL_DISK_SET_DRIVE_LAYOUT_EX, buffer, buffSize, IntPtr.Zero, 0, ref bytesReturned, IntPtr.Zero);

            Marshal.FreeHGlobal(buffer);
            if (!result)
            {
                var exception = new Win32Exception(Marshal.GetLastWin32Error());
                throw new Exception(string.Format("Error occured when trying to set drive layout.\nError code: {0}\nMessage: {1}", exception.NativeErrorCode, exception.Message));
            }
        }
Esempio n. 7
0
        public static ulong GetDeviceLength(int deviceID)
        {
            ulong length = 0;

            IntPtr deviceHandle = NativeDiskWrapper.GetHandleOnDevice(deviceID, NativeDisk.GENERIC_READ);

            unsafe
            {
                int    returnLength = 0;
                IntPtr lengthPtr    = new IntPtr(&length);

                NativeDisk.DeviceIoControl(deviceHandle, NativeDisk.IOCTL_DISK_GET_LENGTH_INFO, IntPtr.Zero, 0, lengthPtr, sizeof(ulong), ref returnLength, IntPtr.Zero);
            }

            NativeDisk.CloseHandle(deviceHandle);

            return(length);
        }
Esempio n. 8
0
        internal static void DiskCreateDiskMBR(IntPtr handle, uint Signature)
        {
            CREATE_DISK disk = new CREATE_DISK();

            disk.PartitionStyle = PARTITION_STYLE.MasterBootRecord; // MBR
            disk.Mbr.Signature  = Signature;
            var buffSize = Marshal.SizeOf(disk);
            var buffer   = Marshal.AllocHGlobal(buffSize);

            Marshal.StructureToPtr(disk, buffer, false);
            int bytesReturned = 0;

            var result = NativeDisk.DeviceIoControl(handle, NativeDisk.IOCTL_DISK_CREATE_DISK, buffer, buffSize, IntPtr.Zero, 0, ref bytesReturned, IntPtr.Zero);

            Marshal.FreeHGlobal(buffer);

            if (!result)
            {
                var exception = new Win32Exception(Marshal.GetLastWin32Error());
                throw new Exception(string.Format("Error occured when trying to create disk MBR.\nError code: {0}\nMessage: {1}", exception.NativeErrorCode, exception.Message));
            }
        }
Esempio n. 9
0
        internal static DRIVE_LAYOUT_INFORMATION_EX DiskGetDriveLayoutEx(IntPtr handle)
        {
            var buffSize = Marshal.SizeOf(typeof(DRIVE_LAYOUT_INFORMATION_EX));
            var buffer   = Marshal.AllocHGlobal(buffSize);
            int outBytes = 0;

            var result = NativeDisk.DeviceIoControl(handle, NativeDisk.IOCTL_DISK_GET_DRIVE_LAYOUT_EX, IntPtr.Zero, 0, buffer, buffSize, ref outBytes, IntPtr.Zero);

            if (!result)
            {
                Marshal.FreeHGlobal(buffer);

                var exception = new Win32Exception(Marshal.GetLastWin32Error());
                throw new Exception(string.Format("Error occured when trying to get drive layout.\nError code: {0}\nMessage: {1}", exception.NativeErrorCode, exception.Message));
            }
            else
            {
                var resultStruct = Marshal.PtrToStructure <DRIVE_LAYOUT_INFORMATION_EX>(buffer);
                Marshal.FreeHGlobal(buffer);
                return(resultStruct);
            }
        }
Esempio n. 10
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);
        }
Esempio n. 11
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));
        }
Esempio n. 12
0
        internal static int CheckDriveType(string name)
        {
            int    retVal      = 0;
            int    bytesReaded = 0;
            IntPtr handle;

            string nameNoSlash = name.Substring(0, name.Length - 1);
            var    driveType   = NativeDisk.GetDriveType(name);

            switch (driveType)
            {
            case DriveType.Fixed:
            case DriveType.Removable:
                handle = NativeDisk.CreateFile(nameNoSlash, NativeDisk.FILE_READ_ATTRIBUTES, NativeDisk.FILE_SHARE_READ | NativeDisk.FILE_SHARE_WRITE, IntPtr.Zero, NativeDisk.OPEN_EXISTING, 0, IntPtr.Zero);
                if (handle == NativeDisk.INVALID_HANDLE_VALUE)
                {
                    var exception = new Win32Exception(Marshal.GetLastWin32Error());
                    throw new Exception(string.Format("Error occured when trying to check drive type 1.\nError code: {0}\nMessage: {1}", exception.NativeErrorCode, exception.Message));
                }
                else
                {
                    STORAGE_DEVICE_DESCRIPTOR devDescriptor = new STORAGE_DEVICE_DESCRIPTOR();
                    STORAGE_DEVICE_NUMBER     devNumber     = GetDiskProperty(handle, ref devDescriptor);

                    if ((driveType == DriveType.Removable && devDescriptor.BusType != STORAGE_BUS_TYPE.BusTypeSata) ||
                        (driveType == DriveType.Fixed && devDescriptor.BusType == STORAGE_BUS_TYPE.BusTypeUsb) ||
                        (devDescriptor.BusType == STORAGE_BUS_TYPE.BusTypeSd) || (devDescriptor.BusType == STORAGE_BUS_TYPE.BusTypeMmc))
                    {
                        if (NativeDisk.DeviceIoControl(handle, NativeDisk.IOCTL_STORAGE_CHECK_VERIFY2, IntPtr.Zero, 0, IntPtr.Zero, 0, ref bytesReaded, IntPtr.Zero))
                        {
                            retVal = devNumber.DeviceNumber;
                        }
                        else
                        {
                            NativeDisk.CloseHandle(handle);
                            Utils.SetErrorMode(Utils.ErrorModes.SEM_FAILCRITICALERRORS);

                            try
                            {
                                handle = NativeDisk.CreateFile(name, NativeDisk.FILE_READ_DATA, NativeDisk.FILE_SHARE_READ | NativeDisk.FILE_SHARE_WRITE, IntPtr.Zero, NativeDisk.OPEN_EXISTING, 0, IntPtr.Zero);
                            }
                            finally
                            {
                                Utils.SetErrorMode(Utils.ErrorModes.SYSTEM_DEFAULT);
                            }

                            if (handle == NativeDisk.INVALID_HANDLE_VALUE)
                            {
                                var exception = new Win32Exception(Marshal.GetLastWin32Error());
                                throw new Exception(string.Format("Error occured when trying to check drive type 2.\nError code: {0}\nMessage: {1}", exception.NativeErrorCode, exception.Message));
                            }
                            if (NativeDisk.DeviceIoControl(handle, NativeDisk.IOCTL_STORAGE_CHECK_VERIFY, IntPtr.Zero, 0, IntPtr.Zero, 0, ref bytesReaded, IntPtr.Zero))
                            {
                                retVal = devNumber.DeviceNumber;
                            }
                        }
                    }

                    NativeDisk.CloseHandle(handle);
                }
                break;

            default:
                throw new Win32Exception("Invalid device type");
            }

            return(retVal);
        }