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

# XStoreQueryEntitledProductsAsync

> XStoreQueryEntitledProductsAsync

# XStoreQueryEntitledProductsAsync

傳回使用者擁有權利之所有與目前遊戲相關產品的商店資訊。

## 語法

```cpp theme={null}
HRESULT XStoreQueryEntitledProductsAsync(  
         const XStoreContextHandle storeContextHandle,  
         XStoreProductKind productKinds,  
         uint32_t maxItemsToRetrievePerPage,  
         XAsyncBlock* async  
)  
```

### 參數

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

由 [XStoreCreateContext](https://docs.xbox.com/zh-TW/reference/system/xstore/functions/xstorecreatecontext.md) 傳回之使用者的商店內容控制代碼。

*productKinds*   \_In\_\
類型：[XStoreProductKind](https://docs.xbox.com/zh-TW/reference/system/xstore/enums/xstoreproductkind.md)

要查詢的產品種類。

*maxItemsToRetrievePerPage*   \_In\_\
類型：uint32\_t

每頁要擷取的項目數目上限。
每頁傳回的項目數目可介於 0 到 `maxItemsToRetrievePerPage` 之間。

*async*   \_Inout\_\
類型：[XAsyncBlock\*](https://docs.xbox.com/zh-TW/reference/system/xasync/structs/xasyncblock.md)

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

### 傳回值

類型：HRESULT

成功時傳回 S\_OK，否則傳回錯誤碼。

## 備註

呼叫 [XStoreQueryEntitledProductsResult](https://docs.xbox.com/zh-TW/reference/system/xstore/functions/xstorequeryentitledproductsresult.md) 以擷取商店產品資訊與此函式的執行結果。
請在 `XStoreQueryEntitledProductsAsync` 的回呼函式中執行 [XStoreQueryEntitledProductsResult](https://docs.xbox.com/zh-TW/reference/system/xstore/functions/xstorequeryentitledproductsresult.md)。
結果包含一個 `XStoreProductQueryHandle`，您可以呼叫 [XStoreEnumerateProductsQuery](https://docs.xbox.com/zh-TW/reference/system/xstore/functions/xstoreenumerateproductsquery.md) 來列舉它。

> [!NOTE]
>
> 玩家擁有權利的項目越多，此 API 傳回所需的時間可能就越長。
>
> 此 API 支援與 [XStoreQueryAssociatedProductsAsync](https://docs.xbox.com/zh-TW/reference/system/xstore/functions/xstorequeryassociatedproductsasync.md) 中所述相同的分頁模式。
>
> 分頁在沙箱與 RETAIL 之間的行為可能不同。請驗證您的分頁實作，並務必在發行前於 RETAIL 中進行測試。
>
> 如需詳細資訊，請參閱[分頁](https://docs.xbox.com/zh-TW/reference/system/xstore/functions/xstorequeryassociatedproductsasync.md#paging)。

您無法事先得知此查詢會傳回的產品數目。

下列程式碼片段提供範例，示範如何查詢使用者對與目前遊戲相關之內容的權利。
結果會跨多個頁面處理。

```cpp theme={null}
struct QueryContext
{
    uint32_t count = 0;  // used to accumulate count of products returned across pages
    XStoreProductQueryHandle handle = nullptr;  // handle to reuse for paged operations
};

void CleanupProductQuery(XAsyncBlock* async)
{
    if (!async) return;

    auto* ctx = static_cast<QueryContext*>(async->context);
    if (ctx)
    {
        if (ctx->handle)
        {
            XStoreCloseProductsQueryHandle(ctx->handle);
            ctx->handle = nullptr;
        }

        delete ctx;
        async->context = nullptr;
    }

    delete async;
}

bool CALLBACK ProductEnumerationCallback(const XStoreProduct* product, void* context)
{
    // Use result of query
    auto* ctx = static_cast<QueryContext*>(context);

    // Empty results are possible and should be skipped
    if (product)
    {
        printf("%s %s %u\n", product->title, product->storeId, product->productKind);
        ctx->count++;
    }

    return true;
}

void CALLBACK QueryEntitledProductsCallback(XAsyncBlock* async)
{
    auto* ctx = static_cast<QueryContext*>(async->context);

    HRESULT hr = XStoreQueryEntitledProductsResult(async, &ctx->handle);
    if (FAILED(hr)) { CleanupProductQuery(async); return; }

    hr = XStoreEnumerateProductsQuery(ctx->handle, ctx, ProductEnumerationCallback);
    if (FAILED(hr)) { CleanupProductQuery(async); return; }

    if (XStoreProductsQueryHasMorePages(ctx->handle))
    {
        QueryNextPage(async);
        return;
    }

    printf("Enumeration complete, %u entitled products found\n", ctx->count);
    CleanupProductQuery(async);
}

void QueryEntitledProducts(XStoreContextHandle storeContextHandle, XTaskQueueHandle taskQueueHandle)
{
    auto async = new XAsyncBlock{};
    async->context = new QueryContext{ 0, nullptr };
    async->queue = taskQueueHandle; 
    async->callback = QueryEntitledProductsCallback; 
 
    XStoreProductKind typeFilter = 
        XStoreProductKind::Consumable | 
        XStoreProductKind::Durable | 
        XStoreProductKind::Game |
        XStoreProductKind::Pass |
        XStoreProductKind::UnmanagedConsumable;
 
    HRESULT hr = XStoreQueryEntitledProductsAsync( 
        storeContextHandle, 
        typeFilter, 
        25,           // Products per page (25 is a good default, can set lower to verify paging in sandbox)
        async);

    if (FAILED(hr))
    {
        printf("Failed to get entitled products: 0x%x\r\n", hr); 
        CleanupProductQuery(async);
    }
}

// In RETAIL, content will be returned across multiple pages.
// Paging is not complete until XStoreProductsQueryHasMorePages returns false.
void QueryNextPage(XAsyncBlock *async)
{
    async->callback = [](XAsyncBlock* async)
    {
        auto* ctx = static_cast<QueryContext*>(async->context);

        XStoreProductQueryHandle next = nullptr;
        HRESULT hr = XStoreProductsQueryNextPageResult(async, &next);
        if (FAILED(hr)) { CleanupProductQuery(async); return; }

        // Safe swap: avoids leaks if 'next' is a new handle, avoids double-close if identical
        if (next && next != ctx->handle)
        {
            if (ctx->handle) XStoreCloseProductsQueryHandle(ctx->handle);
            ctx->handle = next;
        }
        else
        {
            ctx->handle = next; // covers null or same-handle cases
        }

        hr = XStoreEnumerateProductsQuery(ctx->handle, ctx, ProductEnumerationCallback);
        if (FAILED(hr)) { CleanupProductQuery(async); return; }

        if (XStoreProductsQueryHasMorePages(ctx->handle))
        {
            QueryNextPage(async);
            return;
        }

        printf("Enumeration complete, %u entitled products found\n", ctx->count);
        CleanupProductQuery(async);
    };

    auto* ctx = static_cast<QueryContext*>(async->context);
    HRESULT hr = XStoreProductsQueryNextPageAsync(ctx->handle, async);
    if (FAILED(hr))
    {
        CleanupProductQuery(async);
    }
}

```

## 需求

**標頭：** XStore.h (包含在 XGameRuntime.h 中)

**程式庫：** xgameruntime.lib

**支援的平台：** Windows、XBOX One 系列主機和 XBOX Series 主機

## 概念文件

* [在遊戲中實作遊戲內購買](https://learn.microsoft.com/gaming/gdk/docs/gdk-dev/pc-dev/tutorials/pc-e2e-guide/e2e-services/e2e-in-game-purchases)
* [基本商店作業](https://docs.xbox.com/zh-TW/publishing/xstore-commerce/xstore-basic-operations.md)
* [如何使用不含套件的耐久性產品](https://docs.xbox.com/zh-TW/publishing/xstore-commerce/xstore-dwob.md)
* [授與玩家附加元件內容的存取權](https://docs.xbox.com/zh-TW/publishing/xstore-commerce/xstore-granting-access.md)
* [GDK 商務系統概觀](https://docs.xbox.com/zh-TW/publishing/xstore-commerce/xstore-overview.md)
* [Microsoft Store API 的細微速率限制](https://docs.xbox.com/zh-TW/publishing/xstore-commerce/xstore-fgrl.md)

## 另請參閱

[XStore](https://docs.xbox.com/zh-TW/reference/system/xstore/xstore_members.md)\
[XStoreQueryEntitledProductsResult](https://docs.xbox.com/zh-TW/reference/system/xstore/functions/xstorequeryentitledproductsresult.md)
[XStoreQueryAssociatedProductsAsync](https://docs.xbox.com/zh-TW/reference/system/xstore/functions/xstorequeryassociatedproductsasync.md)\
[XStoreQueryEntitledProductsAsync](https://docs.xbox.com/zh-TW/reference/system/xstore/functions/xstorequeryentitledproductsasync.md)\
[XStoreQueryProductsAsync](https://docs.xbox.com/zh-TW/reference/system/xstore/functions/xstorequeryproductsasync.md)\
[XStoreQueryProductForCurrentGameAsync](https://docs.xbox.com/zh-TW/reference/system/xstore/functions/xstorequeryproductforcurrentgameasync.md)\
[XStoreQueryProductForPackageAsync](https://docs.xbox.com/zh-TW/reference/system/xstore/functions/xstorequeryproductforpackageasync.md)\
[基本商店作業](https://docs.xbox.com/zh-TW/publishing/xstore-commerce/xstore-basic-operations.md)\
[授與玩家附加元件內容的存取權](https://docs.xbox.com/zh-TW/publishing/xstore-commerce/xstore-granting-access.md)


## Related topics

- [XStoreQueryEntitledProductsResult](https://docs.xbox.com/zh-TW/reference/system/xstore/functions/xstorequeryentitledproductsresult.md)
- [XStoreQueryProductForPackageAsync](https://docs.xbox.com/zh-TW/reference/system/xstore/functions/xstorequeryproductforpackageasync.md)
- [XStoreQueryProductsAsync](https://docs.xbox.com/zh-TW/reference/system/xstore/functions/xstorequeryproductsasync.md)
- [XStoreQueryProductForPackageResult](https://docs.xbox.com/zh-TW/reference/system/xstore/functions/xstorequeryproductforpackageresult.md)
- [XStoreQueryProductForCurrentGameAsync](https://docs.xbox.com/zh-TW/reference/system/xstore/functions/xstorequeryproductforcurrentgameasync.md)
