示例#1
0
 public static extern int AttachVirtualDisk(
     IntPtr VirtualDiskHandle,
     ref SECURITY_DESCRIPTOR SecurityDescriptor,
     ATTACH_VIRTUAL_DISK_FLAG Flags,
     int ProviderSpecificFlags,
     ref ATTACH_VIRTUAL_DISK_PARAMETERS Parameters,
     IntPtr Overlapped);
示例#2
0
 public static extern unsafe int AttachVirtualDisk(
     SafeFileHandle VirtualDiskHandle,
     IntPtr SecurityDescriptor,
     ATTACH_VIRTUAL_DISK_FLAG Flags,
     uint ProviderSpecificFlags,
     ref ATTACH_VIRTUAL_DISK_PARAMETERS Parameters,
     [In] NativeOverlapped *Overlapped);
示例#3
0
 public static extern Int32 AttachVirtualDisk(VirtualDiskSafeHandle VirtualDiskHandle, IntPtr SecurityDescriptor, ATTACH_VIRTUAL_DISK_FLAG Flags, Int32 ProviderSpecificFlags, ref ATTACH_VIRTUAL_DISK_PARAMETERS Parameters, IntPtr Overlapped);
示例#4
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.");

        }