/// <summary> /// Mount the virtual drive, permanently. The virtual drive will stay mounted beyond the lifetime of this instance. /// </summary> /// <param name="makePermanent"></param> /// <returns></returns> /// <remarks></remarks> public bool Attach(bool makePermanent) { if (_Handle == IntPtr.Zero || Attached) { return(false); } var mm = new MemPtr(8L); mm.ByteAt(0L) = 1; uint r = VirtDisk.AttachVirtualDisk(_Handle, IntPtr.Zero, makePermanent ? ATTACH_VIRTUAL_DISK_FLAG.ATTACH_VIRTUAL_DISK_FLAG_PERMANENT_LIFETIME : ATTACH_VIRTUAL_DISK_FLAG.ATTACH_VIRTUAL_DISK_FLAG_NONE, 0U, mm.Handle, IntPtr.Zero); mm.Free(); return(r == 0L); }
public override bool MountDiskImage() { if (Path.GetExtension(DiskImage).ToLower() != ".iso") { AutoMounter.Plugin.LogInfo($"WinVirtDisk: Failed to open ISO as VirtualDisk, not an ISO - {DiskImage}"); return(false); } // Check file exists! Probably should return some nice error message... if (!File.Exists(DiskImage)) { AutoMounter.Plugin.LogInfo($"WinVirtDisk: Failed to open ISO as VirtualDisk, ISO does not exist on disk {DiskImage}"); return(false); } if (!AutoMountHelpers.IsDriveLetterFree(RequestedDriveLetter)) { AutoMounter.Plugin.LogInfo($"WinVirtDisk: Failed to open ISO as VirtualDisk, Drive Letter in use {RequestedDriveLetter}"); AutoMounter.API.Dialogs.ShowErrorMessage("Failed to open ISO as VirtualDisk, Drive Letter is in use", "AutoMounter"); return(false); } VirtDisk.VIRTUAL_STORAGE_TYPE type = new VirtDisk.VIRTUAL_STORAGE_TYPE(VirtDisk.VIRTUAL_STORAGE_TYPE_DEVICE_TYPE.VIRTUAL_STORAGE_TYPE_DEVICE_ISO); Win32Error error = VirtDisk.OpenVirtualDisk( ref type, DiskImage, VirtDisk.VIRTUAL_DISK_ACCESS_MASK.VIRTUAL_DISK_ACCESS_READ, VirtDisk.OPEN_VIRTUAL_DISK_FLAG.OPEN_VIRTUAL_DISK_FLAG_NONE, null, out mountedHandle); // Opened successfully if (error.Succeeded) { VirtDisk.ATTACH_VIRTUAL_DISK_PARAMETERS param = VirtDisk.ATTACH_VIRTUAL_DISK_PARAMETERS.Default; // Attach disk (no drive letter) error = VirtDisk.AttachVirtualDisk( mountedHandle, NullSafeHandle.Null(), VirtDisk.ATTACH_VIRTUAL_DISK_FLAG.ATTACH_VIRTUAL_DISK_FLAG_NO_DRIVE_LETTER | VirtDisk.ATTACH_VIRTUAL_DISK_FLAG.ATTACH_VIRTUAL_DISK_FLAG_READ_ONLY, // NO_DRIVE_LETTER at a later date 0, ref param, IntPtr.Zero); if (error.Succeeded) { // Try and assign the requested drive letter to the mounted image if (AssignDriveLetter(RequestedDriveLetter, mountedHandle)) { return(true); } else { AutoMounter.Plugin.LogInfo($"WinVirtDisk: Failed to open ISO as VirtualDisk, Unable to mount to drive letter {RequestedDriveLetter}"); AutoMounter.API.Dialogs.ShowErrorMessage("Failed to open ISO as VirtualDisk, Unable to mount to assigned drive letter", "AutoMounter"); UnmountCurrentDiskImage(); return(false); } } else { AutoMounter.Plugin.LogInfo($"WinVirtDisk: Failed to open ISO as VirtualDisk {error.ToString()}, AttachVirtualDisk Fail"); AutoMounter.API.Dialogs.ShowErrorMessage("Failed to open ISO as VirtualDisk, AttachVirtualDisk Fail", "AutoMounter"); UnmountCurrentDiskImage(); return(false); } } else { AutoMounter.Plugin.LogInfo($"WinVirtDisk: Failed to open ISO as VirtualDisk {error.ToString()}, OpenVirtualDisk Fail"); AutoMounter.API.Dialogs.ShowErrorMessage("Failed to open ISO as VirtualDisk, OpenVirtualDisk Fail", "AutoMounter"); UnmountCurrentDiskImage(); return(false); } }