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

# XStoreQueryGameAndDlcPackageUpdatesAsync

> XStoreQueryGameAndDlcPackageUpdatesAsync

# XStoreQueryGameAndDlcPackageUpdatesAsync

检索游戏包以及任何关联的 DLC 或 hub-aware 包的可用更新列表，然后可用于下载并安装这些更新。

## 语法

```cpp theme={null}
HRESULT XStoreQueryGameAndDlcPackageUpdatesAsync(  
         const XStoreContextHandle storeContextHandle,  
         XAsyncBlock* async  
)  
```

### 参数

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

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

*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

HRESULT 成功或错误代码。

## 备注

要检索可用更新列表以及此函数的执行结果，请在调用此函数后调用 [XStoreQueryGameAndDlcPackageUpdatesResult](/reference/system/xstore/functions/xstorequerygameanddlcpackageupdatesresult)。要获取要检索的更新数量，请在调用此函数后调用 [XStoreQueryGameAndDlcPackageUpdatesResultCount](/reference/system/xstore/functions/xstorequerygameanddlcpackageupdatesresultcount)。结果计数函数很重要，因为它可以让你确定要传递给结果函数的合适数组大小。

| 调用上下文  | 结果                                                                       |
| ------ | ------------------------------------------------------------------------ |
| 系列游戏中心 | 系列游戏中心本身、任何依赖于它的 hub-aware 游戏，以及与任何 hub-aware 游戏或系列游戏中心本身关联的任何 DLC、附加内容。 |
| 其他游戏   | 游戏本身以及与其关联的 DLC、附加内容。                                                    |

以下代码片段演示了检索当前包的游戏和可选更新的示例。

