예제 #1
0
        /// <summary>
        /// Adds a single .cab or .msu file to a Windows® image.
        /// </summary>
        /// <param name="session">A valid DISM Session. The DISM Session must be associated with an image. You can associate a session with an image by using the OpenSession method.</param>
        /// <param name="packagePath">A relative or absolute path to the .cab or .msu file being added or a folder containing the expanded files of a single .cab file.</param>
        /// <param name="ignoreCheck">Specifies whether to ignore the internal applicability checks that are done when a package is added.</param>
        /// <param name="preventPending">Specifies whether to add a package if it has pending online actions.</param>
        /// <param name="progressCallback">A DismProgressCallback method to call when progress is made.</param>
        /// <param name="userData">Optional user data to pass to the DismProgressCallback method.</param>
        /// <exception cref="DismException">When a failure occurs.</exception>
        /// <exception cref="OperationCanceledException">When the user requested the operation be canceled.</exception>
        /// <exception cref="DismRebootRequiredException">When the operation requires a reboot to complete.</exception>
        /// <exception cref="DismPackageNotApplicableException">When the package is not applicable to the specified session.</exception>
        public static void AddPackage(DismSession session, string packagePath, bool ignoreCheck, bool preventPending, Microsoft.Dism.DismProgressCallback progressCallback, object userData)
        {
            // Create a DismProgress object to wrap the callback and allow cancellation
            DismProgress progress = new DismProgress(progressCallback, userData);

            int hresult = NativeMethods.DismAddPackage(session, packagePath, ignoreCheck, preventPending, progress.EventHandle, progress.DismProgressCallbackNative, IntPtr.Zero);

            DismUtilities.ThrowIfFail(hresult, session);
        }
예제 #2
0
        private static void SetEdition(DismSession session, string editionID, string productKey, Dism.DismProgressCallback progressCallback, object userData)
        {
            // Create a DismProgress object to wrap the callback and allow cancellation
            DismProgress progress = new DismProgress(progressCallback, userData);

            int hresult = NativeMethods._DismSetEdition(session, editionID, productKey, progress.EventHandle, progress.DismProgressCallbackNative, IntPtr.Zero);

            DismUtilities.ThrowIfFail(hresult, session);
        }
예제 #3
0
        /// <summary>
        /// Disables a feature in the current image.
        /// </summary>
        /// <param name="session">A valid DISM Session. The DISM Session must be associated with an image. You can associate a session with an image by using the DismOpenSession Function.</param>
        /// <param name="featureName">The name of the feature that you want to disable. To disable more than one feature, separate each feature name with a semicolon.</param>
        /// <param name="packageName">Optional. The name of the parent package that the feature is a part of.
        ///
        /// This is an optional parameter. If no package is specified, then the default Windows® Foundation package is used.</param>
        /// <param name="removePayload">Specifies whether to remove the files required to enable the feature.</param>
        /// <param name="progressCallback">A progress callback method to invoke when progress is made.</param>
        /// <param name="userData">Optional user data to pass to the DismProgressCallback method.</param>
        /// <exception cref="DismException">When a failure occurs.</exception>
        /// <exception cref="OperationCanceledException">When the user requested the operation be canceled.</exception>
        /// <exception cref="DismRebootRequiredException">When the operation requires a reboot to complete.</exception>
        public static void DisableFeature(DismSession session, string featureName, string packageName, bool removePayload, Microsoft.Dism.DismProgressCallback progressCallback, object userData)
        {
            // Create a DismProgress object to wrap the callback and allow cancellation
            DismProgress progress = new DismProgress(progressCallback, userData);

            int hresult = NativeMethods.DismDisableFeature(session, featureName, packageName, removePayload, progress.EventHandle, progress.DismProgressCallbackNative, IntPtr.Zero);

            DismUtilities.ThrowIfFail(hresult, session);
        }
