> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# XStoreDownloadAndInstallPackagesAsync

> XStoreDownloadAndInstallPackagesAsync

# XStoreDownloadAndInstallPackagesAsync

下载并安装指定的应用商店包。

## 语法

```cpp theme={null}
HRESULT XStoreDownloadAndInstallPackagesAsync(  
         const XStoreContextHandle storeContextHandle,  
         const char** storeIds,  
         size_t storeIdsCount,  
         XAsyncBlock* async  
)  
```

### 参数

*storeContextHandle*   \_In\_\
类型：XStoreContextHandle

由 [XStoreCreateContext](/reference/system/xstore/functions/xstorecreatecontext) 返回的用户的应用商店上下文句柄。

*storeIds*   \_In\_z\_count\_(storeIdsCount)\
类型：char\*\*

包标识符字符串的列表。
包标识符唯一标识来自 Microsoft 应用商店的包。有关包标识符的详细信息，请参阅[管理和授权可下载内容 (DLC)](https://learn.microsoft.com/gaming/gdk/docs/store/commerce/fundamentals/xstore-manage-and-license-optional-packages)。

*storeIdsCount*   \_In\_\
类型：size\_t

**storeIds** 中的 ID 数量。

*async*   \_Inout\_\
类型：[XAsyncBlock\*](/reference/system/xasync/structs/xasyncblock)

定义正在执行的异步工作的 [XAsyncBlock](/reference/system/xasync/structs/xasyncblock)。可使用 [XAsyncBlock](/reference/system/xasync/structs/xasyncblock) 轮询调用状态并检索调用结果。有关详细信息，请参阅 [XAsyncBlock](/reference/system/xasync/structs/xasyncblock)。

### 返回值

类型：[HRESULT](https://learn.microsoft.com/openspecs/windows_protocols/ms-erref/0642cb2f-2075-4469-918c-4441e69c548a)

HRESULT 成功或错误代码。

## 备注

要检索此函数的下载和安装结果，请在调用此函数后调用 [XStoreDownloadAndInstallPackagesResult](/reference/system/xstore/functions/xstoredownloadandinstallpackagesresult)。要检索已下载和已安装的项数，请在调用此函数后调用 [XStoreDownloadAndInstallPackagesResultCount](/reference/system/xstore/functions/xstoredownloadandinstallpackagesresultcount)。结果计数函数很重要，因为它可以让你确定要传递给结果函数的合适数组大小。

当从正在运行的游戏中调用此 API 时，下载将被视为高优先级，它们将按此 API 的 storeIds 参数中指定的顺序排在队列前面。

以下代码片段演示了下载和安装应用商店包的示例。

```cpp theme={null}
  
void CALLBACK DownloadAndInstallPackagesCallback(XAsyncBlock* asyncBlock)
{
    uint32_t count;

    HRESULT hr = XStoreDownloadAndInstallPackagesResultCount(
        asyncBlock,
        &count);

    if (FAILED(hr))
    {
        printf("Failed retrieve the installing packages count: 0x%x\r\n", hr);
        return;
    }

    printf("Number of updates: %d", count);

    auto packageIdentifiers = new char[count][XPACKAGE_IDENTIFIER_MAX_LENGTH];
    hr = XStoreDownloadAndInstallPackagesResult(
        asyncBlock,
        count,
        &packageIdentifiers[0]);

    if (FAILED(hr))
    {
        delete[] packageIdentifiers;
        printf("Failed retrieve the installing packages results: 0x%x\r\n", hr);
        return;
    }

    for (uint32_t index = 0; index < count; index++)
    {
        printf("packageIdentifier: %s\r\n", packageIdentifiers[index]);
    }

    delete[] packageIdentifiers;
}

void DownloadAndInstallPackages(XStoreContextHandle storeContextHandle, XTaskQueueHandle taskQueueHandle, const char** storeIds, size_t storeIdsCount)
{
    auto asyncBlock = std::make_unique<XAsyncBlock>();
    ZeroMemory(asyncBlock.get(), sizeof(*asyncBlock));
    asyncBlock->queue = taskQueueHandle;
    asyncBlock->callback = DownloadAndInstallPackagesCallback;

    HRESULT hr = XStoreDownloadAndInstallPackagesAsync(
        storeContextHandle,
        storeIds,
        storeIdsCount,
        asyncBlock.get());

    if (FAILED(hr))
    {
        printf("Failed to start download and install: 0x%x\r\n", hr);
        return;
    }
}
```

## 要求

**头文件：** XStore.h（包含于 XGameRuntime.h 中）

**库：** xgameruntime.lib

**支持的平台：** Windows、XBOX One 系列主机和 XBOX Series 主机

## 概念文档

* [管理和授权可下载内容 (DLC)](https://learn.microsoft.com/gaming/gdk/docs/store/commerce/fundamentals/xstore-manage-and-license-optional-packages)

## 另请参阅

[XStore](/reference/system/xstore/xstore_members)\
[XStoreDownloadAndInstallPackagesResult](/reference/system/xstore/functions/xstoredownloadandinstallpackagesresult)\
[XStoreDownloadAndInstallPackagesResultCount](/reference/system/xstore/functions/xstoredownloadandinstallpackagesresultcount)


## Related topics

- [XStoreDownloadAndInstallPackagesResult](/zh-CN/reference/system/xstore/functions/xstoredownloadandinstallpackagesresult.md)
- [XStoreDownloadAndInstallPackagesResultCount](/zh-CN/reference/system/xstore/functions/xstoredownloadandinstallpackagesresultcount.md)
- [XStoreQueryPackageIdentifier](/zh-CN/reference/system/xstore/functions/xstorequerypackageidentifier.md)
- [XStoreShowProductPageUIResult](/zh-CN/reference/system/xstore/functions/xstoreshowproductpageuiresult.md)
- [管理并授权可下载内容 (DLC)](/zh-CN/publishing/xstore-commerce/xstore-dlc.md)
