Example #1
0
 /// <summary>
 /// Adds third party drivers (.inf) from the specified directory to an offline 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 <see cref="OpenOfflineSession(string)" /> method.</param>
 /// <param name="driverDirectory">A relative or absolute path to a directory containing driver .inf files.</param>
 /// <param name="forceUnsigned">Indicates whether to accept unsigned drivers to an x64-based image. Unsigned drivers will automatically be added to an x86-based image.</param>
 /// <param name="recursive"><code>true</code> to search recursively for driver files, otherwise <code>false</code>.</param>
 /// <exception cref="DirectoryNotFoundException">The directory specified by the <paramref name="driverDirectory" /> parameter does not exist.</exception>
 public static void AddDriversEx(DismSession session, string driverDirectory, bool forceUnsigned, bool recursive)
 {
     foreach (string driverPath in Directory.EnumerateFiles(driverDirectory, "*.inf", recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly))
     {
         AddDriver(session, driverPath, forceUnsigned);
     }
 }
Example #2
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);
        }
Example #3
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);
        }
Example #4
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);
        }
Example #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);
        }
Example #6
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);
        }
        /// <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);
        }
Example #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);
        }
        /// <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);
        }
Example #10
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);
        }
Example #11
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);
        }
Example #13
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="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)
 {
     AddProvisionedAppxPackage(
         session,
         appPath,
         dependencyPackages,
         null,
         string.IsNullOrEmpty(licensePath) ? null : new List <string> {
         licensePath
     },
         customDataPath,
         null);
 }
Example #14
0
        /// <summary>
        /// Throws an exception if the specified function fails.
        /// </summary>
        /// <param name="hresult">An HRESULT value from a function return to check.</param>
        /// <param name="session">An optional <see cref="DismSession"/> to reload if necessary.</param>
        internal static void ThrowIfFail(int hresult, DismSession session = null)
        {
            if (hresult == DismApi.DISMAPI_S_RELOAD_IMAGE_SESSION_REQUIRED && session != null)
            {
                // Reload the session if necessary
                session.Reload();

                return;
            }

            if (hresult != DismApi.ERROR_SUCCESS)
            {
                throw DismException.GetDismExceptionForHResult(hresult);
            }
        }
Example #15
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);
            }
        }
Example #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);
            }
        }
Example #17
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);
            }
        }
Example #18
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);
            }
        }
Example #19
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);
            }
        }
        /// <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);
            }
        }
Example #21
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);
            }
        }
Example #22
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);
            }
        }
Example #23
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);
            }
        }
Example #24
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);
        }
Example #25
0
 public static extern int _DismAddProvisionedAppxPackage(
     DismSession Session,
     [MarshalAs(UnmanagedType.LPWStr)]
     string AppPath,
     [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPWStr, SizeParamIndex = 3)]
     string[] DependencyPackages,
     uint DependencyPackageCount,
     [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPWStr, SizeParamIndex = 5)]
     string[] OptionalPackages,
     uint OptionalPackageCount,
     [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPWStr, SizeParamIndex = 7)]
     string[] LicensePaths,
     uint LicensePathCount,
     bool SkipLicense,
     [MarshalAs(UnmanagedType.LPWStr)]
     string CustomDataPath,
     [MarshalAs(UnmanagedType.LPWStr)]
     string Regions);
Example #26
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);
        }
        /// <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)
        {
            DismCapabilityCollection capabilities = new DismCapabilityCollection();

            int hresult = NativeMethods.DismGetCapabilities(session, out IntPtr capabilityPtr, out UInt32 capabilityCount);

            try
            {
                DismUtilities.ThrowIfFail(hresult, session);

                // Add the items
                capabilities.AddRange <DismCapability_>(capabilityPtr, (int)capabilityCount, c => new DismCapability(c));
            }
            finally
            {
                DismApi.Delete(capabilityPtr);
            }

            return(capabilities);
        }
Example #28
0
        /// <summary>
        /// Throws an exception if the specified function fails.
        /// </summary>
        /// <param name="hresult">An HRESULT value from a function return to check.</param>
        /// <param name="session">An optional <see cref="DismSession" /> to reload if necessary.</param>
        /// <param name="callerMemberName">The name of the calling member.</param>
        internal static void ThrowIfFail(int hresult, DismSession session = null, [CallerMemberName] string callerMemberName = null)
        {
            if (hresult == DismApi.DISMAPI_S_RELOAD_IMAGE_SESSION_REQUIRED)
            {
                if (session == null)
                {
                    throw new DismException(hresult, $"The {callerMemberName} function returned {nameof(DismApi.DISMAPI_S_RELOAD_IMAGE_SESSION_REQUIRED)} but was not passed a session to reload.");
                }

                // Reload the session if necessary
                session.Reload();

                return;
            }

            if (hresult != DismApi.ERROR_SUCCESS)
            {
                throw DismException.GetDismExceptionForHResult(hresult) ?? new DismException(hresult, $"The {callerMemberName} function returned the error code 0x{hresult:X8}");
            }
        }
        /// <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)
        {
            var features = new DismFeatureCollection();

            int hresult = NativeMethods.DismGetFeatureParent(session, featureName, identifier, packageIdentifier, out IntPtr featurePtr, out UInt32 featureCount);

            try
            {
                DismUtilities.ThrowIfFail(hresult, session);

                // Add the items
                features.AddRange <DismApi.DismFeature_>(featurePtr, (int)featureCount, i => new DismFeature(i));
            }
            finally
            {
                // Clean up
                DismApi.Delete(featurePtr);
            }

            return(features);
        }
        /// <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)
        {
            var driverPackages = new DismDriverPackageCollection();

            int hresult = NativeMethods.DismGetDrivers(session, allDrivers, out IntPtr driverPackagePtr, out UInt32 driverPackageCount);

            try
            {
                DismUtilities.ThrowIfFail(hresult, session);

                // Add the items
                driverPackages.AddRange <DismApi.DismDriverPackage_>(driverPackagePtr, (int)driverPackageCount, i => new DismDriverPackage(i));
            }
            finally
            {
                // Clean up
                DismApi.Delete(driverPackagePtr);
            }

            return(driverPackages);
        }