예제 #4
0
        /// <summary>
        /// Removes the capability from an image.
        /// </summary>
        /// <param name="session">A valid DISM Session. The DISM Session must be associated with an image. You can associate a session with an image by using the DismOpenSession Function.</param>
        /// <param name="capabilityName">The name of the capability that is being removed</param>
        /// <param name="progressCallback">A progress callback method to invoke when progress is made.</param>
        /// <param name="userData">Optional user data to pass to the DismProgressCallback method.</param>
        /// <exception cref="DismException">When a failure occurs.</exception>
        /// <exception cref="OperationCanceledException">When the user requested the operation be canceled.</exception>
        /// <exception cref="DismRebootRequiredException">When the operation requires a reboot to complete.</exception>
        public static void RemoveCapability(DismSession session, string capabilityName, Dism.DismProgressCallback progressCallback, object userData)
        {
            // Create a DismProgress object to wrap the callback and allow cancellation
            DismProgress progress = new DismProgress(progressCallback, userData);

            int hresult = NativeMethods.DismRemoveCapability(session, capabilityName, progress.EventHandle, progress.DismProgressCallbackNative, IntPtr.Zero);

            DismUtilities.ThrowIfFail(hresult, session);
        }
예제 #5
0
        /// <summary>
        /// Removes a package from an image.
        /// </summary>
        /// <param name="session">A valid DISM Session. The DISM Session must be associated with an image. You can associate a session with an image by using the DismOpenSession Function.</param>
        /// <param name="identifier">Either an absolute path to a .cab file or the package name, depending on the PackageIdentifier parameter value.</param>
        /// <param name="packageIdentifier">A DismPackageIdentifier Enumeration.</param>
        /// <param name="progressCallback">A progress callback method to invoke when progress is made.</param>
        /// <param name="userData">Optional user data to pass to the DismProgressCallback method.</param>
        private static void RemovePackage(DismSession session, string identifier, DismPackageIdentifier packageIdentifier, Dism.DismProgressCallback progressCallback, object userData)
        {
            // Create a DismProgress object to wrap the callback and allow cancellation
            var progress = new DismProgress(progressCallback, userData);

            int hresult = NativeMethods.DismRemovePackage(session, identifier, packageIdentifier, progress.EventHandle, progress.DismProgressCallbackNative, IntPtr.Zero);

            DismUtilities.ThrowIfFail(hresult, session);
        }
        /// <summary>
        /// Adds an app package (.appx) that will install for each new user to a Windows image.
        /// </summary>
        /// <param name="session">A valid DISM Session.</param>
        /// <param name="appPath">Specifies the location of the app package (.appx) to add to the Windows image.</param>
        /// <param name="dependencyPackages">Specifies the location of dependency packages.</param>
        /// <param name="licensePath">Specifies the location of the .xml file containing your application license.</param>
        /// <param name="customDataPath">Specifies the location of a custom data file. The custom data file will be renamed custom.data and saved in the app data store.</param>
        /// <exception cref="DismRebootRequiredException">When the operation requires a reboot to complete.</exception>
        public static void AddProvisionedAppxPackage(DismSession session, string appPath, List <string> dependencyPackages, string licensePath, string customDataPath)
        {
            string[] dependencyPackagesArray = dependencyPackages?.ToArray() ?? new string[0];

            bool skipLicense = String.IsNullOrEmpty(licensePath);

            int hresult = NativeMethods._DismAddProvisionedAppxPackage(session, appPath, dependencyPackagesArray, (uint)dependencyPackagesArray.Length, licensePath, skipLicense, customDataPath);

            DismUtilities.ThrowIfFail(hresult, session);
        }
        /// <summary>
        /// Checks whether the image can be serviced or whether it is corrupted.
        /// </summary>
        /// <param name="session">A valid DISM Session. The DISM Session must be associated with an image. You can associate a session with an image by using the DismOpenSession Function.</param>
        /// <param name="scanImage">Specifies whether to scan the image or just check for flags from a previous scan.</param>
        /// <param name="progressCallback">A DismProgressCallback method to call when progress is made.</param>
        /// <param name="userData">Optional user data to pass to the DismProgressCallback method.</param>
        /// <returns>A <see cref="DismImageHealthState"/> indicating the health state of the image.</returns>
        /// <exception cref="DismException">When a failure occurs.</exception>
        /// <exception cref="OperationCanceledException">When the user requested the operation be canceled.</exception>
        public static DismImageHealthState CheckImageHealth(DismSession session, bool scanImage, Microsoft.Dism.DismProgressCallback progressCallback, object userData)
        {
            // Create a DismProgress object to wrap the callback and allow cancellation
            var progress = new DismProgress(progressCallback, userData);

            int hresult = NativeMethods.DismCheckImageHealth(session, scanImage, progress.EventHandle, progress.DismProgressCallbackNative, IntPtr.Zero, out DismImageHealthState imageHealthState);

            DismUtilities.ThrowIfFail(hresult);

            return(imageHealthState);
        }
