Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sample projects of how to use the NuGet packages #5142

Merged
merged 3 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0-windows10.0.22000.0</TargetFramework>
<RootNamespace>Configuration_InProc</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Platforms>x64;x86;ARM64</Platforms>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.WindowsPackageManager.Configuration" Version="1.9.25180" GeneratePathProperty="True" />
</ItemGroup>

<ItemGroup>
<Content Include="$(PkgMicrosoft_WindowsPackageManager_Configuration)\runtimes\win10-$(Platform)\native\Microsoft.Management.Configuration.dll" Link="Microsoft.Management.Configuration.dll" CopyToOutputDirectory="PreserveNewest" />
<Content Include="$(PkgMicrosoft_WindowsPackageManager_Configuration)\runtimes\win10-$(Platform)\native\Microsoft.Management.Configuration.winmd" Link="Microsoft.Management.Configuration.winmd" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

</Project>
30 changes: 30 additions & 0 deletions samples/MinimalCallers/C#/Configuration-InProc/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Shows the contents of a configuration yml file
using Windows.Storage;
using Microsoft.Management.Configuration;
using Microsoft.Management.Configuration.Processor;

if (args.Length != 1)
{
Console.WriteLine("Usage: Configuration-InProc <path>");
return;
}

var configStatics = new ConfigurationStaticFunctions();
if (!configStatics.IsConfigurationAvailable)
{
throw new Exception("Configuration is not available");
}

// Differs from the OutOfProc version in that we create the factory object directly
var factory = new PowerShellConfigurationSetProcessorFactory();

var processor = configStatics.CreateConfigurationProcessor(factory);

var file = await StorageFile.GetFileFromPathAsync(args[0]);
var fileStream = await file.OpenStreamForReadAsync();

var openResult = processor.OpenConfigurationSet(fileStream.AsInputStream());
var configSet = openResult.Set;

Console.WriteLine("Configuration set:");
configSet.Serialize(Console.OpenStandardOutput().AsOutputStream());
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0-windows10.0.22000.0</TargetFramework>
<RootNamespace>Configuration_OutOfProc</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Platforms>x64;x86;ARM64</Platforms>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.WindowsPackageManager.Configuration.OutOfProc" Version="1.9.25180" GeneratePathProperty="True" />
</ItemGroup>

<ItemGroup>
<Content Include="$(PkgMicrosoft_WindowsPackageManager_Configuration_OutOfProc)\runtimes\win10-$(Platform)\native\Microsoft.Management.Configuration.dll" Link="Microsoft.Management.Configuration.dll" CopyToOutputDirectory="PreserveNewest" />
<Content Include="$(PkgMicrosoft_WindowsPackageManager_Configuration_OutOfProc)\runtimes\win10-$(Platform)\native\Microsoft.Management.Configuration.winmd" Link="Microsoft.Management.Configuration.winmd" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

</Project>
28 changes: 28 additions & 0 deletions samples/MinimalCallers/C#/Configuration-OutOfProc/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Shows the contents of a configuration yml file
using Windows.Storage;
using Microsoft.Management.Configuration;

if (args.Length != 1)
{
Console.WriteLine("Usage: Configuration-InProc <path>");
return;
}

var configStatics = new ConfigurationStaticFunctions();
if (!configStatics.IsConfigurationAvailable)
{
throw new Exception("Configuration is not available");
}

var factory = await configStatics.CreateConfigurationSetProcessorFactoryAsync("pwsh");

var processor = configStatics.CreateConfigurationProcessor(factory);

var file = await StorageFile.GetFileFromPathAsync(args[0]);
var fileStream = await file.OpenStreamForReadAsync();

var openResult = processor.OpenConfigurationSet(fileStream.AsInputStream());
var configSet = openResult.Set;

Console.WriteLine("Configuration set:");
configSet.Serialize(Console.OpenStandardOutput().AsOutputStream());
39 changes: 39 additions & 0 deletions samples/MinimalCallers/C#/WinGet-InProc/C#_WinGet_InProc.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0-windows10.0.22000.0</TargetFramework>
<RootNamespace>WinGet_InProc</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Platforms>x64;x86;ARM64</Platforms>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Windows.CsWinRT" Version="2.2.0" />
<PackageReference Include="Microsoft.WindowsPackageManager.InProcCom" Version="1.9.25180">
<!-- NU1701: The package targets .NET Framework, but this uses .NET -->
<NoWarn>NU1701</NoWarn>
<GeneratePathProperty>true</GeneratePathProperty>
<IncludeAssets>none</IncludeAssets>
</PackageReference>
</ItemGroup>

