winget-cli

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

WinRTHelpers.cs (3263B)


      1 // -----------------------------------------------------------------------------
      2 // <copyright file="WinRTHelpers.cs" company="Microsoft Corporation">
      3 //     Copyright (c) Microsoft Corporation. Licensed under the MIT License.
      4 // </copyright>
      5 // -----------------------------------------------------------------------------
      6 
      7 namespace Microsoft.WinGet.Client.Engine.Helpers
      8 {
      9     using System;
     10     using System.IO;
     11     using System.Runtime.InteropServices;
     12 
     13     /// <summary>
     14     /// Helper class for winrtact.dll calls.
     15     /// </summary>
     16     internal static class WinRTHelpers
     17     {
     18 #if POWERSHELL_WINDOWS
     19         private static readonly string ArchDependencyPath;
     20 
     21         static WinRTHelpers()
     22         {
     23             ArchDependencyPath = Path.Combine(
     24                     Path.GetDirectoryName(
     25                         Path.GetDirectoryName(typeof(WinRTHelpers).Assembly.Location)),
     26                     "SharedDependencies",
     27                     RuntimeInformation.ProcessArchitecture.ToString().ToLower());
     28         }
     29 #endif
     30 
     31         /// <summary>
     32         /// Calls winrtact_Initialize.
     33         /// </summary>
     34         public static void Initialize()
     35         {
     36 #if POWERSHELL_WINDOWS
     37             SetDllDirectoryW(ArchDependencyPath);
     38 
     39             try
     40             {
     41 #endif
     42                 InitializeUndockedRegFreeWinRT();
     43 
     44 #if POWERSHELL_WINDOWS
     45             }
     46             finally
     47             {
     48                 SetDllDirectoryW(null);
     49             }
     50 #endif
     51         }
     52 
     53         /// <summary>
     54         /// Calls WinGetServerManualActivation_CreateInstance.
     55         /// </summary>
     56         /// <param name="clsid">Class id.</param>
     57         /// <param name="iid">IID.</param>
     58         /// <param name="flags">Flags.</param>
     59         /// <param name="instance">Out object.</param>
     60         /// <returns>Result of WinGetServerManualActivation_CreateInstance.</returns>
     61         public static int ManualActivation(Guid clsid, Guid iid, uint flags, out object instance)
     62         {
     63 #if POWERSHELL_WINDOWS
     64             SetDllDirectoryW(ArchDependencyPath);
     65 
     66             try
     67             {
     68 #endif
     69                 return WinGetServerManualActivation_CreateInstance(clsid, iid, flags, out instance);
     70 
     71 #if POWERSHELL_WINDOWS
     72             }
     73             finally
     74             {
     75                 SetDllDirectoryW(null);
     76             }
     77 #endif
     78         }
     79 
     80         [DllImport("winrtact.dll", EntryPoint = "winrtact_Initialize", ExactSpelling = true, PreserveSig = true)]
     81         private static extern void InitializeUndockedRegFreeWinRT();
     82 
     83         [DllImport("winrtact.dll", EntryPoint = "WinGetServerManualActivation_CreateInstance", ExactSpelling = true, PreserveSig = true)]
     84         private static extern int WinGetServerManualActivation_CreateInstance(
     85             [In, MarshalAs(UnmanagedType.LPStruct)] Guid clsid,
     86             [In, MarshalAs(UnmanagedType.LPStruct)] Guid iid,
     87             uint flags,
     88             [Out, MarshalAs(UnmanagedType.IUnknown)] out object instance);
     89 
     90         [DllImport("kernel32.dll", SetLastError = true)]
     91         [return: MarshalAs(UnmanagedType.Bool)]
     92         private static extern bool SetDllDirectoryW([MarshalAs(UnmanagedType.LPWStr)] string? directory);
     93     }
     94 }