예제 #8
0
        public static string GetCurrentEdition(DismSession Session)
        {
            int hresult = NativeMethods._DismGetCurrentEdition(Session, out IntPtr EditionIdStringBuf);

            DismUtilities.ThrowIfFail(hresult);

            // Get a string from the pointer
            string EditionId = EditionIdStringBuf.ToStructure <DismString>();

            return(EditionId);
        }
예제 #9
0
        /// <summary>
        /// Mounts a WIM or VHD image file to a specified location.
        /// </summary>
        /// <param name="imageFilePath">The path to the WIM or VHD file on the local computer. A .wim, .vhd, or .vhdx file name extension is required.</param>
        /// <param name="mountPath">The path of the location where the image should be mounted. This mount path must already exist on the computer. The Windows image in a .wim, .vhd, or .vhdx file can be mounted to an empty folder on an NTFS formatted drive. A Windows image in a .vhd or .vhdx file can also be mounted to an unassigned drive letter. You cannot mount an image to the root of the existing drive.</param>
        /// <param name="imageIndex">The index of the image in the WIM file that you want to mount. For a VHD file, you must specify an index of 1.</param>
        /// <param name="imageName">The name of the image that you want to mount.</param>
        /// <param name="imageIdentifier">A DismImageIdentifier Enumeration value such as DismImageIndex.</param>
        /// <param name="readOnly">Specifies if the image should be mounted in read-only mode.</param>
        /// <param name="options">Specifies options to use when mounting an image.</param>
        /// <param name="progressCallback">A progress callback method to invoke when progress is made.</param>
        /// <param name="userData">Optional user data to pass to the DismProgressCallback method.</param>
        private static void MountImage(string imageFilePath, string mountPath, int imageIndex, string imageName, DismImageIdentifier imageIdentifier, bool readOnly, DismMountImageOptions options, Microsoft.Dism.DismProgressCallback progressCallback, object userData)
        {
            // Determine the flags to pass to the native call
            var flags = (readOnly ? DismApi.DISM_MOUNT_READONLY : DismApi.DISM_MOUNT_READWRITE) | (uint)options;

            // Create a DismProgress object to wrap the callback and allow cancellation
            var progress = new DismProgress(progressCallback, userData);

            int hresult = NativeMethods.DismMountImage(imageFilePath, mountPath, (uint)imageIndex, imageName, imageIdentifier, flags, progress.EventHandle, progress.DismProgressCallbackNative, IntPtr.Zero);

            DismUtilities.ThrowIfFail(hresult);
        }
예제 #10
0
        /// <summary>
        /// Commits the changes made to a Windows® image in a mounted .wim or .vhd file.
        /// </summary>
        /// <param name="session">A valid DISM Session. The DISM Session must be associated with an image. You can associate a session with an image by using the <see cref="OpenOfflineSession(string)" /> method.</param>
        /// <param name="discardChanges">true or false to discard changes made to the image.</param>
        /// <param name="progressCallback">A progress callback method to invoke when progress is made.</param>
        /// <param name="userData">Optional user data to pass to the DismProgressCallback method.</param>
        /// <exception cref="DismException">When a failure occurs.</exception>
        /// <exception cref="OperationCanceledException">When the user requested the operation be canceled.</exception>
        public static void CommitImage(DismSession session, bool discardChanges, Microsoft.Dism.DismProgressCallback progressCallback, object userData)
        {
            // Create the flags
            UInt32 flags = discardChanges ? DISM_DISCARD_IMAGE : DISM_COMMIT_IMAGE;

            // Create a DismProgress object to wrap the callback and allow cancellation
            DismProgress progress = new DismProgress(progressCallback, userData);

            int hresult = NativeMethods.DismCommitImage(session, flags, progress.EventHandle, progress.DismProgressCallbackNative, IntPtr.Zero);

            DismUtilities.ThrowIfFail(hresult, session);
        }
        /// <summary>
        /// Repairs a corrupted image that has been identified as repairable by the CheckImageHealth Function.
        /// </summary>
        /// <param name="session">A valid DISM Session. The DISM Session must be associated with an image. You can associate a session with an image by using the DismOpenSession Function.</param>
        /// <param name="limitAccess">Specifies whether the RestoreImageHealth method should contact Windows Update (WU) as a source location for downloading repair files. Before checking WU, DISM will check for the files in the sourcePaths provided and in any locations specified in the registry by Group Policy. If the files that are required to enable the feature are found in these other specified locations, this flag is ignored.</param>
        /// <param name="sourcePaths">List of source locations to check for repair files.</param>
        /// <param name="progressCallback">A progress callback method to invoke when progress is made.</param>
        /// <param name="userData">Optional user data to pass to the DismProgressCallback method.</param>
        /// <exception cref="DismException">When a failure occurs.</exception>
        /// <exception cref="OperationCanceledException">When the user requested the operation be canceled.</exception>
        /// <exception cref="DismRebootRequiredException">When the operation requires a reboot to complete.</exception>
        public static void RestoreImageHealth(DismSession session, bool limitAccess, List <string> sourcePaths, Dism.DismProgressCallback progressCallback, object userData)
        {
            // Get the list of source paths as an array
            var sourcePathsArray = sourcePaths?.ToArray() ?? new string[0];

            // Create a DismProgress object to wrap the callback and allow cancellation
            var progress = new DismProgress(progressCallback, userData);

            int hresult = NativeMethods.DismRestoreImageHealth(session, sourcePathsArray, (uint)sourcePathsArray.Length, limitAccess, progress.EventHandle, progress.DismProgressCallbackNative, IntPtr.Zero);

            DismUtilities.ThrowIfFail(hresult, session);
        }