<!-- Tell CsWinRT to create the projection for the API -->
<PropertyGroup>
<CsWinRTWindowsMetadata>10.0.19041.0</CsWinRTWindowsMetadata>
<CsWinRTIncludes>Microsoft.Management.Deployment</CsWinRTIncludes>
</PropertyGroup>
<ItemGroup>
<CsWinRTInputs Include="$(PkgMicrosoft_WindowsPackageManager_InProcCom)\lib\Microsoft.Management.Deployment.winmd" />
</ItemGroup>

<!-- Reference and include necessary binaries -->
<ItemGroup>
<Content Include="$(PkgMicrosoft_WindowsPackageManager_InProcCom)\runtimes\win-$(Platform)\native\Microsoft.Management.Deployment.InProc.dll" Link="Microsoft.Management.Deployment.dll" CopyToOutputDirectory="PreserveNewest" />
<Content Include="$(PkgMicrosoft_WindowsPackageManager_InProcCom)\runtimes\win-$(Platform)\native\Microsoft.Management.Deployment.InProc.dll.manifest" Link="Microsoft.Management.Deployment.dll.manifest" CopyToOutputDirectory="PreserveNewest" />
<Content Include="$(PkgMicrosoft_WindowsPackageManager_InProcCom)\runtimes\win-$(Platform)\native\Microsoft.Management.Deployment.winmd" Link="Microsoft.Management.Deployment.winmd" CopyToOutputDirectory="PreserveNewest" />
<Content Include="$(PkgMicrosoft_WindowsPackageManager_InProcCom)\runtimes\win-$(Platform)\native\WindowsPackageManager.dll" Link="WindowsPackageManager.dll" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

</Project>
25 changes: 25 additions & 0 deletions samples/MinimalCallers/C#/WinGet-InProc/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Lists all installed packages
using Microsoft.Management.Deployment;

var packageManager = new PackageManager();

var installedCatalogConnectResult = packageManager.GetLocalPackageCatalog(LocalPackageCatalog.InstalledPackages).Connect();
if (installedCatalogConnectResult.Status != ConnectResultStatus.Ok)
{
throw new Exception("Error connecting to catalog");
}

var installedCatalog = installedCatalogConnectResult.PackageCatalog;

var findOptions = new FindPackagesOptions();
var searchResult = installedCatalog.FindPackages(findOptions);
if (searchResult.Status != FindPackagesResultStatus.Ok)
{
throw new Exception("Error finding packages");
}

