winget-cli

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

Factory.cpp (3996B)


      1 // Copyright (c) Microsoft Corporation.
      2 // Licensed under the MIT License.
      3 #include "pch.h"
      4 #include "Factory.h"
      5 #include <winrt/Microsoft.Management.Configuration.h>
      6 #include <winget/Runtime.h>
      7 #include <WinGetServerManualActivation_Client.h>
      8 
      9 namespace Microsoft::Management::Configuration::OutOfProc
     10 {
     11     namespace
     12     {
     13         const CLSID& GetConfigurationStaticsCLSID()
     14         {
     15 #if USE_PROD_CLSIDS
     16             static const CLSID CLSID_ConfigurationStatics = { 0x73d763b7,0x2937,0x432f,{0xa9,0x7a,0xd9,0x8a,0x4a,0x59,0x61,0x26} };  // 73D763B7-2937-432F-A97A-D98A4A596126
     17 #else
     18             static const CLSID CLSID_ConfigurationStatics = { 0xc9ed7917,0x66ab,0x4e31,{0xa9,0x2a,0xf6,0x5f,0x18,0xef,0x79,0x33} };  // C9ED7917-66AB-4E31-A92A-F65F18EF7933
     19 #endif
     20 
     21             return CLSID_ConfigurationStatics;
     22         }
     23 
     24         winrt::Microsoft::Management::Configuration::IConfigurationStatics CreateOOPStaticsObject()
     25         {
     26             bool isAdmin = AppInstaller::Runtime::IsRunningAsAdmin();
     27 
     28             try
     29             {
     30                 return winrt::create_instance<winrt::Microsoft::Management::Configuration::IConfigurationStatics>(GetConfigurationStaticsCLSID(), CLSCTX_LOCAL_SERVER | CLSCTX_NO_CODE_DOWNLOAD);
     31             }
     32             catch (const winrt::hresult_error& hre)
     33             {
     34                 // We only want to fall through to trying the manual activation if we are running as admin and couldn't find the registration.
     35                 if (!(isAdmin && hre.code() == REGDB_E_CLASSNOTREG))
     36                 {
     37                     throw;
     38                 }
     39             }
     40 
     41             winrt::com_ptr<::IUnknown> result;
     42             THROW_IF_FAILED(WinGetServerManualActivation_CreateInstance(GetConfigurationStaticsCLSID(), winrt::guid_of<winrt::Microsoft::Management::Configuration::IConfigurationStatics>(), 0, result.put_void()));
     43             return result.as<winrt::Microsoft::Management::Configuration::IConfigurationStatics>();
     44         }
     45     }
     46 
     47     Factory::Factory()
     48     {
     49         IncrementRefCount();
     50     }
     51 
     52     Factory::~Factory()
     53     {
     54         DecrementRefCount();
     55     }
     56 
     57     bool Factory::HasReferences()
     58     {
     59         return s_referenceCount.load() != 0;
     60     }
     61 
     62     void Factory::Terminate()
     63     {
     64         WinGetServerManualActivation_Terminate();
     65     }
     66 
     67     bool Factory::IsCLSID(const GUID& clsid)
     68     {
     69         if (clsid == GetConfigurationStaticsCLSID())
     70         {
     71             return true;
     72         }
     73 
     74         return false;
     75     }
     76 
     77     bool Factory::IsCLSID(HSTRING clsid)
     78     {
     79         constexpr std::wstring_view s_ClassName = L"Microsoft.Management.Configuration.ConfigurationStaticFunctions";
     80 
     81         UINT32 length = 0;
     82         PCWSTR buffer = WindowsGetStringRawBuffer(clsid, &length);
     83 
     84         if (std::wstring_view{ buffer, length } == s_ClassName)
     85         {
     86             return true;
     87         }
     88 
     89         return false;
     90     }
     91 
     92     winrt::Windows::Foundation::IInspectable Factory::ActivateInstance()
     93     {
     94         return CreateOOPStaticsObject().as<winrt::Windows::Foundation::IInspectable>();
     95     }
     96 
     97     HRESULT STDMETHODCALLTYPE Factory::CreateInstance(::IUnknown* pUnkOuter, REFIID riid, void** ppvObject) try
     98     {
     99         RETURN_HR_IF(E_POINTER, !ppvObject);
    100         *ppvObject = nullptr;
    101         RETURN_HR_IF(CLASS_E_NOAGGREGATION, pUnkOuter != nullptr);
    102 
    103         return CreateOOPStaticsObject().as(riid, ppvObject);
    104     }
    105     CATCH_RETURN();
    106 
    107     HRESULT STDMETHODCALLTYPE Factory::LockServer(BOOL fLock)
    108     {
    109         if (fLock)
    110         {
    111             IncrementRefCount();
    112         }
    113         else
    114         {
    115             DecrementRefCount();
    116         }
    117 
    118         return S_OK;
    119     }
    120 
    121     void Factory::IncrementRefCount()
    122     {
    123         ++s_referenceCount;
    124     }
    125 
    126     void Factory::DecrementRefCount()
    127     {
    128         --s_referenceCount;
    129     }
    130 
    131     std::atomic<int32_t> Factory::s_referenceCount = ATOMIC_VAR_INIT(0);
    132 }