Lezen in het Engels Bewerken

Share via


Tutorial: Build and deploy an unpackaged app using Preview and Experimental channels of the Windows App SDK

Using the Windows App SDK Stable version: You can auto-initialize the Windows App SDK through the WindowsPackageType project property when you Create your first WinUI 3 project. You can also follow a tutorial (Tutorial: Use the bootstrapper API in an app packaged with external location or unpackaged that uses the Windows App SDK) in which you configure a packaged with external location or unpackaged app to load the Windows App SDK runtime, and call Windows App SDK APIs.

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.

Prerequisites

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

Instructions

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

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.

  1. In Visual Studio, create a new C++ Console App project. Name the project DynamicDependenciesTest. Screenshot of creating a new C++ app in Visual Studio

    Screenshot of naming a new C++ app in Visual Studio

    After you create the project, you should have a 'Hello World' C++ console app.

  2. Next, install the Windows App SDK NuGet package in your project.

    1. In Solution Explorer, right-click the References node and choose Manage Nuget Packages.
    2. 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.
  3. For unpackaged apps, you will need to call the Bootstrapper API to Use the Windows App SDK runtime for apps packaged with external location or unpackaged. This enables you to use Windows App SDK APIs at runtime.

    1. 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>   
      
    2. 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; 
      } 
      
    3. 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(); 
      
    4. Your final code should look like this.

      C++
      #include <iostream> 
      #include <windows.h> 
      #include <MddBootstrap.h>
      
      int main() 
      { 
      
          // 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(); 
      } 
      
  4. Press F5 to build and run your app.