Esempio n. 1
0
        public static void Main(string[] args)
        {
            // vhdutil attach <file>
            // vhdutil detach <file>

            if (args.Length < 2)
            {
                return;
            }

            string cmd = args[0];

            try
            {
                if (cmd == "attach")
                {
                    IntPtr handle = IntPtr.Zero;

                    // open disk handle
                    var openParameters = new OpenVirtualDiskParameters();
                    openParameters.Version          = OpenVirtualDiskVersion.OpenVirtualDiskVersion1;
                    openParameters.Version1.RWDepth = OpenVirtualDiskRwDepthDefault;

                    var openStorageType = new VirtualStorageType();
                    openStorageType.DeviceId = VirtualStorageTypeDeviceVhd;
                    openStorageType.VendorId = VirtualStorageTypeVendorMicrosoft;

                    int openResult = OpenVirtualDisk(ref openStorageType, args[1],
                                                     VirtualDiskAccessMask.VirtualDiskAccessAll, OpenVirtualDiskFlag.OpenVirtualDiskFlagNone,
                                                     ref openParameters, ref handle);
                    if (openResult != ErrorSuccess)
                    {
                        throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture,
                                                                          "Native error {0}.", openResult));
                    }

                    // attach disk – permanently
                    var attachParameters = new AttachVirtualDiskParameters();
                    attachParameters.Version = AttachVirtualDiskVersion.AttachVirtualDiskVersion1;
                    int attachResult = AttachVirtualDisk(handle, IntPtr.Zero,
                                                         AttachVirtualDiskFlag.AttachVirtualDiskFlagPermanentLifetime, 0, ref attachParameters,
                                                         IntPtr.Zero);
                    if (attachResult != ErrorSuccess)
                    {
                        throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture,
                                                                          "Native error {0}.", attachResult));
                    }

                    // close handle to disk
                    CloseHandle(handle);
                }
                else if (cmd == "detach")
                {
                    IntPtr handle = IntPtr.Zero;

                    // open disk handle
                    var openParameters = new OpenVirtualDiskParameters();
                    openParameters.Version          = OpenVirtualDiskVersion.OpenVirtualDiskVersion1;
                    openParameters.Version1.RWDepth = OpenVirtualDiskRwDepthDefault;

                    var openStorageType = new VirtualStorageType();
                    openStorageType.DeviceId = VirtualStorageTypeDeviceVhd;
                    openStorageType.VendorId = VirtualStorageTypeVendorMicrosoft;

                    int openResult = OpenVirtualDisk(ref openStorageType, args[1],
                                                     VirtualDiskAccessMask.VirtualDiskAccessAll, OpenVirtualDiskFlag.OpenVirtualDiskFlagNone,
                                                     ref openParameters, ref handle);
                    if (openResult != ErrorSuccess)
                    {
                        throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture,
                                                                          "Native error {0}.", openResult));
                    }

                    int attachResult = DetachVirtualDisk(handle, DetachVirtualDiskFlag.DetachVirtualDiskFlagNone, 0);
                    if (attachResult != ErrorSuccess)
                    {
                        throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture,
                                                                          "Native error {0}.", attachResult));
                    }

                    // close handle to disk
                    CloseHandle(handle);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Something went wrong: {0}\n{1}", e.Message, e.StackTrace);
            }
        }
Esempio n. 2
0
 public static extern Int32 OpenVirtualDisk(ref VirtualStorageType virtualStorageType, string path, VirtualDiskAccessMask virtualDiskAccessMask, OpenVirtualDiskFlag flags, ref OpenVirtualDiskParameters parameters, ref IntPtr handle);