예제 #12
0
        /// <summary>
        /// Add a capability to an image.
        /// </summary>
        /// <param name="session">A valid DISM Session. The DISM Session must be associated with an image. You can associate a session with an image by using the <see cref="OpenOfflineSession(string)" /> method.</param>
        /// <param name="capabilityName">The name of the capability that is being added.</param>
        /// <param name="limitAccess">The flag indicates whether WU/WSUS should be contacted as a source location for downloading the payload of a capability. If payload of the capability to be added exists, the flag is ignored.</param>
        /// <param name="sourcePaths">A list of source locations. The function shall look up removed payload files from the locations specified in SourcePaths, and if not found, continue the search by contacting WU/WSUS depending on parameter LimitAccess.</param>
        /// <param name="progressCallback">A progress callback method to invoke when progress is made.</param>
        /// <param name="userData">Optional user data to pass to the DismProgressCallback method.</param>
        /// <exception cref="DismException">When a failure occurs.</exception>
        /// <exception cref="DismRebootRequiredException">When the operation requires a reboot to complete.</exception>
        public static void AddCapability(DismSession session, string capabilityName, bool limitAccess, List <string> sourcePaths, Microsoft.Dism.DismProgressCallback progressCallback, object userData)
        {
            // Get the list of source paths as an array
            string[] sourcePathsArray = sourcePaths?.ToArray() ?? new string[0];

            // Create a DismProgress object to wrap the callback and allow cancellation
            DismProgress progress = new DismProgress(progressCallback, userData);

            int hresult = NativeMethods.DismAddCapability(session, capabilityName, limitAccess, sourcePathsArray, (uint)sourcePathsArray.Length, progress.EventHandle, progress.DismProgressCallbackNative, IntPtr.Zero);

            DismUtilities.ThrowIfFail(hresult, session);
        }
예제 #13
0
        /// <summary>
        /// Unmounts a Windows image from a specified location.
        /// </summary>
        /// <param name="mountPath">A relative or absolute path to the mount directory of the image.</param>
        /// <param name="commitChanges">Specifies whether or not the changes to the image should be saved.</param>
        /// <param name="progressCallback">A progress callback method to invoke when progress is made.</param>
        /// <param name="userData">Optional user data to pass to the DismProgressCallback method.</param>
        /// <exception cref="DismException">When a failure occurs.</exception>
        /// <exception cref="OperationCanceledException">When the user requested the operation be canceled.</exception>
        public static void UnmountImage(string mountPath, bool commitChanges, Dism.DismProgressCallback progressCallback, object userData)
        {
            // Determine flags
            var flags = commitChanges ? DismApi.DISM_COMMIT_IMAGE : DismApi.DISM_DISCARD_IMAGE;

            // Create a DismProgress object to wrap the callback and allow cancellation
            var progress = new DismProgress(progressCallback, userData);

            int hresult = NativeMethods.DismUnmountImage(mountPath, flags, progress.EventHandle, progress.DismProgressCallbackNative, IntPtr.Zero);

            DismUtilities.ThrowIfFail(hresult);
        }