// Can't use foreach due to C#/WinRT limitations
for (int i = 0; i < searchResult.Matches.Count; ++i)
{
Console.WriteLine("Package found: " + searchResult.Matches[i].CatalogPackage.Id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0-windows10.0.22000.0</TargetFramework>
<RootNamespace>WinGet_OutOfProc</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Platforms>x64;ARM64;x86</Platforms>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Windows.CsWinRT" Version="2.2.0" />
<PackageReference Include="Microsoft.WindowsPackageManager.ComInterop" Version="1.9.25180">
<!-- NU1701: The package targets .NET Framework, but this uses .NET -->
<NoWarn>NU1701</NoWarn>
<GeneratePathProperty>true</GeneratePathProperty>
<IncludeAssets>none</IncludeAssets>
</PackageReference>
</ItemGroup>

<!-- Tell CsWinRT to create the projection for the API -->
<PropertyGroup>
<CsWinRTWindowsMetadata>10.0.19041.0</CsWinRTWindowsMetadata>
<CsWinRTIncludes>Microsoft.Management.Deployment</CsWinRTIncludes>
</PropertyGroup>
<ItemGroup>
<CsWinRTInputs Include="$(PkgMicrosoft_WindowsPackageManager_ComInterop)\lib\Microsoft.Management.Deployment.winmd" />
</ItemGroup>

<!-- Reference and include necessary binaries -->
<ItemGroup>
<Content Include="$(PkgMicrosoft_WindowsPackageManager_ComInterop)\lib\Microsoft.Management.Deployment.winmd" Link="Microsoft.Management.Deployment.winmd" CopyToOutputDirectory="PreserveNewest" />
<Content Include="$(PkgMicrosoft_WindowsPackageManager_ComInterop)\runtimes\win10-$(Platform)\native\winrtact.dll" Link="winrtact.dll" CopyToOutputDirectory="PreserveNewest" />
<!-- Replace 'dynamic' with 'static' to use binaries compiled with static linking -->
<Content Include="$(PkgMicrosoft_WindowsPackageManager_ComInterop)\bin\win10-$(Platform)\native\dynamic\Microsoft.Management.Deployment.dll" Link="Microsoft.Management.Deployment.dll" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

</Project>
25 changes: 25 additions & 0 deletions samples/MinimalCallers/C#/WinGet-OutOfProc/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Lists all installed packages
using Microsoft.Management.Deployment;

var packageManager = new PackageManager();

var installedCatalogConnectResult = packageManager.GetLocalPackageCatalog(LocalPackageCatalog.InstalledPackages).Connect();
if (installedCatalogConnectResult.Status != ConnectResultStatus.Ok)
{
throw new Exception("Error connecting to catalog");
}

var installedCatalog = installedCatalogConnectResult.PackageCatalog;

var findOptions = new FindPackagesOptions();
var searchResult = installedCatalog.FindPackages(findOptions);
if (searchResult.Status != FindPackagesResultStatus.Ok)
{
throw new Exception("Error finding packages");
}

// Can't use foreach due to C#/WinRT limitations
for (int i = 0; i < searchResult.Matches.Count; ++i)
{
Console.WriteLine("Package found: " + searchResult.Matches[i].CatalogPackage.Id);
}
54 changes: 54 additions & 0 deletions samples/MinimalCallers/C++/WinGet-InProc/WinGet-InProc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <Windows.h>

#include <winrt/Microsoft.Management.Deployment.h>
#include <winrt/Windows.Foundation.Collections.h>

#include <iostream>

using namespace winrt::Microsoft::Management::Deployment;

const CLSID CLSID_PackageManager = { 0xC53A4F16, 0x787E, 0x42A4, 0xB3, 0x04, 0x29, 0xEF, 0xFB, 0x4B, 0xF5, 0x97 }; //C53A4F16-787E-42A4-B304-29EFFB4BF597
const CLSID CLSID_FindPackagesOptions = { 0x572DED96, 0x9C60, 0x4526, { 0x8F, 0x92, 0xEE, 0x7D, 0x91, 0xD3, 0x8C, 0x1A } }; //572DED96-9C60-4526-8F92-EE7D91D38C1A

// Simple RAII for COM initialization
struct ComInitialization
{
ComInitialization()
{
winrt::check_hresult(CoInitialize(nullptr));
}

~ComInitialization()
{
CoUninitialize();
}
};

// Lists all installed packages
int main()
{
ComInitialization comInitialization;

auto packageManager = winrt::create_instance<PackageManager>(CLSID_PackageManager, CLSCTX_ALL);

auto installedCatalogConnectResult = packageManager.GetLocalPackageCatalog(LocalPackageCatalog::InstalledPackages).Connect();
if (installedCatalogConnectResult.Status() != ConnectResultStatus::Ok)
{
std::wcerr << L"Error connecting to catalog" << std::endl;
return E_FAIL;
}
auto installedCatalog = installedCatalogConnectResult.PackageCatalog();

auto findOptions = winrt::create_instance<FindPackagesOptions>(CLSID_FindPackagesOptions, CLSCTX_ALL);
auto searchResult = installedCatalog.FindPackages(findOptions);
if (searchResult.Status() != FindPackagesResultStatus::Ok)
{
std::wcerr << L"Error finding packages" << std::endl;
return E_FAIL;
}

for (auto package : searchResult.Matches())
{
std::wcout << L"Found package: " << package.CatalogPackage().Id().c_str() << std::endl;
}
}
Loading
Loading