/// <summary> /// Dismounts all volumes mounted by the TestCrypt driver. /// </summary> public static void DismountAll() { IntPtr mountListPtr = IntPtr.Zero; IntPtr umountPtr = IntPtr.Zero; try { // open/load the TestCrypt driver first SafeFileHandle hDriver = OpenDriver(); // retrieve a list of all volumes currently mounted by the TestCrypt driver uint result = 0; mountListPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(MOUNT_LIST_STRUCT))); if (DeviceApi.DeviceIoControl(hDriver, TC_IOCTL_GET_MOUNTED_VOLUMES, mountListPtr, (uint)Marshal.SizeOf(typeof(MOUNT_LIST_STRUCT)), mountListPtr, (uint)Marshal.SizeOf(typeof(MOUNT_LIST_STRUCT)), ref result, IntPtr.Zero)) { MOUNT_LIST_STRUCT mountList = (MOUNT_LIST_STRUCT)Marshal.PtrToStructure(mountListPtr, typeof(MOUNT_LIST_STRUCT)); if (mountList.ulMountedDrives != 0) { // inform the operating system about pending device removal BroadcastDeviceChange(DBT_DEVICEREMOVEPENDING, 0, mountList.ulMountedDrives); UMOUNT_STRUCT umount = new UMOUNT_STRUCT(); umount.ignoreOpenFiles = true; umountPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(UMOUNT_STRUCT))); Marshal.StructureToPtr(umount, umountPtr, true); if (DeviceApi.DeviceIoControl(hDriver, TC_IOCTL_DISMOUNT_ALL_VOLUMES, umountPtr, (uint)Marshal.SizeOf(typeof(UMOUNT_STRUCT)), umountPtr, (uint)Marshal.SizeOf(typeof(UMOUNT_STRUCT)), ref result, IntPtr.Zero)) { umount = (UMOUNT_STRUCT)Marshal.PtrToStructure(umountPtr, typeof(UMOUNT_STRUCT)); if (umount.nReturnCode != 0) { throw new TrueCryptException(TrueCryptException.ExceptionCause.DriverIoControlFailed, umount.nReturnCode, "TC_IOCTL_DISMOUNT_ALL_VOLUMES", null); } else { // inform the operating system about completed device removal BroadcastDeviceChange(DBT_DEVICEREMOVECOMPLETE, 0, mountList.ulMountedDrives); } } else { Win32Exception ex = new Win32Exception(); throw new TrueCryptException(TrueCryptException.ExceptionCause.DriverIoControlFailed, ex.ErrorCode, "TC_IOCTL_DISMOUNT_ALL_VOLUMES", ex.Message); } } else { // there is currently no volume mounted by the TestCrypt driver } } else { Win32Exception ex = new Win32Exception(); throw new TrueCryptException(TrueCryptException.ExceptionCause.DriverIoControlFailed, ex.ErrorCode, "TC_IOCTL_GET_MOUNTED_VOLUMES", ex.Message); } hDriver.Close(); } finally { if (mountListPtr != IntPtr.Zero) { Marshal.FreeHGlobal(mountListPtr); } if (umountPtr != IntPtr.Zero) { Marshal.FreeHGlobal(umountPtr); } } }
/// <summary> /// Dismounts all volumes mounted by the TestCrypt driver. /// </summary> public static void DismountAll() { IntPtr mountListPtr = IntPtr.Zero; IntPtr umountPtr = IntPtr.Zero; try { // open/load the TestCrypt driver first SafeFileHandle hDriver = OpenDriver(); // retrieve a list of all volumes currently mounted by the TestCrypt driver uint result = 0; mountListPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(MOUNT_LIST_STRUCT))); if (DeviceApi.DeviceIoControl(hDriver, TC_IOCTL_GET_MOUNTED_VOLUMES, mountListPtr, (uint)Marshal.SizeOf(typeof(MOUNT_LIST_STRUCT)), mountListPtr, (uint)Marshal.SizeOf(typeof(MOUNT_LIST_STRUCT)), ref result, IntPtr.Zero)) { MOUNT_LIST_STRUCT mountList = (MOUNT_LIST_STRUCT)Marshal.PtrToStructure(mountListPtr, typeof(MOUNT_LIST_STRUCT)); if (mountList.ulMountedDrives != 0) { // inform the operating system about pending device removal BroadcastDeviceChange(DBT_DEVICEREMOVEPENDING, 0, mountList.ulMountedDrives); UMOUNT_STRUCT umount = new UMOUNT_STRUCT(); umount.ignoreOpenFiles = true; umountPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(UMOUNT_STRUCT))); Marshal.StructureToPtr(umount, umountPtr, true); if (DeviceApi.DeviceIoControl(hDriver, TC_IOCTL_DISMOUNT_ALL_VOLUMES, umountPtr, (uint)Marshal.SizeOf(typeof(UMOUNT_STRUCT)), umountPtr, (uint)Marshal.SizeOf(typeof(UMOUNT_STRUCT)), ref result, IntPtr.Zero)) { umount = (UMOUNT_STRUCT)Marshal.PtrToStructure(umountPtr, typeof(UMOUNT_STRUCT)); if (umount.nReturnCode != 0) { throw new TrueCryptException(TrueCryptException.ExceptionCause.DriverIoControlFailed, umount.nReturnCode, "TC_IOCTL_DISMOUNT_ALL_VOLUMES", null); } else { // inform the operating system about completed device removal BroadcastDeviceChange(DBT_DEVICEREMOVECOMPLETE, 0, mountList.ulMountedDrives); } } else { Win32Exception ex = new Win32Exception(); throw new TrueCryptException(TrueCryptException.ExceptionCause.DriverIoControlFailed, ex.ErrorCode, "TC_IOCTL_DISMOUNT_ALL_VOLUMES", ex.Message); } } else { // there is currently no volume mounted by the TestCrypt driver } } else { Win32Exception ex = new Win32Exception(); throw new TrueCryptException(TrueCryptException.ExceptionCause.DriverIoControlFailed, ex.ErrorCode, "TC_IOCTL_GET_MOUNTED_VOLUMES", ex.Message); } hDriver.Close(); } finally { if (mountListPtr != IntPtr.Zero) { Marshal.FreeHGlobal(mountListPtr); } if (umountPtr != IntPtr.Zero) { Marshal.FreeHGlobal(umountPtr); } } }