> ## 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](/zh-TW/reference/system/xstore/functions/xstorecreatecontext) 所傳回之使用者的市集內容控制代碼。

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

套件識別碼字串的清單。
套件識別碼可唯一識別 Microsoft Store 中的套件。如需套件識別碼的詳細資訊，請參閱[管理可下載內容 (DLC) 並授權](/zh-TW/publishing/xstore-commerce/xstore-dlc)。

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

**storeIds** 中的識別碼數目。

*async*   \_Inout\_\
類型：[XAsyncBlock\*](/zh-TW/reference/system/xasync/structs/xasyncblock)

定義正在進行之非同步工作的 [XAsyncBlock](/zh-TW/reference/system/xasync/structs/xasyncblock)。[XAsyncBlock](/zh-TW/reference/system/xasync/structs/xasyncblock) 可用來輪詢呼叫的狀態並擷取呼叫結果。如需詳細資訊，請參閱 [XAsyncBlock](/zh-TW/reference/system/xasync/structs/xasyncblock)。

### 傳回值

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

HRESULT 成功或錯誤碼。

## 備註

若要擷取此函式的下載和安裝結果，請在呼叫此函式之後呼叫 [XStoreDownloadAndInstallPackagesResult](/zh-TW/reference/system/xstore/functions/xstoredownloadandinstallpackagesresult)。若要擷取已下載並安裝的項目數目，請在呼叫此函式之後呼叫 [XStoreDownloadAndInstallPackagesResultCount](/zh-TW/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) 並授權](/zh-TW/publishing/xstore-commerce/xstore-dlc)

## 另請參閱

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


## Related topics

- [XStoreDownloadAndInstallPackagesAsync](/reference/system/xstore/functions/xstoredownloadandinstallpackagesasync.md)
- [XStoreDownloadAndInstallPackagesResult](/reference/system/xstore/functions/xstoredownloadandinstallpackagesresult.md)
- [XStoreDownloadAndInstallPackagesResultCount](/reference/system/xstore/functions/xstoredownloadandinstallpackagesresultcount.md)
