备注

如果您要创建UWP应用程序包,请参阅使用MakeAppx.exe工具创建应用程序包。

了解如何使用打包API为Windows应用商店应用创建应用包。

如果要手动创建桌面应用程序包,还可以使用使用打包API的MakeAppx.exe工具。有关详细信息,请参阅App packager(MakeAppx.exe)。

如果您使用的是Visual Studio,建议您使用Visual Studio打包向导打包您的应用程序。有关更多详细信息,请参阅打包UWP应用程序。

说明

第1步:创建包编写器

要创建包编写器,请调用IAppxFactory :: CreatePackageWriter方法。第一个参数是将写入包的输出流。第二个参数是指向APPX_PACKAGE_SETTINGS结构的指针,该结构指定包设置。第三个参数是一个输出参数,它接收指向IAppxPackageWriter指针的指针。

C ++复制
#include <windows.h>
#include <shlwapi.h>
#include <AppxPackaging.h>
// We store the produced package under this file name.
const LPCWSTR OutputPackagePath = L"HelloWorld.appx";
int wmain()
{
    HRESULT hr = S_OK;
    // Specify the appropriate COM threading model
    hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
    if (SUCCEEDED(hr))
    {
        // Create a package writer
        IAppxPackageWriter* packageWriter = NULL;
        hr = GetPackageWriter(OutputPackagePath, &packageWriter);
    }
}
//
// Creates an app package writer with default settings.
//
// Parameters:
//   outputFileName
//     Fully qualified name of the app package (.appx file) to be created.
//   writer
//     On success, receives the created instance of IAppxPackageWriter.
//
HRESULT GetPackageWriter(
    _In_ LPCWSTR outputFileName,
    _Outptr_ IAppxPackageWriter** writer)
{
    const LPCWSTR Sha256AlgorithmUri = L"https://www.w3.org/2001/04/xmlenc#sha256";
    HRESULT hr = S_OK;
    IStream* outputStream = NULL;
    IUri* hashMethod = NULL;
    APPX_PACKAGE_SETTINGS packageSettings = {0};
    IAppxFactory* appxFactory = NULL;
    // Create a stream over the output file for the package
    hr = SHCreateStreamOnFileEx(
            outputFileName,
            STGM_CREATE | STGM_WRITE | STGM_SHARE_EXCLUSIVE,
            0,     // default file attributes
            TRUE,  // create file if it does not exist
            NULL,  // no template
            &outputStream);
    // Create default package writer settings, including hash algorithm URI
    // and Zip format.
    if (SUCCEEDED(hr))
    {        hr = CreateUri(
                Sha256AlgorithmUri,
                Uri_CREATE_CANONICALIZE,
                0, // reserved parameter
                &hashMethod);
    }
    if (SUCCEEDED(hr))
    {
        packageSettings.forceZip32 = TRUE;
        packageSettings.hashMethod = hashMethod;
    }
    // Create a new Appx factory
    if (SUCCEEDED(hr))
    {
        hr = CoCreateInstance(
                __uuidof(AppxFactory),
                NULL,
                CLSCTX_INPROC_SERVER,
                __uuidof(IAppxFactory),
                (LPVOID*)(&appxFactory));
    }
    // Create a new package writer using the factory
    if (SUCCEEDED(hr))
    {
        hr = appxFactory->CreatePackageWriter(
                outputStream,
                &packageSettings,
                writer);
    }
    // Clean up allocated resources
    if (appxFactory != NULL)
    {
        appxFactory->Release();
        appxFactory = NULL;
    }
    if (hashMethod != NULL)
    {
        hashMethod->Release();
        hashMethod = NULL;
    }
    if (outputStream != NULL)
    {
        outputStream->Release();
        outputStream = NULL;
    }
    return hr;
}

第2步:将应用程序的有效内容文件添加到包中

调用IAppxPackageWriter :: AddPayloadFile方法将文件添加到包中。第一个参数是文件的相对路径。第二个参数表示文件的内容类型。第三个参数指定APPX_COMPRESSION_OPTION枚举中的选项。第四个参数是文件的输入流。以下效果展示

