Tutorial: Use the bootstrapper API in an app packaged with external location or unpackaged that uses the Windows App SDK

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.

Important

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.

If you have advanced needs (such as custom error handling, or to load a specific version of the Windows App SDK), then you can instead call the bootstrapper API explicitly. And that's the approach that this topic demonstrates. Also, for more info, see Use the Windows App SDK runtime for apps packaged with external location or unpackaged.

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.

Prerequisites

  1. Install tools for the Windows App SDK.
  2. Ensure that all dependencies for packaged with external location and unpackaged apps are installed (see Windows App SDK deployment guide for framework-dependent apps packaged with external location or unpackaged). An easy way to do that is to run the Windows App SDK runtime installer.

Instructions

You can follow this tutorial using a C# or a C++ project.

Note

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 is packaged with external location or unpackaged.

  1. In Visual Studio, create a new C# Console Application project. Name the project DynamicDependenciesTest.

  2. Next, configure your project.

    1. In Solution Explorer, right-click your project and choose Edit Project File.

    2. 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.

      <TargetFramework>net6.0-windows10.0.19041.0</TargetFramework>
      
    3. Save and close the project file.

  3. 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.

    1. Select Build > Configuration Manager.
    2. Select the drop-down under Active solution platform and click New to open the New Solution Platform dialog box.
    3. In the drop-down under Type or select the new platform, select x64.
    4. Click OK to close the New Solution Platform dialog box.
    5. In Configuration Manager, click Close.
  4. Install the Windows App SDK NuGet package in your project.

    1. In Solution Explorer, right-click the Dependencies node and choose Manage Nuget Packages.
    2. In the NuGet Package Manager window, select the Browse tab, and install the Microsoft.WindowsAppSDK package.
  5. You're now ready to use the bootstrapper API (see Use the Windows App SDK runtime for apps packaged with external location or unpackaged) to dynamically take a dependency on the Windows App SDK framework package. This enables you to use the Windows App SDK APIs in your app.

    Open the Program.cs code file and replace the default code with the following code.

    using System;
    using Microsoft.Windows.ApplicationModel.DynamicDependency;
    
    namespace DynamicDependenciesTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                Bootstrap.Initialize(0x00010000);
                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 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.

  6. 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.

    1. Add a new Resources File (.resw) to your project.

    2. With the resources file open in the editor, create a new string resource with the following properties.

      • Name: Message
      • Value: Hello, World!
    3. Save the resources file.

    4. Open the Program.cs code file and add the following statement to the top of the file:

      using Microsoft.Windows.ApplicationModel.Resources;
      
    5. Replace the Console.WriteLine("Hello, World!"); line with the following code.

      // 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);
      
  7. Press F5 to build and run your app. You should see the string Hello, World! successfully displayed.