예제 #14
0
        /// <summary>
        /// Reloads the session by closing the current session and opening it again.
        /// </summary>
        internal void Reload()
        {
            if (!IsInvalid)
            {
                DismApi.NativeMethods.DismCloseSession(handle);
            }

            int hresult = DismApi.NativeMethods.DismOpenSession(_imagePath, _windowsDirectory, _systemDrive, out IntPtr sessionPtr);

            DismUtilities.ThrowIfFail(hresult);

            SetHandle(sessionPtr);
        }
예제 #15
0
        /// <summary>
        /// Initializes DISM API. Initialize must be called once per process before calling any other DISM API functions.
        /// </summary>
        /// <param name="logLevel">Indicates the level of logging.</param>
        /// <param name="logFilePath">A relative or absolute path to a log file. All messages generated will be logged to this path. If NULL, the default log path, %windir%\Logs\DISM\dism.log, will be used.</param>
        /// <param name="scratchDirectory">A relative or absolute path to a scratch directory. DISM API will use this directory for internal operations. If null, the default temp directory, \Windows\%Temp%, will be used.</param>
        /// <exception cref="DismException">When a failure occurs.</exception>
        public static void Initialize(DismLogLevel logLevel, string logFilePath, string scratchDirectory)
        {
            lock (InitializeShutDownLock)
            {
                if (!_isInitialized)
                {
                    int hresult = NativeMethods.DismInitialize(logLevel, logFilePath, scratchDirectory);

                    DismUtilities.ThrowIfFail(hresult);

                    _isInitialized = true;
                }
            }
        }
예제 #16
0
        /// <summary>
        /// Gets information about app packages (.appx) in an image that will be installed for each new user.
        /// </summary>
        /// <param name="session">A valid DISM Session.</param>
        /// <returns>A <see cref="DismAppxPackageCollection" /> object containing a collection of <see cref="DismAppxPackage" /> objects.</returns>
        /// <exception cref="DismException">When a failure occurs.</exception>
        /// <exception cref="DismRebootRequiredException">When the operation requires a reboot to complete.</exception>
        public static DismAppxPackageCollection GetProvisionedAppxPackages(DismSession session)
        {
            int hresult = NativeMethods._DismGetProvisionedAppxPackages(session, out IntPtr appxPackagesPtr, out UInt32 appxPackagesCount);

            try
            {
                DismUtilities.ThrowIfFail(hresult, session);

                return(new DismAppxPackageCollection(appxPackagesPtr, appxPackagesCount));
            }
            finally
            {
                Delete(appxPackagesPtr);
            }
        }
예제 #17
0
        /// <summary>
        /// Gets DISM capabilities.
        /// </summary>
        /// <param name="session">A valid DismSession. The DismSession must be associated with an image. You can associate a session with an image by using the <see cref="OpenOfflineSession(string)" /> method.</param>
        /// <returns>A <see cref="DismCapabilityCollection" /> object containing a collection of <see cref="DismCapability" /> objects.</returns>
        /// <exception cref="DismException">When a failure occurs.</exception>
        public static DismCapabilityCollection GetCapabilities(DismSession session)
        {
            int hresult = NativeMethods.DismGetCapabilities(session, out IntPtr capabilityPtr, out UInt32 capabilityCount);

            try
            {
                DismUtilities.ThrowIfFail(hresult, session);

                return(new DismCapabilityCollection(capabilityPtr, capabilityCount));
            }
            finally
            {
                Delete(capabilityPtr);
            }
        }
예제 #18
0
        /// <summary>
        /// Gets the parent features of a specified feature.
        /// </summary>
        /// <param name="session">A valid DISM Session. The DISM Session must be associated with an image. You can associate a session with an image by using the OpenSession Function.</param>
        /// <param name="featureName">The name of the feature that you want to find the parent of.</param>
        /// <param name="identifier">Either an absolute path to a .cab file or the package name, depending on the PackageIdentifier parameter value.</param>
        /// <param name="packageIdentifier">Optional. A valid DismPackageIdentifier Enumeration value.</param>
        /// <returns>A <see cref="DismFeatureCollection" /> object containing a collection of <see cref="DismFeature" /> objects.</returns>
        private static DismFeatureCollection GetFeatureParent(DismSession session, string featureName, string identifier, DismPackageIdentifier packageIdentifier)
        {
            int hresult = NativeMethods.DismGetFeatureParent(session, featureName, identifier, packageIdentifier, out IntPtr featurePtr, out UInt32 featureCount);

            try
            {
                DismUtilities.ThrowIfFail(hresult, session);

                return(new DismFeatureCollection(featurePtr, featureCount));
            }
            finally
            {
                // Clean up
                Delete(featurePtr);
            }
        }
