This article provides a step-by-step tutorial for configuring a packaged with external location or unpackaged app so that it can load the Windows App SDK runtime and call Windows App SDK APIs. This tutorial demonstrates this scenario using a basic Console app project, but the steps apply to any unpackaged desktop app that uses the Windows App SDK.
Before completing this tutorial, we recommend that you review Runtime architecture to learn more about the Framework package dependency your app takes when it uses Reunion, and the additional components required to work in an unpackaged app.
Notitie
The dynamic dependencies and bootstrapper APIs fail when called by an elevated process. As a result, Visual Studio should not be launched elevated. See issue for more details.
Follow these instructions to configure a C++ project. Starting in 1.0 Preview 3, you can also configure a C++ project that includes WinUI 3 unpackaged support.
In Visual Studio, create a new C++ Console App project. Name the project DynamicDependenciesTest.
After you create the project, you should have a 'Hello World' C++ console app.
Next, install the Windows App SDK NuGet package in your project.
In Solution Explorer, right-click the References node and choose Manage Nuget Packages.
In the NuGet Package Manager window, select the Include prerelease check box near the top of the window, select the Browse tab, and install one of the following packages:
To install 1.0 Preview 3 or 1.0 Experimental, search for Microsoft.WindowsAppSDK.
To install 0.8 Preview, search for Microsoft.ProjectReunion.
Add the following include files to the top of your DynamicDependenciesTest.cpp file. The mddbootstrap.h header is available via the Windows App SDK NuGet package.
C++
#include<windows.h> #include<MddBootstrap.h>
Next, add this code at the beginning of your main method to call the MddBootstrapInitialize function to initialize the Bootstrapper and handle any errors. This code defines what version of the Windows App SDK the app is dependent upon when initializing the Bootstrapper.
C++
// The following code is for 1.0 Preview 3. If using 1.0 Experimental, // replace with versionTag{ L"experimental1" }. If using version 0.8 Preview,// replace with majorMinorVersion{ 0x00000008 } and versionTag{ L"preview" }. const UINT32 majorMinorVersion{ 0x00010000 };
PCWSTR versionTag{ L"preview3" };
const PACKAGE_VERSION minVersion{};
const HRESULT hr{ MddBootstrapInitialize(majorMinorVersion, versionTag, minVersion) };
// Check the return code for errors. If there is an error, display the result.if (FAILED(hr))
{
wprintf(L"Error 0x%X in MddBootstrapInitialize(0x%08X, %s, %hu.%hu.%hu.%hu)\n",
hr, majorMinorVersion, versionTag, minVersion.Major, minVersion.Minor, minVersion.Build, minVersion.Revision);
return hr;
}
Finally, add this code to display the string Hello World! and call the MddBootstrapShutdown function to uninitialize the Bootstrapper.
C++
std::cout << "Hello World!\n";
// Release the DDLM and clean up.
MddBootstrapShutdown();
Your final code should look like this.
C++
#include<iostream> #include<windows.h> #include<MddBootstrap.h>intmain(){
// Take a dependency on Windows App SDK 1.0 Preview 3. If using 1.0 Experimental,// replace with versionTag{ L"experimental1" }. If using version 0.8 Preview, // replace with majorMinorVersion{ 0x00000008 } and versionTag{ L"preview" }.const UINT32 majorMinorVersion{ 0x00010000 };
PCWSTR versionTag{ L"preview3" };
const PACKAGE_VERSION minVersion{};
const HRESULT hr{ MddBootstrapInitialize(majorMinorVersion, versionTag, minVersion) };
// Check the return code. If there is a failure, display the result.if (FAILED(hr))
{
wprintf(L"Error 0x%X in MddBootstrapInitialize(0x%08X, %s, %hu.%hu.%hu.%hu)\n",
hr, majorMinorVersion, versionTag, minVersion.Major, minVersion.Minor, minVersion.Build, minVersion.Revision);
return hr;
}
std::cout << "Hello World!\n";
// Release the DDLM and clean up.
MddBootstrapShutdown();
}
Press F5 to build and run your app.
Follow these instructions to configure a C# project. Starting in 1.0 Preview 3, you can also configure a C# project that includes WinUI 3 unpackaged support.
In Visual Studio, create a new C# Console Application project. Name the project DynamicDependenciesTest.
Next, configure your project.
In Solution Explorer, right-click your project and choose Edit Project File.
Replace the value of the TargetFramework element with a Target Framework Moniker. For example, use the following if your app targets Windows 10, version 2004.
Change the platform for your solution to x64. The default value in a .NET project is AnyCPU, but WinUI 3 doesn't support that platform.
Select Build > Configuration Manager.
Select the drop-down under Active solution platform and click New to open the New Solution Platform dialog box.
In the drop-down under Type or select the new platform, select x64.
Click OK to close the New Solution Platform dialog box.
In Configuration Manager, click Close.
Install the Windows App SDK NuGet package in your project.
In Solution Explorer, right-click the Dependencies node and choose Manage Nuget Packages.
In the NuGet Package Manager window, select the Include prerelease check box near the top of the window, select the Browse tab, and install the Microsoft.WindowsAppSDK package.
Open the Program.cs code file and replace the default code with the following code.
C#
using System;
using Microsoft.Windows.ApplicationModel.DynamicDependency;
namespaceDynamicDependenciesTest
{
classProgram
{
staticvoidMain(string[] args)
{
Bootstrap.Initialize(0x00010000, "preview3");
Console.WriteLine("Hello World!");
// Release the DDLM and clean up.
Bootstrap.Shutdown();
}
}
}
The bootstrapper API is a native C/C++ API that enables you to use the Windows App SDK APIs in your app. In .NET apps that use the Windows App SDK 1.0 Preview 3 or a later release, you can use the .NET wrapper for the bootstrapper API. This wrapper provides an easier way of calling the bootstrapper API in a .NET app than calling the native C/C++ functions directly. The previous code example calls the static Initialize and Shutdown methods of the Bootstrap class in the .NET wrapper for the bootstrapper API.
To demonstrate that the Windows App SDK runtime components were loaded properly, add some code that uses the ResourceManager class in the Windows App SDK to load a string resource.
Add a new Resources File (.resw) to your project.
With the resources file open in the editor, create a new string resource with the following properties.
Name: Message
Value: Hello World!
Save the resources file.
Open the Program.cs code file and add the following statement to the top of the file:
C#
using Microsoft.Windows.ApplicationModel.Resources;
Replace the Console.WriteLine("Hello World!"); line with the following code.
C#
// Create a resource manager using the resource index generated during build.var manager = new Microsoft.ApplicationModel.Resources.ResourceManager("DynamicDependenciesTest.pri");
// Lookup a string in the RESW file using its name.
Console.WriteLine(manager.MainResourceMap.GetValue("Resources/Message").ValueAsString);
Press F5 to build and run your app. You should see the string Hello World! successfully displayed.
Follow these instructions to configure a C# project that uses the 1.0 Experimental or earlier release of the Windows App SDK.
In Visual Studio, create a new C# Console Application project. Name the project DynamicDependenciesTest.
Next, configure your project.
In Solution Explorer, right-click your project and choose Edit Project File.
Replace the value of the TargetFramework element with a Target Framework Moniker. For example, use the following if your app targets Windows 10, version 2004.
Change the platform for your solution to x64. The default value in a .NET project is AnyCPU, but WinUI 3 doesn't support that platform.
Select Build > Configuration Manager.
Select the drop-down under Active solution platform and click New to open the New Solution Platform dialog box.
In the drop-down under Type or select the new platform, select x64.
Click OK to close the New Solution Platform dialog box.
In Configuration Manager, click Close.
Install the Windows App SDK NuGet package in your project.
In Solution Explorer, right-click the Dependencies node and choose Manage Nuget Packages.
In the NuGet Package Manager window, select the Include prerelease check box near the top of the window, select the Browse tab, and install one of the following packages:
To install 1.0 Experimental, search for Microsoft.WindowsAppSDK.
To install 0.8 Preview, search for Microsoft.ProjectReunion.
Add a new code file named MddBootstrap.cs to your project and add the following code to it. The MddBootstrapInitialize and MddBootstrapShutdown functions shown in this code example are available via the Windows App SDK NuGet package.
In the Program.cs code file, replace the default code with the following code.
C#
using System;
using Microsoft.Windows.ApplicationModel;
namespaceDynamicDependenciesTest
{
classProgram
{
staticvoidMain(string[] args)
{
// Take a dependency on Windows App SDK 1.0 Experimental.// If using version 0.8 Preview, replace with MddBootstrap.Initialize(8, "preview").
MddBootstrap.Initialize(0x00010000, "experimental1");
Console.WriteLine("Hello World!");
// Release the DDLM and clean up.
MddBootstrap.Shutdown();
}
}
}
To demonstrate that the Windows App SDK runtime components were loaded properly, add some code that uses the ResourceManager class in the Windows App SDK to load a string resource.
Add a new Resources File (.resw) to your project.
With the resources file open in the editor, create a new string resource with the following properties.
Name: Message
Value: Hello World!
Save the resources file.
Open the Program.cs code file and add the following statement to the top of the file:
C#
using Microsoft.Windows.ApplicationModel.Resources;
Replace the Console.WriteLine("Hello World!"); line with the following code.
C#
// Create a resource manager using the resource index generated during build.var manager = new ResourceManager("DynamicDependenciesTest.pri");
// Lookup a string in the RESW file using its name.
Console.WriteLine(manager.MainResourceMap.GetValue("Resources/Message").ValueAsString);
Press F5 to build and run your app. You should see the string Hello World! successfully displayed.
De bron voor deze inhoud vindt u op GitHub, waar u ook problemen en pull-aanvragen kunt maken en bekijken. Raadpleeg onze gids voor inzenders voor meer informatie.
Feedback over Windows developer
Windows developer is een opensourceproject. Selecteer een koppeling om feedback te geven:
Maak een .NET-project en leer hoe u pakketten toevoegt en pakketafhankelijkheden in uw project beheert. Gebruik het .NET Core CLI- en NuGet-register om bibliotheken en hulpprogramma's toe te voegen aan uw C#-toepassingen via Visual Studio Code.