Example #1
0
 public static extern int OpenVirtualDisk(
     ref VIRTUAL_STORAGE_TYPE VirtualStorageType,
     [MarshalAs(UnmanagedType.LPWStr)] string Path,
     VIRTUAL_DISK_ACCESS_MASK VirtualDiskAccessMask,
     OPEN_VIRTUAL_DISK_FLAG Flags,
     ref OPEN_VIRTUAL_DISK_PARAMETERS Parameters,
     out IntPtr Handle);
Example #2
0
 public static extern UInt32 OpenVirtualDisk(
     ref VIRTUAL_STORAGE_TYPE VirtualStorageType,
     string Path,
     VIRTUAL_DISK_ACCESS_MASK VirtualDiskAccessMask,
     OPEN_VIRTUAL_DISK_FLAG Flags,
     IntPtr Parameters,   // to make it optional (pass IntPtr.Zero)
     ref VirtualDiskSafeHandle Handle);
Example #3
0
 public static extern UInt32 OpenVirtualDisk(
     ref VIRTUAL_STORAGE_TYPE VirtualStorageType,
     string Path,
     VIRTUAL_DISK_ACCESS_MASK VirtualDiskAccessMask,
     OPEN_VIRTUAL_DISK_FLAG Flags,
     ref OPEN_VIRTUAL_DISK_PARAMETERS Parameters,
     ref VirtualDiskSafeHandle Handle);
        public void LoadFromISO(string path)
        {
            Unload();
            SourceISOName = path;
            SafeFileHandle       handle          = null;
            VIRTUAL_STORAGE_TYPE openStorageType = new VIRTUAL_STORAGE_TYPE();

            openStorageType.DeviceId = VIRTUAL_STORAGE_TYPE_DEVICE_ISO;
            openStorageType.VendorId = VIRTUAL_STORAGE_TYPE_VENDOR_MICROSOFT;
            int result = OpenVirtualDisk(ref openStorageType, SourceISOName, VIRTUAL_DISK_ACCESS_ATTACH_RO | VIRTUAL_DISK_ACCESS_GET_INFO, 0, IntPtr.Zero, out handle);

            if (result != 0)
            {
                throw new Win32Exception(result);
            }
            result = AttachVirtualDisk(handle, IntPtr.Zero, ATTACH_VIRTUAL_DISK_FLAG_READ_ONLY | ATTACH_VIRTUAL_DISK_FLAG_PERMANENT_LIFETIME, 0, IntPtr.Zero, IntPtr.Zero);
            if (result != 0)
            {
                throw new Win32Exception(result);
            }
            StringBuilder sb     = new StringBuilder(250);
            int           sbSize = sb.Capacity;

            result = GetVirtualDiskPhysicalPath(handle, ref sbSize, sb);
            if (result != 0)
            {
                throw new Win32Exception(result);
            }
            String RawDevName = sb.ToString().Replace(@"\\.\", @"\DEVICE\").ToUpperInvariant();

            foreach (DriveInfo di in DriveInfo.GetDrives())
            {
                sb = new StringBuilder(250);
                if (QueryDosDevice(di.Name.Replace("\\", ""), sb, 250) == 0)
                {
                    throw new Win32Exception();
                }
                if (sb.ToString().ToUpperInvariant().Equals(RawDevName))
                {
                    SourceISODrive = di;
                }
            }
            handle.Close();
            if (SourceISODrive == null)
            {
                throw new Win32Exception("Mounted ISO not found");
            }
            SourceWIM = Path.Combine(SourceISODrive.Name, @"sources\install.wim");
            if (!File.Exists(SourceWIM))
            {
                SourceWIM = Path.Combine(SourceISODrive.Name, @"sources\install.esd");
            }
            if (!File.Exists(SourceWIM))
            {
                throw new Win32Exception("No Install.Wim found inside ISO");
            }
            InitializeWIM();
        }
Example #5
0
 public static extern int CreateVirtualDisk(
     ref VIRTUAL_STORAGE_TYPE VirtualStorageType,
     [MarshalAs(UnmanagedType.LPWStr)] string Path,
     VIRTUAL_DISK_ACCESS_MASK VirtualDiskAccessMask,
     ref SECURITY_DESCRIPTOR SecurityDescriptor,
     CREATE_VIRTUAL_DISK_FLAG Flags,
     int ProviderSpecificFlags,
     ref CREATE_VIRTUAL_DISK_PARAMETERS_VERSION2 Parameters,
     IntPtr Overlapped,
     out IntPtr Handle);
        public void Unload()
        {
            if (SourceWIMMountPoint != null)
            {
                UnmountWIM();
            }
            if (SourceISODrive != null)
            {
                VIRTUAL_STORAGE_TYPE openStorageType = new VIRTUAL_STORAGE_TYPE();
                openStorageType.DeviceId = VIRTUAL_STORAGE_TYPE_DEVICE_ISO;
                openStorageType.VendorId = VIRTUAL_STORAGE_TYPE_VENDOR_MICROSOFT;
                SafeFileHandle handle = null;
                int            result = OpenVirtualDisk(ref openStorageType, SourceISOName, VIRTUAL_DISK_ACCESS_DETACH, 0, IntPtr.Zero, out handle);
                if (result != 0)
                {
                    throw new Win32Exception(result);
                }
                result = DetachVirtualDisk(handle, DETACH_VIRTUAL_DISK_FLAG_NONE, 0);
                if (result != 0)
                {
                    throw new Win32Exception(result);
                }
                handle.Close();
            }

            AvailableEditions         = null;
            SourceISOName             = null;
            SourceISODrive            = null;
            SourceWIM                 = null;
            SourceFile                = null;
            BootloaderSourceDirectory = null;
            BootMenuEntryName         = null;
            DestinationFileName       = null;
            VersionInfo               = null;
            EfiSupported              = false;
            EfiUsed = false;
        }
Example #7
0
 public static extern Int32 OpenVirtualDisk(ref VIRTUAL_STORAGE_TYPE VirtualStorageType, String Path, VIRTUAL_DISK_ACCESS_MASK VirtualDiskAccessMask, OPEN_VIRTUAL_DISK_FLAG Flags, ref OPEN_VIRTUAL_DISK_PARAMETERS Parameters, ref VirtualDiskSafeHandle Handle);
Example #8
0
 public static extern Int32 CreateVirtualDisk(ref VIRTUAL_STORAGE_TYPE VirtualStorageType, String Path, VIRTUAL_DISK_ACCESS_MASK VirtualDiskAccessMask, IntPtr SecurityDescriptor, CREATE_VIRTUAL_DISK_FLAG Flags, Int32 ProviderSpecificFlags, ref CREATE_VIRTUAL_DISK_PARAMETERS Parameters, ref NativeOverlapped Overlapped, ref VirtualDiskSafeHandle Handle);
 private static extern Int32 OpenVirtualDisk(ref VIRTUAL_STORAGE_TYPE VirtualStorageType, String Path, int VirtualDiskAccessMask, int Flags, IntPtr Parameters, out SafeFileHandle Handle);
Example #10
0
        // https://sites.google.com/a/jsc-solutions.net/work/knowledge-base/15-dualvr/20150630/udp

        static void Main(string[] args)
        {
            // http://stackoverflow.com/questions/2818179/how-to-force-my-net-app-to-run-as-administrator-on-windows-7

            // "X:\opensource\codeplex\discutils\src\DiscUtils.csproj"
            // https://github.com/perpetual-motion/discutils

            // http://technet.microsoft.com/en-us/magazine/ee872416.aspx
            // http://blogs.technet.com/b/danstolts/archive/2012/11/09/how_2d00_to_2d00_mount_2d00_vhd_2d00_image_2d00_from_2d00_windows_2d00_7_2d00_step_2d00_by_2d00_step_2d00_without_2d00_any_2d00_third_2d00_party_2d00_toolsthe_2d00_easy_2d00_way.aspx

            CDBuilder builder = new CDBuilder();
            builder.UseJoliet = true;
            builder.VolumeIdentifier = "A_SAMPLE_DISK";
            builder.AddFile(@"Folder\Hello.txt", Encoding.ASCII.GetBytes("Hello World!"));

            //Additional information: The process cannot access the file 'X:\jsc.svn\examples\merge\Test\TestCreateVHD\TestCreateVHD\bin\Debug\sample.iso' because it is being used by another process.
            builder.Build(@"sample2.iso");

            //long diskSize = 4 * 1024 * 1024; //30MB
            long diskSize = 5 * 1024 * 1024; //30MB
                                             // Additional information: Requested size is too small for a partition
                                             // 		sectorCount	8143	int

            // Comupter Management/Storage/Disk Management/ shows it unallocated!

            // win7 does not know how to mount vhd?
            // 
            //Additional information: The process cannot access the file 'X:\jsc.svn\examples\merge\Test\TestCreateVHD\TestCreateVHD\bin\Debug\mydisk.vhd' because it is being used by another process.

            string fileName = new FileInfo(@"mydisk2.vhd").FullName;

            using (Stream vhdStream = File.Create(fileName))
            {
                Disk disk = Disk.InitializeDynamic(vhdStream, DiscUtils.Ownership.Dispose, diskSize);

                BiosPartitionTable.Initialize(disk, WellKnownPartitionType.WindowsFat);
                using (FatFileSystem fs = FatFileSystem.FormatPartition(disk, 0, "label1"))
                {
                    fs.CreateDirectory(@"TestDir\CHILD");
                    using (var f = new StreamWriter(fs.OpenFile(@"TestDir\Hello.txt", FileMode.OpenOrCreate, FileAccess.Write)))
                    {
                        f.WriteLine("Hello World!");
                    }

                    // do other things with the file system...
                }
            }

            // http://msdn.microsoft.com/en-us/magazine/dd569754.aspx
            // http://msdn.microsoft.com/en-us/library/windows/desktop/dd323692(v=vs.85).aspx
            // http://www.programmershare.com/1846408/
            // https://www.jmedved.com/2009/05/open-and-attach/


            // http://blogs.msdn.com/b/virtual_pc_guy/archive/2008/01/10/mounting-vhds-from-managed-code.aspx
            // http://support.microsoft.com/kb/981778
            // http://stackoverflow.com/questions/573086/how-to-elevate-privileges-only-when-required
            // This is not true. You can change the Owner of the process and set the DACL and ACL values for the user giving them administrative powers.



            IntPtr handle = IntPtr.Zero;


            // open disk handle
            var openParameters = new OPEN_VIRTUAL_DISK_PARAMETERS();
            openParameters.Version = OPEN_VIRTUAL_DISK_VERSION.OPEN_VIRTUAL_DISK_VERSION_1;
            openParameters.Version1.RWDepth = OPEN_VIRTUAL_DISK_RW_DEPTH_DEFAULT;

            var openStorageType = new VIRTUAL_STORAGE_TYPE();
            openStorageType.DeviceId = VIRTUAL_STORAGE_TYPE_DEVICE_VHD;
            openStorageType.VendorId = VIRTUAL_STORAGE_TYPE_VENDOR_MICROSOFT;

            int openResult = OpenVirtualDisk(ref openStorageType, fileName, VIRTUAL_DISK_ACCESS_MASK.VIRTUAL_DISK_ACCESS_ALL, OPEN_VIRTUAL_DISK_FLAG.OPEN_VIRTUAL_DISK_FLAG_NONE, ref openParameters, ref handle);
            if (openResult != ERROR_SUCCESS)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Native error {0}.", openResult));
            }


            // attach disk - permanently
            var attachParameters = new ATTACH_VIRTUAL_DISK_PARAMETERS();
            attachParameters.Version = ATTACH_VIRTUAL_DISK_VERSION.ATTACH_VIRTUAL_DISK_VERSION_1;
            int attachResult = AttachVirtualDisk(handle, IntPtr.Zero, ATTACH_VIRTUAL_DISK_FLAG.ATTACH_VIRTUAL_DISK_FLAG_PERMANENT_LIFETIME, 0, ref attachParameters, IntPtr.Zero);
            // attachResult = 1314
            // If you get “Native error 1314.” exception, you didn’t run code as user with administrative rights. If you get “Native error 32.”, virtual disk is already attached. 


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


            // close handle to disk
            CloseHandle(handle);

            // http://technet.microsoft.com/en-us/library/dd440865(v=ws.10).aspx
            System.Windows.Forms.MessageBox.Show("Disk is attached.");

        }