```cpp theme={null}
struct UpdateContext
{
    XStoreContextHandle storeContextHandle;
    XTaskQueueHandle taskQueueHandle;
    bool downloadOnly;
};

void CALLBACK DownloadAndInstallPackageUpdatesCallback(XAsyncBlock* asyncBlock)
{
    HRESULT hr = XStoreDownloadAndInstallPackageUpdatesResult(asyncBlock);

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

void CALLBACK DownloadPackageUpdatesCallback(XAsyncBlock* asyncBlock)
{
    HRESULT hr = XStoreDownloadPackageUpdatesResult(asyncBlock);

    if (FAILED(hr))
    {
        printf("Failed download package updates: 0x%x\r\n", hr);
        return;
    }
}

void CALLBACK QueryGameAndDlcPackageUpdatesCallback(XAsyncBlock* asyncBlock)
{
    UpdateContext* updateContext = reinterpret_cast<UpdateContext*>(asyncBlock->context);
    uint32_t count;

    HRESULT hr = XStoreQueryGameAndDlcPackageUpdatesResultCount(
        asyncBlock,
        &count);

    if (FAILED(hr))
    {
        printf("Failed retrieve the game and dlc update count: 0x%x\r\n", hr);
        delete updateContext;
        return;
    }

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

    if (count > 0)
    {
        XStorePackageUpdate* updates = new XStorePackageUpdate[count];
        hr = XStoreQueryGameAndDlcPackageUpdatesResult(
            asyncBlock,
            count,
            &updates);

        if (FAILED(hr))
        {
            delete[] updates;
            delete updateContext;
            printf("Failed retrieve the game and dlc updates: 0x%x\r\n", hr);
            return;
        }

        auto packageIdentifiers = new char[count][XPACKAGE_IDENTIFIER_MAX_LENGTH];
        for (uint32_t index = 0; index < count; index++)
        {
            printf("packageIdentifier: %s\r\n", updates[index].packageIdentifier);
            printf("isMandatory      : %s\r\n", updates[index].isMandatory ? "true" : "false");

            memcpy(&packageIdentifiers[index], updates[index].packageIdentifier, XPACKAGE_IDENTIFIER_MAX_LENGTH);
        }

        delete[] updates;

        auto downloadAsyncBlock = std::make_unique<XAsyncBlock>();
        ZeroMemory(downloadAsyncBlock.get(), sizeof(*downloadAsyncBlock));
        downloadAsyncBlock->queue = updateContext->taskQueueHandle;
        downloadAsyncBlock->context = updateContext;

        if (updateContext->downloadOnly)
        {
            // NOTE: This can be used instead to only perform the download.
            // This is helpful if you wish to download in the background
            // while the player continues to play. Once the download is completed,
            // you could then warn the user and call XStoreDownloadAndInstallPackageUpdatesAsync
            // to trigger the game to update which may close the game.

            downloadAsyncBlock->callback = DownloadPackageUpdatesCallback;
            hr = XStoreDownloadPackageUpdatesAsync(
                updateContext->storeContextHandle,
                (const char**)(&packageIdentifiers[0]),
                count,
                downloadAsyncBlock.get());

            if (FAILED(hr))
            {
                delete updateContext;
                delete[] packageIdentifiers;
                printf("Failed start download: 0x%x\r\n", hr);
                return;
            }
        }
        else
        {
            downloadAsyncBlock->callback = DownloadAndInstallPackageUpdatesCallback;
            hr = XStoreDownloadAndInstallPackageUpdatesAsync(
                updateContext->storeContextHandle,
                (const char**)(&packageIdentifiers[0]),
                count,
                downloadAsyncBlock.get());

            if (FAILED(hr))
            {
                delete updateContext;
                delete[] packageIdentifiers;
                printf("Failed start download and install: 0x%x\r\n", hr);
                return;
            }
        }

        delete[] packageIdentifiers;
    }
    else
    {
        delete updateContext;
    }
}

void QueryGameAndDlcPackageUpdates(XStoreContextHandle storeContextHandle, XTaskQueueHandle taskQueueHandle, bool downloadOnly)
{
    UpdateContext* updateContext = new UpdateContext;
    updateContext->storeContextHandle = storeContextHandle;
    updateContext->taskQueueHandle = taskQueueHandle;
    updateContext->downloadOnly = downloadOnly;

    auto asyncBlock = std::make_unique<XAsyncBlock>();
    ZeroMemory(asyncBlock.get(), sizeof(*asyncBlock));
    asyncBlock->queue = taskQueueHandle;
    asyncBlock->context = updateContext;
    asyncBlock->callback = QueryGameAndDlcPackageUpdatesCallback;

    HRESULT hr = XStoreQueryGameAndDlcPackageUpdatesAsync(
        storeContextHandle,
        asyncBlock.get());

    if (FAILED(hr))
    {
        printf("Failed to query game and DLC updates: 0x%x\r\n", hr);
        delete updateContext;
        return;
    }
    
    if(FAILED(XAsyncGetStatus(asyncBlock, true))) 
    { 
        printf("XStoreQueryGameAndDlcPackageUpdatesAsync failed\r\n"); 
        return; 
    } 
}

```

## 要求

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

**库：** xgameruntime.lib

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

## 概念文档

* [检查更新](https://learn.microsoft.com/gaming/gdk/docs/store/commerce/fundamentals/xstore-checking-for-updates)

## 另请参阅

[XStore](/reference/system/xstore/xstore_members)\
[XStoreQueryGameAndDlcPackageUpdatesResult](/reference/system/xstore/functions/xstorequerygameanddlcpackageupdatesresult)\
[XStoreQueryGameAndDlcPackageUpdatesResultCount](/reference/system/xstore/functions/xstorequerygameanddlcpackageupdatesresultcount)\
[系列游戏中心](https://learn.microsoft.com/gaming/gdk/docs/store/franchise-game-hubs)


## Related topics

- [XStoreQueryGameAndDlcPackageUpdatesResult](/zh-CN/reference/system/xstore/functions/xstorequerygameanddlcpackageupdatesresult.md)
- [XStoreQueryGameAndDlcPackageUpdatesResultCount](/zh-CN/reference/system/xstore/functions/xstorequerygameanddlcpackageupdatesresultcount.md)
- [XStorePackageUpdate](/zh-CN/reference/system/xstore/structs/xstorepackageupdate.md)
- [XStoreQueryPackageUpdatesResult](/zh-CN/reference/system/xstore/functions/xstorequerypackageupdatesresult.md)
- [XStoreDownloadPackageUpdatesResult](/zh-CN/reference/system/xstore/functions/xstoredownloadpackageupdatesresult.md)
