Tutorial: Use the bootstrapper API in an app packaged with external location or unpackaged that uses the Windows App SDK
Artikel
This article shows how to configure an app that's not installed by using MSIX (that is, it's packaged with external location or unpackaged) to use the bootstrapper API so that it explicitly loads the Windows App SDK runtime, and calls Windows App SDK APIs. Apps that are not installed via MSIX include apps packaged with external location, and unpackaged apps.
Vigtigt
Beginning in the Windows App SDK 1.0, the default approach to loading the Windows App SDK from a packaged with external location or unpackaged app is to use auto-initialization via the <WindowsPackageType> project property (as well as making additional configuration changes). For the steps involved in auto-initialization in the context of WinUI 3 project, see Create your first WinUI 3 project. Or, if have an existing project that's not WinUI 3, then see Use the Windows App SDK in an existing project.
This topic demonstrates explicitly calling the bootstrapper API from 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 that your app takes when it uses the Windows App SDK, and the additional components required to work in a packaged with external location or unpackaged app.
You can follow this tutorial using a C# or a C++ project.
Bemærk
The dynamic dependencies and bootstrapper APIs fail when called by an elevated process. As a result, Visual Studio shouldn't be launched elevated. See Dynamic Dependencies doesn't support elevation #567 for more details.
Follow these instructions to configure a C# WinUI 3 project that's either packaged with external location, or unpackaged.
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, 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.
Open the Program.cs code file, and replace the default code with the following code to call the Bootstrap.Initialize method to initialize the bootstrapper. This code defines what version of the Windows App SDK the app is dependent upon when initializing the bootstrapper.
Vigtigt
You'll need to edit the code below to suit your specific configuration. See the descriptions of the parameters of the Bootstrap.Initialize method so that you can specify one of the versions of the Windows App SDK that you have installed.
C#
using System;
using Microsoft.Windows.ApplicationModel.DynamicDependency;
namespaceDynamicDependenciesTest
{
classProgram
{
staticvoidMain(string[] args)
{
Bootstrap.Initialize(0x00010002);
Console.WriteLine("Hello, World!");
// Release the DDLM and clean up.
Bootstrap.Shutdown();
}
}
}
At its root, the bootstrapper API is a native C/C++ API that enables you to use the Windows App SDK APIs in your app. But in a .NET app that uses the Windows App SDK 1.0 or later, you can use the .NET wrapper for the bootstrapper API. That 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 (leave the default name).
With the resources file open in the editor, create a new string resource with the following properties.
Name: Message
Value: Hello, resources!
Save the resources file.
Open the Program.cs code file, and 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.Windows.ApplicationModel.Resources.ResourceManager("DynamicDependenciesTest.pri");
// Look up a string in the .resw file using its name.
Console.WriteLine(manager.MainResourceMap.GetValue("Resources/Message").ValueAsString);
Click Start Without Debugging (or Start Debugging) to build and run your app. You should see the string Hello, resources! successfully displayed.
Follow these instructions to configure a C++ WinUI 3 project that's either packaged with external location, or unpackaged.
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 Browse tab, and search for Microsoft.WindowsAppSDK.
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.
Vigtigt
You'll need to edit the code below to suit your specific configuration. See the descriptions of the parameters of the MddBootstrapInitialize function so that you can specify one of the versions of the Windows App SDK that you have installed.
C++
const UINT32 majorMinorVersion{ 0x00010002 };
PCWSTR versionTag{ L"" };
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 Stable.const UINT32 majorMinorVersion{ 0x00010002 };
PCWSTR versionTag{ L"" };
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();
}
Click Start Without Debugging (or Start Debugging) to build and run your app.
Kilden til dette indhold kan findes på GitHub, hvor du også kan oprette og gennemse problemer og pullanmodninger. Du kan få flere oplysninger i vores vejledning til bidragydere.
Windows developer feedback
Windows developer er et åben kildekode projekt. Vælg et link for at give feedback:
If your app isn't installed by using MSIX (that is, it's packaged with external location or unpackaged), then you must initialize the Windows App SDK for use before you can call Windows App SDK features such as WinUI 3, App Lifecycle, MRT Core, and DWriteCore.
A Windows App SDK project is framework-dependent by default. To switch to self-contained deployment, follow the steps in this article (the terms *framework-dependent* and *self-contained* are described in [Windows App SDK deployment overview](../deploy-overview.md)).
This article provides guidance about deploying framework-dependent packaged apps (see [What is MSIX?](/windows/msix/overview)) that use the Windows App SDK.
This article provides instructions for updating a project created with an earlier preview or release version of the Windows App SDK or WinUI 3 to the latest version.