黄铜浮球阀, 不锈钢法兰浮球阀, 不锈钢水箱浮球阀, Q941F-16P不锈钢电动球阀, 丝扣浮球阀, 黄铜小孔浮球阀, Z45X暗杆软密封闸阀, GLY储油罐量油孔, FZT防爆阻火呼吸人孔, CWX微型电动球阀, Q941F电动开关球阀, Q941F电动法兰球阀, Q911F电动内螺纹球阀, Q671F气动对夹式球阀, Q611N气动高压球阀, Q641F气动带手动球阀, Q611F气动三片式球阀, Q611F气动两片式球阀, Q641F气动调节球阀, Q971F电动对夹球阀, Q641F不锈钢气动球阀, 不锈钢高压球阀, 超高压球阀, Q41Y高压硬密封球阀, QJ41M耐高温球阀, QJ41M不锈钢高温球阀, 法兰高温球阀, Q41H高温硬密封球阀, Q41H金属硬密封高温球阀, (Q45F)T型三通球阀, (Q44F)L型三通球阀, KHB3K高压三通球阀, BQ71F薄型保温球阀, Q11F螺纹球阀, Q11F内螺纹球阀, Q21F外螺纹球阀, Q41F法兰球阀, Q41F法兰式球阀, Q61F焊接球阀, Q61F全焊接球阀, Q61F焊接式球阀, Q71F对夹球阀, Q71F对夹式球阀, Q61N对焊球阀, Q91F卡套球阀, Q91F不锈钢卡套球阀, Q41N锻钢球阀, Q61N焊接锻钢球阀, Q41N高压锻钢球阀, KQ41F不锈钢抗硫球阀, 切断球阀, 气动切断球阀-气动O型切断球阀, 电动切断球阀-电动O型切断球阀, Q46F不锈钢四通球阀, Q46F四通球阀, Q41F燃气球阀, Q41F燃气专用球阀, Q81F卫生球阀, Q81F快装球阀, Q81F卫生级快装球阀, Q81F卫生级球阀, Q681F卫生级气动球阀, 广式球阀, Q11F广式内螺纹球阀, Q21F广式外螺纹球阀, Q41F广式法兰球阀, Q11F丝口球阀, Q11F丝扣球阀, V型球阀, 电动V型球阀, 气动V型球阀, YJZQ高压液压球阀, KHBF-KHMF法兰式高压球阀, CJZQ高压球阀, VH3V二位三通高压球阀, BKH-MKH高压球阀, VH2V直通高压球阀, BME高压球阀, KHB高压球阀, KHP-PKH高压板式球阀, (喷煤粉)卸灰球阀, Q647MF气动卸灰球阀(喷煤粉专用球阀)

C ++复制
// Path where all input files are stored
const LPCWSTR DataPath = L"Data\\";
// Add all payload files to the package writer
for (int i = 0; SUCCEEDED(hr) && (i < PayloadFilesCount); i++)
{
    IStream* fileStream = NULL;
    hr = GetFileStream(DataPath, PayloadFilesName[i], &fileStream);
    if (SUCCEEDED(hr))
    {
        packageWriter->AddPayloadFile(
            PayloadFilesName[i],
            PayloadFilesContentType[i],
            PayloadFilesCompression[i],
            fileStream);
        }
        if (fileStream != NULL)
        {
            fileStream->Release();
            fileStream = NULL;
        }
    }
}

前面的代码使用这些变量定义和GetFileStream辅助函数。

C ++复制
#include <strsafe.h>
#include <shlwapi.h>
// The produced app package's content consists of these files, with
// corresponding content types and compression options.
const int PayloadFilesCount = 4;
const LPCWSTR PayloadFilesName[PayloadFilesCount] = {
    L"AppTile.png",
    L"Default.html",
    L"images\\smiley.jpg",
    L"Error.html",
};
const LPCWSTR PayloadFilesContentType[PayloadFilesCount] = {
    L"image/png",
    L"text/html",
    L"image/jpeg",
    L"text/html",
};
const APPX_COMPRESSION_OPTION PayloadFilesCompression[PayloadFilesCount] = {
    APPX_COMPRESSION_OPTION_NONE,
    APPX_COMPRESSION_OPTION_NORMAL,
    APPX_COMPRESSION_OPTION_NONE,
    APPX_COMPRESSION_OPTION_NORMAL,
};
//
// Creates a readable IStream over the specified file. For simplicity, we assume that the fully
// qualified file name is 100 characters or less. Your code should
// handle longer names, and allocate the buffer dynamically.
//
// Parameters:
//   path
//      Path of the folder that contains the file to be opened; must end with a '\'
//   fileName
//      Name, of the file to be opened, not including the path
//   stream
//      On success, receives the created instance of IStream
//
HRESULT GetFileStream(
    _In_ LPCWSTR path,
    _In_ LPCWSTR fileName,
    _Outptr_ IStream** stream)
{
    HRESULT hr = S_OK;
    const int MaxFileNameLength = 100;
    WCHAR fullFileName[MaxFileNameLength + 1];
    // Create full file name by concatenating path and fileName
    hr = StringCchCopyW(fullFileName, MaxFileNameLength, path);
    if (SUCCEEDED(hr))
    {
        hr = StringCchCat(fullFileName, MaxFileNameLength, fileName);
    }
    // Create stream for reading the file
    if (SUCCEEDED(hr))
    {
        hr = SHCreateStreamOnFileEx(
                fullFileName,
                STGM_READ | STGM_SHARE_EXCLUSIVE,
                0,      // default file attributes
                FALSE,  // don't create new file
                NULL,   // no template
                stream);
    }
    return hr;
}

第3步:将包清单添加到包中

每个包都必须有一个包清单。要将包清单添加到包中,请为该文件创建输入流,然后调用IAppxPackageWriter :: Close方法在包的末尾写入清单并关闭包编写器的输出流。

此代码使用GetFileStream上一步中显示的帮助程序函数为程序包清单创建流。

C ++复制
// We read the app package's manifest from this file
const LPCWSTR ManifestFileName = L"AppxManifest.xml";
IStream* manifestStream = NULL;
hr = GetFileStream(DataPath, ManifestFileName, &manifestStream);
if (SUCCEEDED(hr))
{
    hr = packageWriter->Close(manifestStream);
}
if (manifestStream != NULL)
{
    manifestStream->Release();
    manifestStream = NULL;
}

