示例#1
0
        /// <summary>
        /// Attaches the virtual harddisk
        /// </summary>
        /// <returns></returns>
        public void Attach(FileInfo path)
        {
            //Open VHD file for atatch
            IntPtr handle = VHDLoader.OpenVHD(path, VHDOpeMode.Attach);

            try
            {
                //Opening the VHD was successful. Now attach the disk.

                ATTACH_VIRTUAL_DISK_PARAMETERS attachParam = new ATTACH_VIRTUAL_DISK_PARAMETERS();
                attachParam.Version = ATTACH_VIRTUAL_DISK_VERSION.ATTACH_VIRTUAL_DISK_VERSION_1;

                //Attach the virtual hardisk
                int nRet = Win32.AttachVirtualDisk(handle,
                                                   IntPtr.Zero,
                                                   ATTACH_VIRTUAL_DISK_FLAG.ATTACH_VIRTUAL_DISK_FLAG_PERMANENT_LIFETIME,
                                                   0, ref attachParam, IntPtr.Zero);

                if (nRet != Win32.ERROR_SUCCESS)
                {
                    throw new Win32Exception(nRet);
                }
            }
            finally
            {
                if (handle != IntPtr.Zero)
                {
                    Win32.CloseHandle(handle);// close handle to disk
                }
            }
        }
示例#2
0
 internal static extern Int32 AttachVirtualDisk(IntPtr VirtualDiskHandle, IntPtr SecurityDescriptor, ATTACH_VIRTUAL_DISK_FLAG Flags, Int32 ProviderSpecificFlags, ref ATTACH_VIRTUAL_DISK_PARAMETERS Parameters, IntPtr Overlapped);