/// <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) { var packages = new DismPackageCollection(); int hresult = NativeMethods.DismGetPackages(session, out IntPtr packagePtr, out UInt32 packageCount); try { DismUtilities.ThrowIfFail(hresult, session); // Add the items packages.AddRange <DismApi.DismPackage_>(packagePtr, (int)packageCount, i => new DismPackage(i)); } finally { // Clean up DismApi.Delete(packagePtr); } return(packages); }
/// <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) { var imageInfos = new DismImageInfoCollection(); int hresult = NativeMethods.DismGetImageInfo(imageFilePath, out IntPtr imageInfoPtr, out UInt32 imageInfoCount); try { DismUtilities.ThrowIfFail(hresult); // Add the items imageInfos.AddRange <DismApi.DismImageInfo_>(imageInfoPtr, (int)imageInfoCount, i => new DismImageInfo(i)); } finally { // Clean up DismApi.Delete(imageInfoPtr); } return(imageInfos); }
/// <summary> /// Gets a <see cref="DismException" /> or <see cref="Exception" /> for the specified error code. /// </summary> /// <param name="errorCode">The error code to get an exception for.</param> /// <returns>A <see cref="DismException" /> or <see cref="Exception" /> that represents the error code.</returns> internal static Exception GetDismExceptionForHResult(int errorCode) { // Look for known error codes switch ((uint)errorCode) { case DismApi.ERROR_REQUEST_ABORTED: case 0x80070000 ^ DismApi.ERROR_REQUEST_ABORTED: case DismApi.ERROR_CANCELLED: case 0x80070000 ^ DismApi.ERROR_CANCELLED: return(new OperationCanceledException()); case DismApi.ERROR_SUCCESS_REBOOT_REQUIRED: return(new DismRebootRequiredException(errorCode)); case DismApi.DISMAPI_E_DISMAPI_NOT_INITIALIZED: // User has not called DismApi.Initialize() return(new DismNotInitializedException(errorCode)); case DismApi.DISMAPI_E_OPEN_SESSION_HANDLES: // User has not called CloseSession() on open sessions return(new DismOpenSessionsException(errorCode)); case DismApi.CBS_E_NOT_APPLICABLE: return(new DismPackageNotApplicableException(errorCode)); } // Attempt to get an error message from the DismApi string lastError = DismApi.GetLastErrorMessage(); // See if the result is not null if (!string.IsNullOrEmpty(lastError)) { // Return a DismException object return(new DismException(errorCode, lastError.Trim())); } // Return an Exception for the HResult return(Marshal.GetExceptionForHR(errorCode)); }
/// <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) { var driverInfos = new DismDriverCollection(); int hresult = NativeMethods.DismGetDriverInfo(session, driverPath, out IntPtr driverInfoPtr, out UInt32 driverInfoCount, out IntPtr driverPackagePtr); try { DismUtilities.ThrowIfFail(hresult, session); // Add the items driverInfos.AddRange <DismApi.DismDriver_>(driverInfoPtr, (int)driverInfoCount, i => new DismDriver(i)); } finally { // Clean up DismApi.Delete(driverInfoPtr); DismApi.Delete(driverPackagePtr); } return(driverInfos); }
/// <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> /// <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) { DismApi.CommitImage(session, discardChanges, progressCallback, null); }
/// <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> /// <exception cref="DismException">When a failure occurs.</exception> public static void CommitImage(DismSession session, bool discardChanges) { DismApi.CommitImage(session, discardChanges, null); }
/// <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> /// <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) { return(DismApi.CheckImageHealth(session, scanImage, progressCallback, null)); }
/// <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> /// <returns>A <see cref="DismImageHealthState"/> indicating the health state of the image.</returns> /// <exception cref="DismException">When a failure occurs.</exception> public static DismImageHealthState CheckImageHealth(DismSession session, bool scanImage) { return(DismApi.CheckImageHealth(session, scanImage, null)); }