第4步:清理包编写器

在从wmain函数返回之前,调用Release方法来清理包编写器并调用CoUninitialize函数。

C ++复制
if (packageWriter != NULL)
{
    packageWriter->Release();
    packageWriter = NULL;
}
CoUninitialize();

如何创建应用程序包(C ++)的更多相关文章

  1. Win10 创建应用程序包及部署

    https://msdn.microsoft.com/zh-cn/library/windows/apps/xaml/hh454036.aspx https://msdn.microsoft.com/ ...

  2. 如何以编程方式签署应用程序包(C ++)

    了解如何使用SignerSignEx2函数对应用包进行签名. 如果要使用Packaging API以编程方式创建Windows应用商店应用包,则还需要在部署之前对应用包进行签名.Packaging A ...

  3. 如何使用SignTool签署应用程序包

    备注 有关签署UWP应用程序包的信息,请参阅使用SignTool签署应用程序包. 了解如何使用SignTool对Windows应用商店应用包进行签名,以便部署它们.SignTool是Windows软件 ...

  4. Silverlight 之 创建

          Silverlight 项目文件是您可以使用不同工具来创建和编辑的文本文件.例如,可以使用 Visual Studio 2010 以及 Expression Blend 来创建 Silve ...

  5. UWP 从创建到发布流程一栏

    # UWP的产品新建到发布流程一览 1,UWP开发特性 U: Universal(通用) W: Windows P: Plantform(平台) 运行在Windows10设备 比WPF更加多样化和完善 ...

  6. xamarin.forms uwp app部署到手机移动设备进行测试,真机调试(device portal方式部署)

    最近学习xamarin.刚好 手上有一个lumia 930.所以试一试把uwp app部署到手机上,并真机调试一把. 目前环境: 1.开发pc电脑是win10,版本1607.加入了insider,所以 ...

  7. 我的第一个WP8.1应用总结

    我的LUMIA925已经买了很久了,想自己开发WP应用放在上面,却一直想不到有什么特别的想法和需要.前几天的事情正好让我有了这个机会. 前几天在客户机房工作的时候,同事打电话来说另一个客户由于换了电脑 ...

  8. iOS开发-- 开发环境,证书和授权文件

    一.成员介绍 1.    Certification(证书)证书是对电脑开发资格的认证,每个开发者帐号有一套,分为两种:1)    Developer Certification(开发证书)安装在电脑 ...

  9. IOS 开发环境,证书和授权文件等详解

    (转自:http://blog.csdn.net/gtncwy/article/details/8617788) 一.成员介绍1.    Certification(证书)证书是对电脑开发资格的认证, ...

随机推荐

  1. 第二章——机器学习项目完整案例(End-to-End Machine Learning Project)

    本章通过一个例子,介绍机器学习的整个流程. 2.1 使用真实数据集练手(Working with Real Data) 国外一些获取数据的网站: Popular open data repositor ...

  2. Unable to find remote helper for 'https'

    出现这个报错,说明git目前的状态是正常的,要么没装好,要么自己解决压缩安装导致没有权限 第三次情况是,使用yum install git 重新安装后,仍然报错,是因为环境变量中GIT_HOM配置的仍 ...

  3. jquery-bootgrid

    http://www.jquery-bootgrid.com/GettingStarted 日志是生产环境非常重要的配置,在迁移老的工程到spring-boot时日志的设置兼容很重要,以下是自己在配置 ...

  4. python 简单的信息管理系统

    #!/usr/bin/python #coding=utf-8 import io import os import time FileRead = io.open('callingcard','r' ...

  5. BigInteger类及方法应用

    注:以下内容来自传智播客(itcast)老师授课 BigInteger 可以让超过Integer范围内的数据进行运算 构造方法 public BigInteger(String val) 成员方法 p ...

  6. mybatis 异常Result Maps collection does not contain value for java.lang.String

    Result Maps collection does not contain value for java.lang.String 以上是我报的错. 只要报Result Maps collectio ...

  7. java 判断是否为中文字符,部分,。中文符号不能识别

    public static void main(String[] args) { int i = 0; for (char c : ",.判断一个字符串是否有中文一般情况是利用Unicode ...

  8. Windbg分析高内存占用问题

    1. 问题简介 最近产品发布大版本补丁更新,一商超客户升级后,反馈系统经常奔溃,导致超市的收银系统无法正常收银,现场排队付款的顾客更是抱怨声声.为了缓解现场的情况, 客户都是手动回收IIS应用程序池才 ...

  9. [Swift]LeetCode1033. 移动石子直到连续 | Moving Stones Until Consecutive

    Three stones are on a number line at positions a, b, and c. Each turn, let's say the stones are curr ...

  10. Bagging之随机森林

    随机森林(Random Forest)是一种Bagging(Bootstrap Aggregating)集成算法,在样本随机(样本扰动)的基础上,进一步运用特征随机(属性扰动)的机制,得到比一般的Ba ...