예제 #19
0
        /// <summary>
        /// Initializes DISM API, using the specified DISM Generation. Initialize must be called once per process before calling any other DISM API functions.
        /// </summary>
        /// <param name="logLevel">Indicates the level of logging.</param>
        /// <param name="logFilePath">A relative or absolute path to a log file. All messages generated will be logged to this path. If NULL, the default log path, %windir%\Logs\DISM\dism.log, will be used.</param>
        /// <param name="scratchDirectory">A relative or absolute path to a scratch directory. DISM API will use this directory for internal operations. If null, the default temp directory, \Windows\%Temp%, will be used.</param>
        /// <param name="dismGeneration">The DISM Generational Library to be used.</param>
        /// /// <exception cref="Exception">If an error occurs loading the latest DISM Generational Library.</exception>
        /// <exception cref="DismException">When a failure occurs.</exception>
        public static void InitializeEx(DismLogLevel logLevel, string logFilePath, string scratchDirectory, DismGeneration dismGeneration)
        {
            if (CurrentDismGeneration != DismGeneration.NotFound)
            {
                throw new Exception($"A DISM Generation library is already loaded ({dismGeneration}). Please call Shutdown() first to release the existing library.");
            }

            if (dismGeneration != DismGeneration.NotFound && !DismUtilities.LoadDismGenerationLibrary(dismGeneration))
            {
                throw new Exception($"Loading the latest DISM Generation library ({dismGeneration}) failed.");
            }

            DismApi.Initialize(logLevel, logFilePath, scratchDirectory);

            CurrentDismGeneration = dismGeneration;
        }
예제 #20
0
        /// <summary>
        /// Gets a collection of images contained in the specified .wim or .vhd file.
        /// </summary>
        /// <param name="imageFilePath">// Clean up</param>
        /// <returns>A <see cref="DismImageInfoCollection" /> object containing a collection of <see cref="DismImageInfo" /> objects.</returns>
        /// <exception cref="DismException">When a failure occurs.</exception>
        public static DismImageInfoCollection GetImageInfo(string imageFilePath)
        {
            int hresult = NativeMethods.DismGetImageInfo(imageFilePath, out IntPtr imageInfoPtr, out UInt32 imageInfoCount);

            try
            {
                DismUtilities.ThrowIfFail(hresult);

                return(new DismImageInfoCollection(imageInfoPtr, imageInfoCount));
            }
            finally
            {
                // Clean up
                Delete(imageInfoPtr);
            }
        }
예제 #21
0
        /// <summary>
        /// Gets a list of images that are currently mounted.
        /// </summary>
        /// <returns>A <see cref="DismMountedImageInfoCollection" /> object containing a collection of <see cref="DismMountedImageInfo" /> objects.</returns>
        /// <exception cref="DismException">When a failure occurs.</exception>
        public static DismMountedImageInfoCollection GetMountedImages()
        {
            int hresult = NativeMethods.DismGetMountedImageInfo(out IntPtr mountedImageInfoPtr, out UInt32 mountedImageInfoCount);

            try
            {
                DismUtilities.ThrowIfFail(hresult);

                return(new DismMountedImageInfoCollection(mountedImageInfoPtr, mountedImageInfoCount));
            }
            finally
            {
                // Clean up
                Delete(mountedImageInfoPtr);
            }
        }
예제 #22
0
        /// <summary>
        /// Gets the drivers in an image.
        /// </summary>
        /// <param name="session">A valid DISM Session. The DISM Session must be associated with an image. You can associate a session with an image by using the <see cref="OpenOfflineSession(string)" /> method.</param>
        /// <param name="allDrivers">true or false to specify to retrieve all drivers or just out-of-box drivers.</param>
        /// <returns>A <see cref="DismDriverPackageCollection" /> object containing a collection of <see cref="DismDriverPackage" /> objects.</returns>
        /// <exception cref="DismException">When a failure occurs.</exception>
        public static DismDriverPackageCollection GetDrivers(DismSession session, bool allDrivers)
        {
            int hresult = NativeMethods.DismGetDrivers(session, allDrivers, out IntPtr driverPackagePtr, out UInt32 driverPackageCount);

            try
            {
                DismUtilities.ThrowIfFail(hresult, session);

                return(new DismDriverPackageCollection(driverPackagePtr, driverPackageCount));
            }
            finally
            {
                // Clean up
                Delete(driverPackagePtr);
            }
        }
