Example #1
0
        public static void UnmountVHD(string vhdfile)
        {
            var openParameters = new NativeMethods.OPEN_VIRTUAL_DISK_PARAMETERS();

            openParameters.Version          = NativeMethods.OPEN_VIRTUAL_DISK_VERSION.OPEN_VIRTUAL_DISK_VERSION_1;
            openParameters.Version1.RWDepth = NativeMethods.OPEN_VIRTUAL_DISK_RW_DEPTH_DEFAULT;

            var openStorageType = new NativeMethods.VIRTUAL_STORAGE_TYPE();

            openStorageType.DeviceId = NativeMethods.VIRTUAL_STORAGE_TYPE_DEVICE_UNKNOWN;
            openStorageType.VendorId = NativeMethods.VIRTUAL_STORAGE_TYPE_VENDOR_UNKNOWN;

            var handle = IntPtr.Zero;

            var openResult = NativeMethods.OpenVirtualDisk(ref openStorageType, vhdfile, NativeMethods.VIRTUAL_DISK_ACCESS_MASK.VIRTUAL_DISK_ACCESS_ALL, NativeMethods.OPEN_VIRTUAL_DISK_FLAG.OPEN_VIRTUAL_DISK_FLAG_NONE, ref openParameters, ref handle);

            if (openResult != NativeMethods.ERROR_SUCCESS)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Native error {0}.", openResult));
            }

            var dettachResult = NativeMethods.DetachVirtualDisk(handle, NativeMethods.DETACH_VIRTUAL_DISK_FLAG.DETACH_VIRTUAL_DISK_FLAG_NONE, 0);

            if (dettachResult != NativeMethods.ERROR_SUCCESS)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Native error {0}.", dettachResult));
            }

            NativeMethods.CloseHandle(handle);
        }
Example #2
0
        /// <summary>
        ///     Mounts FFU or VHD files on a target system.
        /// </summary>
        /// <param name="vhdfile">A path as a string to a FFU or VHD file to mount on the current system.</param>
        /// <param name="flag">If set to true, the function will mount a FFU image and not a VHD image.</param>
        /// <returns>
        ///     A string array containing for the first parameter the full path to the junction linked to the image, and for
        ///     the second parameter the Physical disk id path.
        /// </returns>
        public static string MountVHD(string vhdfile, bool readOnly)
        {
            var openParameters = new NativeMethods.OPEN_VIRTUAL_DISK_PARAMETERS();

            openParameters.Version          = NativeMethods.OPEN_VIRTUAL_DISK_VERSION.OPEN_VIRTUAL_DISK_VERSION_1;
            openParameters.Version1.RWDepth = NativeMethods.OPEN_VIRTUAL_DISK_RW_DEPTH_DEFAULT;

            var openStorageType = new NativeMethods.VIRTUAL_STORAGE_TYPE();

            openStorageType.DeviceId = NativeMethods.VIRTUAL_STORAGE_TYPE_DEVICE_UNKNOWN;
            openStorageType.VendorId = NativeMethods.VIRTUAL_STORAGE_TYPE_VENDOR_UNKNOWN;

            var attachParameters = new NativeMethods.ATTACH_VIRTUAL_DISK_PARAMETERS();

            attachParameters.Version = NativeMethods.ATTACH_VIRTUAL_DISK_VERSION.ATTACH_VIRTUAL_DISK_VERSION_1;

            var handle = IntPtr.Zero;

            var openResult = NativeMethods.OpenVirtualDisk(ref openStorageType, vhdfile, NativeMethods.VIRTUAL_DISK_ACCESS_MASK.VIRTUAL_DISK_ACCESS_ALL, NativeMethods.OPEN_VIRTUAL_DISK_FLAG.OPEN_VIRTUAL_DISK_FLAG_NONE, ref openParameters, ref handle);

            if (openResult != NativeMethods.ERROR_SUCCESS)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Native error {0}.", openResult));
            }

            NativeMethods.ATTACH_VIRTUAL_DISK_FLAG flags = NativeMethods.ATTACH_VIRTUAL_DISK_FLAG.ATTACH_VIRTUAL_DISK_FLAG_PERMANENT_LIFETIME;

            if (readOnly)
            {
                flags |= NativeMethods.ATTACH_VIRTUAL_DISK_FLAG.ATTACH_VIRTUAL_DISK_FLAG_READ_ONLY;
            }

            var attachResult = NativeMethods.AttachVirtualDisk(handle, IntPtr.Zero, flags, 0, ref attachParameters, IntPtr.Zero);

            if (attachResult != NativeMethods.ERROR_SUCCESS)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Native error {0}.", attachResult));
            }

            int           bufferSize      = 260;
            StringBuilder vhdPhysicalPath = new StringBuilder(bufferSize);

            NativeMethods.GetVirtualDiskPhysicalPath(handle, ref bufferSize, vhdPhysicalPath);

            NativeMethods.CloseHandle(handle);

            return(Regex.Match(vhdPhysicalPath.ToString(), @"\d+").Value);
        }