winget-cli

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

WinGetInstallerMetadata.cs (3820B)


      1 // -----------------------------------------------------------------------------
      2 // <copyright file="WinGetInstallerMetadata.cs" company="Microsoft Corporation">
      3 //     Copyright (c) Microsoft Corporation. Licensed under the MIT License.
      4 // </copyright>
      5 // -----------------------------------------------------------------------------
      6 
      7 namespace Microsoft.WinGetUtil.Api
      8 {
      9     using System;
     10     using System.Runtime.InteropServices;
     11     using Microsoft.WinGetUtil.Common;
     12     using Microsoft.WinGetUtil.Exceptions;
     13     using Microsoft.WinGetUtil.Interfaces;
     14 
     15     /// <summary>
     16     /// Wrapper for WinGetInstallerMetadata operations.
     17     /// </summary>
     18     public sealed class WinGetInstallerMetadata : IWinGetInstallerMetadata
     19     {
     20         private readonly string outputFilePath;
     21         private IntPtr collectionHandle;
     22 
     23         /// <summary>
     24         /// Initializes a new instance of the <see cref="WinGetInstallerMetadata"/> class.
     25         /// </summary>
     26         /// <param name="collectionHandle">Collection handle.</param>
     27         /// <param name="outputFilePath">Output file path.</param>
     28         internal WinGetInstallerMetadata(IntPtr collectionHandle, string outputFilePath)
     29         {
     30             this.collectionHandle = collectionHandle;
     31             this.outputFilePath = outputFilePath;
     32         }
     33 
     34         /// <inheritdoc />
     35         public void Complete(bool abandon = false)
     36         {
     37             try
     38             {
     39                 this.CompleteInternal(abandon ?
     40                     WinGetCompleteInstallerMetadataCollectionOptions.WinGetCompleteInstallerMetadataCollectionOption_Abandon :
     41                     WinGetCompleteInstallerMetadataCollectionOptions.WinGetCompleteInstallerMetadataCollectionOption_None);
     42                 this.collectionHandle = IntPtr.Zero;
     43             }
     44             catch (Exception e)
     45             {
     46                 throw new WinGetInstallerMetadataException(e);
     47             }
     48         }
     49 
     50         /// <summary>
     51         /// Dispose method.
     52         /// </summary>
     53         public void Dispose()
     54         {
     55             this.Dispose(true);
     56             GC.SuppressFinalize(this);
     57         }
     58 
     59         /// <summary>
     60         /// Dispose method to free the manifest handle.
     61         /// </summary>
     62         /// <param name="disposing">Bool value indicating if Dispose is being run.</param>
     63         public void Dispose(bool disposing)
     64         {
     65             if (disposing)
     66             {
     67                 this.Complete(true);
     68             }
     69         }
     70 
     71         /// <summary>
     72         /// Completes the installer metadata collection process, Always frees the collectionHandle.
     73         /// WinGetCompleteInstallerMetadataCollection must be called exactly once for each call to WinGetBeginInstallerMetadataCollection.
     74         /// </summary>
     75         /// <param name="collectionHandle">Collection Handle.</param>
     76         /// <param name="outputFilePath">Metadata output file path.</param>
     77         /// <param name="options">An enum of type <see cref="WinGetBeginInstallerMetadataCollectionOptions"/>.</param>
     78         /// <returns>Metadata collection handle.</returns>
     79         [DllImport(Constants.DllName, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode, PreserveSig = false)]
     80         private static extern IntPtr WinGetCompleteInstallerMetadataCollection(
     81             IntPtr collectionHandle,
     82             string outputFilePath,
     83             WinGetCompleteInstallerMetadataCollectionOptions options);
     84 
     85         private void CompleteInternal(WinGetCompleteInstallerMetadataCollectionOptions options)
     86         {
     87             if (this.collectionHandle != IntPtr.Zero)
     88             {
     89                 WinGetCompleteInstallerMetadataCollection(
     90                     this.collectionHandle,
     91                     this.outputFilePath,
     92                     options);
     93             }
     94         }
     95     }
     96 }