예제 #23
0
        /// <summary>
        /// Gets a collection of each package in an image and provides basic information about each package, such as the package name and type of package.
        /// </summary>
        /// <param name="session">A valid DISM Session. The DismSession must be associated with an image.</param>
        /// <returns>A <see cref="DismPackageCollection" /> object containing a collection of <see cref="DismPackage" />objects.</returns>
        /// <exception cref="DismException">When a failure occurs.</exception>
        public static DismPackageCollection GetPackages(DismSession session)
        {
            int hresult = NativeMethods.DismGetPackages(session, out IntPtr packagePtr, out UInt32 packageCount);

            try
            {
                DismUtilities.ThrowIfFail(hresult, session);

                return(new DismPackageCollection(packagePtr, packageCount));
            }
            finally
            {
                // Clean up
                Delete(packagePtr);
            }
        }
        /// <summary>
        /// Gets detailed information for the specified feature.
        /// </summary>
        /// <param name="session">A valid DISM Session. The DISM Session must be associated with an image. You can associate a session with an image by using the DismOpenSession Function.</param>
        /// <param name="featureName">The name of the feature that you want to get more information about.</param>
        /// <param name="identifier">Either an absolute path to a .cab file or the package name, depending on the packageIdentifier parameter value.</param>
        /// <param name="packageIdentifier">A valid DismPackageIdentifier Enumeration value.</param>
        /// <returns>A <see cref="DismFeatureInfo"/> object.</returns>
        private static DismFeatureInfo GetFeatureInfo(DismSession session, string featureName, string identifier, DismPackageIdentifier packageIdentifier)
        {
            int hresult = NativeMethods.DismGetFeatureInfo(session, featureName, identifier, packageIdentifier, out IntPtr featureInfoPtr);

            try
            {
                DismUtilities.ThrowIfFail(hresult, session);

                // Return a new DismFeatureInfo from the native pointer
                return(new DismFeatureInfo(featureInfoPtr));
            }
            finally
            {
                // Clean up the native pointer
                DismApi.Delete(featureInfoPtr);
            }
        }
예제 #25
0
        /// <summary>
        /// Adds an app package (.appx) that will install for each new user to a Windows image.
        /// </summary>
        /// <param name="session">A valid DISM Session.</param>
        /// <param name="appPath">Specifies the location of the app package (.appx) to add to the Windows image.</param>
        /// <param name="dependencyPackages">Specifies the location of dependency packages.</param>
        /// <param name="optionalPackages">Specifies the location of optional packages.</param>
        /// <param name="licensePaths">Specifies the locations of .xml files containing your application licenses.</param>
        /// <param name="customDataPath">Specifies the location of a custom data file. The custom data file will be renamed custom.data and saved in the app data store.</param>
        /// <param name="regions">Specifies regions for the package.</param>
        /// <exception cref="DismRebootRequiredException">When the operation requires a reboot to complete.</exception>
        public static void AddProvisionedAppxPackage(DismSession session, string appPath, List <string> dependencyPackages, List <string> optionalPackages, List <string> licensePaths, string customDataPath, string regions)
        {
            int hresult = NativeMethods._DismAddProvisionedAppxPackage(
                session,
                appPath,
                dependencyPackages?.ToArray(),
                (uint)(dependencyPackages?.Count ?? 0),
                optionalPackages?.ToArray(),
                (uint)(optionalPackages?.Count ?? 0),
                licensePaths?.ToArray(),
                (uint)(licensePaths?.Count ?? 0),
                licensePaths == null || licensePaths.Count == 0,
                customDataPath,
                regions);

            DismUtilities.ThrowIfFail(hresult, session);
        }
예제 #26
0
        /// <summary>
        /// Gets extended information about a package.
        /// </summary>
        /// <param name="session">A valid DISM Session. The DISM Session must be associated with an image. You can associate a session with an image by using the OpenImageSession Function.</param>
        /// <param name="identifier">Either an absolute path to a .cab file or the package name, depending on the PackageIdentifier parameter value.</param>
        /// <param name="packageIdentifier">A valid DismPackageIdentifier Enumeration value.</param>
        /// <returns>A <see cref="DismPackageInfo" /> object.</returns>
        private static DismPackageInfo GetPackageInfo(DismSession session, string identifier, DismPackageIdentifier packageIdentifier)
        {
            int hresult = NativeMethods.DismGetPackageInfo(session, identifier, packageIdentifier, out IntPtr packageInfoPtr);

            try
            {
                DismUtilities.ThrowIfFail(hresult, session);

                // Return a new DismPackageInfo object with a reference to the pointer
                return(new DismPackageInfo(packageInfoPtr));
            }
            finally
            {
                // Clean up
                Delete(packageInfoPtr);
            }
        }
