private bool RemoveProvisionedPackage(bool online, string packageName)
        {
            var success = false;

            try
            {
                try
                {
                    DismOpenSession();
                    int hr = DismInterop._DismRemoveProvisionedAppxPackage(this.Session, packageName);
                    success = (hr == 0);
                    Log(true, "Dism: Remove-ProvisionedAppxPackage: {0}", LogIntResult(hr));
                }
                catch (Exception ex)
                {
                    Utilities.Logging.Logger.Log(ex, "Exception while removing provisioned app package");
                    success = false;
                }
                finally
                {
                    DismCloseSession();
                }
            }
            catch (Exception ex)
            {
                Logger.Log(ex, "Exception while removing provisioned app package");
            }
            return(success);
        }
 private void DismCloseSession()
 {
     try
     {
         if (_isSessionOpened)
         {
             int hr = DismInterop.DismCloseSession(this.Session);
             Log(true, "DISM: Close Session: {0}", LogIntResult(hr));
         }
         DismShutdown();
     }
     catch (Exception ex)
     {
         Utilities.Logging.Logger.Log(ex, "DISM CLose session");
     }
 }
 private void DismShutdown()
 {
     if (this.Initialized)
     {
         this.Initialized = false;
         int hr = DismInterop.DismShutdown();
         Log(true, "DISM Shutdown: {0}", LogIntResult(hr));
     }
     if (this.SavedDirectory != null)
     {
         try
         {
             Directory.SetCurrentDirectory(this.SavedDirectory);
         }
         catch (Exception ex)
         {
             Utilities.Logging.Logger.Log(ex, "Dism Shutdown");
         }
     }
 }
        private void DismOpenSession()
        {
            DismInitialize();
            int hr;

            if (this.Online)
            {
                uint sessionToken;
                hr           = DismInterop.DismOpenSession(DismInterop.DismOnlineImage, this.WindowsDirectory, this.SystemDrive, out sessionToken);
                this.Session = sessionToken;
            }
            else
            {
                uint sessionToken;
                hr           = DismInterop.DismOpenSession(this.Path, this.WindowsDirectory, this.SystemDrive, out sessionToken);
                this.Session = sessionToken;
            }
            Log(true, "DISM: Open Session: Session ID: {0}, Result:{1}", this.Session, LogIntResult(hr));
            _isSessionOpened = true;
        }
        private void DismInitialize()
        {
            string currentDirectory;

            try
            {
                currentDirectory = System.IO.Path.GetDirectoryName(this.LogPath);
                if (Directory.Exists(currentDirectory))
                {
                    Directory.SetCurrentDirectory(currentDirectory);
                    this.SavedDirectory = currentDirectory;
                }

                int hr = DismInterop.DismInitialize(LogLevel, this.LogPath, null);
                Log(true, "DISM: Initialize: {0}", LogIntResult(hr));
                Initialized = true;
            }
            catch (Exception ex)
            {
                Logger.Log(ex, "Dism Initialize");
            }
        }
        private bool AddProvisionedPackage(
            bool online,
            Uri packagePath,
            Uri folderPath,
            bool skipLicense,
            IEnumerable <Uri> listOfDependencyPackagePaths,
            Uri licensePath,
            Uri customDataPath)
        {
            bool success = false;

            try
            {
                this.Online = online;
                uint dependencyPackageCount = 0u;

                if (folderPath != null && (packagePath != null || (listOfDependencyPackagePaths != null && listOfDependencyPackagePaths.Any())))
                {
                    Log(true, "Folder path cannot be used along with Packagepath or ListOfDepnendencyPackagepaths");
                }
                if (folderPath == null && packagePath == null)
                {
                    Log(true, "Either the folder or package path must be provied");
                }
                if (skipLicense == true && licensePath != null)
                {
                    Log(true, "Cannot specify both SkipLicense and LicensePath");
                }
                if (listOfDependencyPackagePaths != null)
                {
                    dependencyPackageCount = Convert.ToUInt32(listOfDependencyPackagePaths.Count());
                }
                if (packagePath != null && !File.Exists(packagePath.AbsolutePath))
                {
                    Log(true, "Packagepath must point to a package, not a directory");
                }
                if (folderPath != null && !Directory.Exists(folderPath.AbsolutePath))
                {
                    Log(true, "FolderPath must point to a folder");
                }

                string   pathToAppPackage = string.Empty;
                string[] dependencies     = null;
                string   pathToLicense    = null;
                string   pathToCustomData = null;

                pathToAppPackage = (folderPath != null) ? folderPath.OriginalString : packagePath.OriginalString;
                dependencies     = (listOfDependencyPackagePaths != null && listOfDependencyPackagePaths.Any()) ? listOfDependencyPackagePaths.Select <Uri, string>(u => u.OriginalString).ToArray() : null;
                pathToLicense    = (licensePath != null) ? licensePath.OriginalString : null;
                pathToCustomData = (customDataPath != null) ? customDataPath.OriginalString : null;

                try
                {
                    DismOpenSession();
                    uint sessionToken = this.Session;
                    Log(true, "DISM: Add ProvisionedAppxPackage: (Starting)");
                    Log(true, "Operation started.  Modification may take a minute.  Do not close the window.");
                    int hr = DismInterop._DismAddProvisionedAppxPackage(sessionToken, pathToAppPackage, dependencies, dependencyPackageCount, pathToLicense, skipLicense, pathToCustomData);
                    Log(true, "DISM: Add ProvisionedAppxPackage: (Finished) Result: {0}", LogIntResult(hr));
                    if (hr == 0)
                    {
                        success = true;
                    }
                }
                finally
                {
                    DismCloseSession();
                }
                _progressPresenter.OverallProgress = 100;
            }
            catch (Exception ex)
            {
                Logger.Log(ex, "Exception while adding provisioned appx package");
            }
            Log(true, "DISM: Add ProvisionedAppxPackage: Complete");
            return(success);
        }
 private void DismDelete(IntPtr buffer)
 {
     int hr = DismInterop.DismDelete(buffer);
 }
        private IEnumerable <Model.Packages.PackageInformation> GetProvisionedPackages()
        {
            this.Online = true;
            IntPtr pointerToPackagesBuffer = IntPtr.Zero;
            uint   numberOfPackages        = 0u;
            var    packages = new List <Model.Packages.PackageInformation>();

            try
            {
                DismOpenSession();

                int hr = DismInterop._DismGetProvisionedAppxPackages(this.Session, out pointerToPackagesBuffer, out numberOfPackages);

                IntPtr pointerToBuffer      = pointerToPackagesBuffer;
                int    sizeOfOriginalObject = Marshal.SizeOf(typeof(DismInterop.DismAppxPackage));
                int    itemIndex            = 0;
                while ((long)itemIndex < (long)((ulong)numberOfPackages))
                {
                    try
                    {
                        DismInterop.DismAppxPackage dismAppxPackage = (DismInterop.DismAppxPackage)Marshal.PtrToStructure(pointerToBuffer, typeof(DismInterop.DismAppxPackage));
                        if (dismAppxPackage != null)
                        {
                            int major    = Convert.ToInt32(dismAppxPackage.MajorVersion);
                            int minor    = Convert.ToInt32(dismAppxPackage.MinorVersion);
                            int build    = Convert.ToInt32(dismAppxPackage.Build);
                            int revision = Convert.ToInt32(dismAppxPackage.Revision);
                            packages.Add(new Model.Packages.PackageInformation()
                            {
                                AppVersion            = new Version(major, minor, build, revision),
                                InstallationDirectory = dismAppxPackage.InstallLocation,
                                FullName  = dismAppxPackage.PackageName,
                                Name      = dismAppxPackage.DisplayName,
                                Publisher = dismAppxPackage.PublisherId,
                            });
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.Log(ex, "Reading DismAppPackage out of memory buffer");
                    }
                    pointerToBuffer = new IntPtr(pointerToBuffer.ToInt64() + (long)sizeOfOriginalObject);
                    itemIndex++;
                }// End while

                int i = packages.Count;
            }
            catch (Exception ex)
            {
                Logger.Log(ex, "Getting provisioned appx packages");
            }
            finally
            {
                if (pointerToPackagesBuffer != IntPtr.Zero)
                {
                    DismDelete(pointerToPackagesBuffer);
                }
                DismCloseSession();
            }

            return(packages);
        }