/// <summary> /// Open the specified disk image file. /// </summary> /// <param name="imageFile">The vhd or vhdx file to open.</param> /// <param name="openReadOnly">Open as read-only.</param> /// <returns></returns> /// <remarks></remarks> public bool Open(string imageFile, bool openReadOnly) { bool OpenRet = default; string ext = Path.GetExtension(imageFile).ToLower(); VIRTUAL_STORAGE_TYPE vst; if (_Handle != IntPtr.Zero) { Close(); } var vdp1 = new OPEN_VIRTUAL_DISK_PARAMETERS_V1(); var vdp2 = new OPEN_VIRTUAL_DISK_PARAMETERS_V2(); uint r; var am = VIRTUAL_DISK_ACCESS_MASK.VIRTUAL_DISK_ACCESS_GET_INFO | VIRTUAL_DISK_ACCESS_MASK.VIRTUAL_DISK_ACCESS_DETACH; if (!openReadOnly) { am = am | VIRTUAL_DISK_ACCESS_MASK.VIRTUAL_DISK_ACCESS_ATTACH_RW; } vdp2.Version = OPEN_VIRTUAL_DISK_VERSION.OPEN_VIRTUAL_DISK_VERSION_2; vdp2.ResiliencyGuid = Guid.NewGuid(); vdp2.ReadOnly = false; vdp2.GetInfoOnly = false; vdp1.RWDepth = 1U; vdp1.Version = OPEN_VIRTUAL_DISK_VERSION.OPEN_VIRTUAL_DISK_VERSION_1; vst.VendorId = VirtDisk.VIRTUAL_STORAGE_TYPE_VENDOR_MICROSOFT; switch (ext.ToLower() ?? "") { case ".vhd": vst.DeviceId = VirtDisk.VIRTUAL_STORAGE_TYPE_DEVICE_VHD; r = VirtDisk.OpenVirtualDisk(vst, imageFile, am, OPEN_VIRTUAL_DISK_FLAG.OPEN_VIRTUAL_DISK_FLAG_NONE, vdp1, ref _Handle); break; case ".vhdx": vst.DeviceId = VirtDisk.VIRTUAL_STORAGE_TYPE_DEVICE_VHDX; r = VirtDisk.OpenVirtualDisk(vst, imageFile, am, OPEN_VIRTUAL_DISK_FLAG.OPEN_VIRTUAL_DISK_FLAG_NONE, vdp1, ref _Handle); break; default: return(false); } if (r == 0L) { _ImageFile = imageFile; } OpenRet = r == 0L; return(OpenRet); }
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); } }