winget-cli

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

WinGetManifest.cs (4321B)


      1 // -----------------------------------------------------------------------
      2 // <copyright file="WinGetManifest.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 class around WinGetUtil manifest native implementation.
     17     /// For dll entry points are defined here:
     18     ///     https://github.com/microsoft/winget-cli/blob/master/src/WinGetUtil/WinGetUtil.h.
     19     /// </summary>
     20     public sealed class WinGetManifest : IWinGetManifest
     21     {
     22         private readonly IntPtr manifestHandle;
     23 
     24         /// <summary>
     25         /// Initializes a new instance of the <see cref="WinGetManifest"/> class.
     26         /// </summary>
     27         /// <param name="manifestHandle">Handle of the manifest.</param>
     28         internal WinGetManifest(IntPtr manifestHandle)
     29         {
     30             this.manifestHandle = manifestHandle;
     31         }
     32 
     33         /// <inheritdoc/>
     34         public ManifestValidationResult ValidateManifestV3(
     35             IntPtr indexHandle,
     36             WinGetValidateManifestOptionV2 option,
     37             WinGetValidateManifestOperationType operationType)
     38         {
     39             try
     40             {
     41                 WinGetValidateManifestV3(
     42                     this.manifestHandle,
     43                     indexHandle,
     44                     out WinGetValidateManifestResult result,
     45                     out string failureOrWarningMessage,
     46                     option,
     47                     operationType);
     48 
     49                 return new ManifestValidationResult(result == WinGetValidateManifestResult.Success, failureOrWarningMessage, result);
     50             }
     51             catch (Exception e)
     52             {
     53                 throw new WinGetManifestException(e);
     54             }
     55         }
     56 
     57         /// <summary>
     58         /// Dispose method.
     59         /// </summary>
     60         public void Dispose()
     61         {
     62             this.Dispose(true);
     63             GC.SuppressFinalize(this);
     64         }
     65 
     66         /// <summary>
     67         /// Dispose method to free the manifest handle.
     68         /// </summary>
     69         /// <param name="disposing">Bool value indicating if Dispose is being run.</param>
     70         public void Dispose(bool disposing)
     71         {
     72             if (disposing)
     73             {
     74                 if (this.manifestHandle != IntPtr.Zero)
     75                 {
     76                     WinGetCloseManifest(this.manifestHandle);
     77                 }
     78             }
     79         }
     80 
     81         /// <summary>
     82         /// Closes the manifest handle.
     83         /// </summary>
     84         /// <param name="manifest">Handle of the manifest.</param>
     85         /// <returns>HRESULT.</returns>
     86         [DllImport(Constants.DllName, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode, PreserveSig = false)]
     87         private static extern IntPtr WinGetCloseManifest(IntPtr manifest);
     88 
     89         /// <summary>
     90         /// Validates a given manifest. Returns an enum for validation result and
     91         /// a string representing validation errors if validation failed.
     92         /// </summary>
     93         /// <param name="manifestHandle">Handle of the manifest.</param>
     94         /// <param name="indexHandle">Handle of the index.</param>
     95         /// <param name="result">Out validation result.</param>
     96         /// <param name="failureMessage">Out string failure message, if any.</param>
     97         /// <param name="option">Validate manifest option.</param>
     98         /// <param name="operationType">Validate manifest operation type.</param>
     99         /// <returns>HRESULT.</returns>
    100         [DllImport(Constants.DllName, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode, PreserveSig = false)]
    101         private static extern IntPtr WinGetValidateManifestV3(
    102             IntPtr manifestHandle,
    103             IntPtr indexHandle,
    104             out WinGetValidateManifestResult result,
    105             [MarshalAs(UnmanagedType.BStr)] out string failureMessage,
    106             WinGetValidateManifestOptionV2 option,
    107             WinGetValidateManifestOperationType operationType);
    108     }
    109 }