예제 #27
0
        /// <summary>
        /// Gets information about an .inf file in a specified image.
        /// </summary>
        /// <param name="session">A valid DISM Session. The DISM Session must be associated with an image. You can associate a session with an image by using the <see cref="OpenOfflineSession(string)" /> method.</param>
        /// <param name="driverPath">A relative or absolute path to the driver .inf file.</param>
        /// <returns>A <see cref="DismDriverCollection" /> object containing a collection of <see cref="DismDriver" /> objects.</returns>
        /// <exception cref="DismException">When a failure occurs.</exception>
        public static DismDriverCollection GetDriverInfo(DismSession session, string driverPath)
        {
            int hresult = NativeMethods.DismGetDriverInfo(session, driverPath, out IntPtr driverInfoPtr, out UInt32 driverInfoCount, out IntPtr driverPackagePtr);

            try
            {
                DismUtilities.ThrowIfFail(hresult, session);

                return(new DismDriverCollection(driverInfoPtr, driverInfoCount));
            }
            finally
            {
                // Clean up
                Delete(driverInfoPtr);
                Delete(driverPackagePtr);
            }
        }
예제 #28
0
        /// <summary>
        /// Gets DISM capability info.
        /// </summary>
        /// <param name="session">A valid DismSession. The DismSession must be associated with an image. You can associate a session with an image by using the <see cref="OpenOfflineSession(string)" /> method.</param>
        /// <param name="capabilityName">The name of the specified capability.</param>
        /// <returns>A <see cref="DismCapabilityInfo" /> object.</returns>
        /// <exception cref="DismException">When a failure occurs.</exception>
        public static DismCapabilityInfo GetCapabilityInfo(DismSession session, string capabilityName)
        {
            int hresult = NativeMethods.DismGetCapabilityInfo(session, capabilityName, out IntPtr capabilityInfoPtr);

            try
            {
                DismUtilities.ThrowIfFail(hresult, session);

                // Return a new DismCapabilityInfo from the native pointer
                return(new DismCapabilityInfo(capabilityInfoPtr));
            }
            finally
            {
                // Clean up the native pointer
                Delete(capabilityInfoPtr);
            }
        }
예제 #29
0
        /// <summary>
        /// Gets information about app packages (.appx) in an image that will be installed for each new user.
        /// </summary>
        /// <param name="session">A valid DISM Session.</param>
        /// <returns>A <see cref="DismAppxPackageCollection"/> object containing a collection of <see cref="DismAppxPackage"/> objects.</returns>
        /// <exception cref="DismException">When a failure occurs.</exception>
        /// <exception cref="DismRebootRequiredException">When the operation requires a reboot to complete.</exception>
        public static DismAppxPackageCollection GetProvisionedAppxPackages(DismSession session)
        {
            var appxPackages = new DismAppxPackageCollection();

            int hresult = NativeMethods._DismGetProvisionedAppxPackages(session, out IntPtr appxPackagesPtr, out UInt32 appxPackagesCount);

            try
            {
                DismUtilities.ThrowIfFail(hresult, session);

                appxPackages.AddRange <DismApi.DismAppxPackage_>(appxPackagesPtr, (int)appxPackagesCount, i => new DismAppxPackage(i));
            }
            finally
            {
                DismApi.Delete(appxPackagesPtr);
            }

            return(appxPackages);
        }
예제 #30
0
        /// <summary>
        /// Shuts down DISM API. Shutdown must be called once per process. Other DISM API function calls will fail after Shutdown has been called.
        /// </summary>
        public static void Shutdown()
        {
            lock (InitializeShutDownLock)
            {
                if (_isInitialized)
                {
                    if (CurrentDismGeneration != DismGeneration.NotFound)
                    {
                        DismUtilities.UnloadDismGenerationLibrary();
                        CurrentDismGeneration = DismGeneration.NotFound;
                    }

                    int hresult = NativeMethods.DismShutdown();

                    DismUtilities.ThrowIfFail(hresult);

                    _isInitialized = false;
                }
            }
        }