winget-cli

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

main.cpp (6027B)


      1 // Copyright (c) Microsoft Corporation.
      2 // Licensed under the MIT License.
      3 #include <wil/result_macros.h>
      4 #pragma warning( push )
      5 #pragma warning ( disable : 4324 )
      6 #include <wrl/module.h>
      7 #pragma warning( pop )
      8 #include <winrt/Microsoft.Management.Deployment.h>
      9 
     10 #include "WindowsPackageManager.h"
     11 
     12 #include <AppInstallerCLICore.h>
     13 #include <AppInstallerFileLogger.h>
     14 #include <AppInstallerStrings.h>
     15 #include <AppInstallerTelemetry.h>
     16 #include <AppInstallerErrors.h>
     17 #include <winget/GroupPolicy.h>
     18 #include <ShutdownMonitoring.h>
     19 #include <winget/COMStaticStorage.h>
     20 #include <ComClsids.h>
     21 
     22 using namespace winrt::Microsoft::Management::Deployment;
     23 
     24 // CreatorMap for out-of-proc com registration and direct in-proc com class construction
     25 CoCreatableClassWrlCreatorMapInclude(PackageManager);
     26 CoCreatableClassWrlCreatorMapInclude(FindPackagesOptions);
     27 CoCreatableClassWrlCreatorMapInclude(CreateCompositePackageCatalogOptions);
     28 CoCreatableClassWrlCreatorMapInclude(InstallOptions);
     29 CoCreatableClassWrlCreatorMapInclude(UninstallOptions);
     30 CoCreatableClassWrlCreatorMapInclude(DownloadOptions);
     31 CoCreatableClassWrlCreatorMapInclude(PackageMatchFilter);
     32 CoCreatableClassWrlCreatorMapInclude(AuthenticationArguments);
     33 CoCreatableClassWrlCreatorMapInclude(PackageManagerSettings);
     34 CoCreatableClassWrlCreatorMapInclude(RepairOptions);
     35 CoCreatableClassWrlCreatorMapInclude(AddPackageCatalogOptions);
     36 CoCreatableClassWrlCreatorMapInclude(RemovePackageCatalogOptions);
     37 
     38 // Shim for configuration static functions
     39 CoCreatableClassWrlCreatorMapInclude(ConfigurationStaticFunctionsShim);
     40 
     41 extern "C"
     42 {
     43     int WINDOWS_PACKAGE_MANAGER_API_CALLING_CONVENTION WindowsPackageManagerCLIMain(int argc, wchar_t const** argv) try
     44     {
     45         ::Microsoft::WRL::Module<::Microsoft::WRL::ModuleType::InProc>::Create();
     46         return AppInstaller::CLI::CoreMain(argc, argv);
     47     }
     48     CATCH_RETURN();
     49 
     50     WINDOWS_PACKAGE_MANAGER_API WindowsPackageManagerServerInitialize() try
     51     {
     52         AppInstaller::CLI::ServerInitialize();
     53         return S_OK;
     54     }
     55     CATCH_RETURN();
     56 
     57     WINDOWS_PACKAGE_MANAGER_API WindowsPackageManagerServerModuleCreate(WindowsPackageManagerServerModuleTerminationCallback callback) try
     58     {
     59         AppInstaller::ShutdownMonitoring::ServerShutdownSynchronization::Initialize(callback);
     60         ::Microsoft::WRL::Module<::Microsoft::WRL::ModuleType::OutOfProc>::Create(callback);
     61         return S_OK;
     62     }
     63     CATCH_RETURN();
     64 
     65     WINDOWS_PACKAGE_MANAGER_API WindowsPackageManagerServerModuleRegister() try
     66     {
     67         RETURN_HR(::Microsoft::WRL::Module<::Microsoft::WRL::ModuleType::OutOfProc>::GetModule().RegisterObjects());
     68     }
     69     CATCH_RETURN();
     70 
     71     WINDOWS_PACKAGE_MANAGER_API WindowsPackageManagerServerModuleUnregister() try
     72     {
     73         RETURN_HR(::Microsoft::WRL::Module<::Microsoft::WRL::ModuleType::OutOfProc>::GetModule().UnregisterObjects());
     74     }
     75     CATCH_RETURN();
     76 
     77     void WINDOWS_PACKAGE_MANAGER_API_CALLING_CONVENTION WindowsPackageManagerServerWilResultLoggingCallback(const wil::FailureInfo& failure) noexcept try
     78     {
     79         AppInstaller::Logging::Telemetry().LogFailure(failure);
     80     }
     81     CATCH_LOG();
     82 
     83     WINDOWS_PACKAGE_MANAGER_API WindowsPackageManagerServerCreateInstance(REFCLSID rclsid, REFIID riid, void** out) try
     84     {
     85         RETURN_HR_IF_NULL(E_POINTER, out);
     86         ::Microsoft::WRL::ComPtr<IClassFactory> factory;
     87         RETURN_IF_FAILED(::Microsoft::WRL::Module<::Microsoft::WRL::ModuleType::OutOfProc>::GetModule().GetClassObject(rclsid, IID_PPV_ARGS(&factory)));
     88         RETURN_HR(factory->CreateInstance(nullptr, riid, out));
     89     }
     90     CATCH_RETURN();
     91 
     92     WINDOWS_PACKAGE_MANAGER_API WindowsPackageManagerInProcModuleInitialize() try
     93     {
     94         ::Microsoft::WRL::Module<::Microsoft::WRL::ModuleType::InProc>::Create();
     95         AppInstaller::CLI::InProcInitialize();
     96         return S_OK;
     97     }
     98     CATCH_RETURN();
     99 
    100     bool WINDOWS_PACKAGE_MANAGER_API_CALLING_CONVENTION WindowsPackageManagerInProcModuleTerminate()
    101     {
    102         try
    103         {
    104             // The WRL object count is used to track externally visible objects, which largely means objects created with the `wil::details::module_count_wrapper` type wrapper.
    105             // Configuration objects use a composition based tracking that is similar in nature (only when OOP).
    106             // 
    107             // In-proc DllCanUnloadNow should not be blocked by our internal objects, but they must be destroyed on unload or a future reload will attempt to destroy them
    108             // and our module may have moved.  So when we don't have any more objects that we gave to callers, remove all of our static lifetime objects and indicate
    109             // that we can now be unloaded.
    110             if (::Microsoft::WRL::Module<::Microsoft::WRL::ModuleType::InProc>::GetModule().Terminate())
    111             {
    112                 AppInstaller::WinRT::COMStaticStorageStatics::ResetAll();
    113                 return true;
    114             }
    115         }
    116         catch (...) {}
    117 
    118         return false;
    119     }
    120 
    121     WINDOWS_PACKAGE_MANAGER_API WindowsPackageManagerInProcModuleGetClassObject(
    122         REFCLSID rclsid,
    123         REFIID riid,
    124         LPVOID* ppv) try
    125     {
    126         CLSID redirectedClsid = GetRedirectedClsidFromInProcClsid(rclsid);
    127         RETURN_HR_IF(CLASS_E_CLASSNOTAVAILABLE, IsEqualCLSID(redirectedClsid, CLSID_NULL));
    128         RETURN_HR(::Microsoft::WRL::Module<::Microsoft::WRL::ModuleType::InProc>::GetModule().GetClassObject(redirectedClsid, riid, ppv));
    129     }
    130     CATCH_RETURN();
    131 
    132     WINDOWS_PACKAGE_MANAGER_API WindowsPackageManagerInProcModuleGetActivationFactory(HSTRING classId, void** factory) try
    133     {
    134         RETURN_HR_IF(APPINSTALLER_CLI_ERROR_BLOCKED_BY_POLICY, !::AppInstaller::Settings::GroupPolicies().IsEnabled(::AppInstaller::Settings::TogglePolicy::Policy::WinGet));
    135 
    136         return WINRT_GetActivationFactory(classId, factory);
    137     }
    138     CATCH_RETURN();
    139 }